이번에도 LAMBDA를 메인으로 활용하여 진행했던 프로젝트를 설명하도록 하겠다.
다만, 아키텍쳐보다는 실제 Lambda 코드 위주로 적어보고자 한다. 프로젝트의 자세한 소개는 아래 링크 참고.
https://github.com/SW-GASBY/Gasby-LAMBDA
GitHub - SW-GASBY/Gasby-LAMBDA: AWS LAMBDA FUNCTIONS
AWS LAMBDA FUNCTIONS . Contribute to SW-GASBY/Gasby-LAMBDA development by creating an account on GitHub.
github.com
ARCHITECTURE
해당 프로젝트에서는 총 7개의 Lambda 함수를 사용한다. 각 Lambda 함수는 특정 상황에 따라 동작하며, 각각의 트리거에 따라 작동하여 자동으로 파이프라인이 실행되도록 설계되었다.
이제 부터 해당 아키텍쳐의 주요 람다 함수 코드 몇가지에 대해 설명하겠다.
upload-gasby-request
- Role: 유저의 영상 업로드 및 요청
- Method: Post
- Trigger: API Gateway
import requests
import base64
url = 'https://nj7ceu0i9c.execute-api.ap-northeast-2.amazonaws.com/deploy/request'
# user 요청받아서 만들기
file_path = '/Users/jungheechan/Desktop/kakao.mp4'
userId = '1'
# 파일을 base64로 인코딩
with open(file_path, 'rb') as f:
file_content = f.read()
file_content_base64 = base64.b64encode(file_content).decode('utf-8')
# HTTP POST 요청 보내기
payload = {
'file': file_content_base64,
'userId': userId
}
response = requests.post(url, json=payload)
# 응답 확인
print(response.status_code)
print(response.json())
먼저 가장 기본적인 API Gateway를 통해서 요청 받는 람다이다. 해당 람다는 API 요청과 똑같은 방식으로 요청을 받게 된다.
run-mot
- Role: 유저의 요청에 따른 MOT predict 실행
- Endpoint: Trigger로 작동
- Trigger: S3- [gasby-req]
import json
import requests
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
object_key = event['Records'][0]['s3']['object']['key']
folder_name = '/'.join(object_key.split('/')[:-1])
logger.info(folder_name)
api_url = ''
payload = {
'payload': folder_name
}
headers = {
'Content-Type': 'application/json'
}
api_response = requests.post(api_url, json=payload)
return {
'statusCode': 200,
'body': json.dumps('Successfully processed the file and sent API request!')
}
해당 람다는 S3 Trigger로 작동하게 된다. 때문에 해당 코드의 아래 부분을 잘보면
object_key = event['Records'][0]['s3']['object']['key']
folder_name = '/'.join(object_key.split('/')[:-1])
S3에 Object가 올라오면 작동을 하고 또한 어떤 폴더에 저장이 되었는지도 추출하게끔 되어 있다.
해당 프로젝트에서는 총 두가지 Trigger를 응용했다. API Gateway와 S3 Trigger 방식이다. 이 외에도 DB Update 방식 등 다양한 방식이 있다는데 기회가 되면 다른 Trigger 동작도 사용해보고 싶다.

'AWS' 카테고리의 다른 글
AWS Storage: EC2-EBS와 EFS (0) | 2025.01.22 |
---|---|
AWS CLOUD: EC2와 EC2 Purchasing Option (0) | 2025.01.20 |
AWS: AWS Identity & AccessManagement (AWS IAM)란 (0) | 2025.01.08 |
AWS CLOUD: LAMBDA 활용1 (3) | 2024.12.30 |
AWS CLOUD: LAMBDA란? (0) | 2024.12.29 |