
LAB. AWS API Gateway와 Lambda 함수 연동
워크플로우
1. Lambda 함수 생성
2. API Gateway 구성
3. Postman 설치 후 API TEST
import json
def lambda_handler(event, context):
users = [
{"id": 1, "name": "John"},
{"id": 2, "name": "seongmi"}
]
return {
'statusCode': 200,
'body': json.dumps(users)
}import json
def lambda_handler(event, context):
# event['body']가 이미 파싱된 딕셔너리라면 json.loads()를 제거
body = event['body'] if isinstance(event['body'], dict) else json.loads(event['body'])
new_user = {
"id": 3,
"name": body.get("name")
}
return {
'statusCode': 201,
'body': json.dumps({"message": "User created", "user": new_user})
}{
"statusCode": 200,
"body": "[{\"id\": 1, \"name\": \"John\"}, {\"id\": 2, \"name\": \"seongmi\"}]"
}{
"name": "Alice"
}