API Gateway 통해서 Lambda 함수를 호출하는 API를 개발하고 있었다.
기본적으로 API Gateway 에서는 Lambda와 통신한 결과에 따라 HTTP Status Code를 반환해 주고 있었는데, 나는 응답 값에 따라 HTTP Status Code를 설정해 줄 일이 생겼다.
1. API Gateway에서 Integration Response 사용
첫 번째로 시도한 방법은 API Gateway에서 Integration Response 기능을 사용하는 것이었다.
Lambda Proxy Integration을 사용하면 API Gateway에서 Mapping Templates을 사용할 수 없다.
최대한 기존 소스를 건들지 않는 선에서 수정하려고 Integration Response에서 200 / 400 상황에 따라 응답 값을 조정해 주려고 했다.
뭔가 기본 값은 원하는대로 응답이 잘 오는데, Lambda Error Regax 설정한게 잘 적용이 안돼서 포기....
지나고 생각해보니 Lambda Proxy Integrations 사용한 것 처럼 응답형태를 맞춰야 했던 것 같다.
제대로 안하고 얼렁뚱땅 조금만 수정해서 하려고 하면 언제나 더 오래걸리고 고생하는 듯ㅎ
API Gateway에서 통합 응답 설정 - Amazon API Gateway
API Gateway에서 통합 응답 설정 비 프록시 통합의 경우에는 통합 응답을 하나 이상 설정하고 이를 기본 응답으로 만들어 백엔드에서 클라이언트로 반환되는 결과를 전달해야 합니다. 그 결과를 있
docs.aws.amazon.com
API Gateway에서 Lambda 오류 처리 - Amazon API Gateway
실행 시간에 API Gateway는 Lambda 오류의 errorMessage를 메시지를 selectionPattern 속성의 정규식 패턴과 비교합니다. 양쪽이 일치하면 API Gateway는 Lambda 오류를 해당 HTTP 상태 코드의 HTTP 응답으로 반환합니
docs.aws.amazon.com
2. Lambda Proxy Integrations 설정 사용
최종적으로 적용한 방법은 Lambda 함수 응답 값을 그대로 API Gateway에서 받아넘기도록 설정했다.
API Gateway에서 Lambda Proxy Integrations 설정을 사용하고 Lambda에서 StatusCode를 응답 값에 포함해 넘겨주었다.
Lambda Proxy Integrations 설정을 사용하고 입출력 형식을 맞춰주면 바로 적용된다.
별거 아닌데 공통으로 사용하는 Layer가 있어 최대한 공통부분은 건들지 않으려다 보니 뭔가 꽤 시간을 잡아먹었다.
[입력형식]
{
"resource": "/my/path",
"path": "/my/path",
"httpMethod": "GET",
"headers": {
"header1": "value1",
"header2": "value1,value2"
},
"multiValueHeaders": {
"header1": [
"value1"
],
"header2": [
"value1",
"value2"
]
},
"queryStringParameters": {
"parameter1": "value1,value2",
"parameter2": "value"
},
"multiValueQueryStringParameters": {
"parameter1": [
"value1",
"value2"
],
"parameter2": [
"value"
]
},
"requestContext": {
"accountId": "123456789012",
"apiId": "id",
"authorizer": {
"claims": null,
"scopes": null
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"extendedRequestId": "request-id",
"httpMethod": "GET",
"identity": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"sourceIp": "IP",
"user": null,
"userAgent": "user-agent",
"userArn": null,
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"path": "/my/path",
"protocol": "HTTP/1.1",
"requestId": "id=",
"requestTime": "04/Mar/2020:19:15:17 +0000",
"requestTimeEpoch": 1583349317135,
"resourceId": null,
"resourcePath": "/my/path",
"stage": "$default"
},
"pathParameters": null,
"stageVariables": null,
"body": "Hello from Lambda!",
"isBase64Encoded": false
}
[출력형식]
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },
"body": "..."
}
https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
Lambda proxy integrations in API Gateway - Amazon API Gateway
Thanks for letting us know this page needs work. We're sorry we let you down. If you've got a moment, please tell us how we can make the documentation better.
docs.aws.amazon.com
'IT 기술 > AWS' 카테고리의 다른 글
[AWS] EC2 절약하기!! - 람다, EventBridge로 스케줄링하기 -2 (4) | 2024.10.21 |
---|---|
[AWS] EC2 절약하기!! - 람다, EventBridge로 스케줄링하기 -1 (1) | 2024.10.21 |
[AWS] CodeDeploy 에러 수정 (1) | 2024.10.10 |
[AWS] Access key 발급하기 (0) | 2024.10.08 |