Handle errors in Amazon API Gateway from integrated Lambda functions
Types of Lambda Integration
Proxy integration - The client sends a request to the API Gateway which acts as a proxy server, sends the entire request to its backend Lambda function. The integration method should be set as HTTP POST with the ARN of your Lambda function. Also, note that API Gateway should be granted with sufficient permission to invoke the function.
Non-proxy/Custom integration - Here the request received and response send by the API Gateway can be customized as per the requirement of the use case using Mapping templates and Models.
Handling of Lambda errors
For custom integration, the error returned by your function should be mapped properly to a standard HTTP status code which is apprehended by the clients.
Creating the function
import json
import random
def lambda_handler(event, context):
if random.choice([True, False]):
raise Exception('Dummy error ...')
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
{
"errorMessage": "Dummy error ...",
"errorType": "Exception",
"requestId": "abcd1234-abb9-4ca3-b848-993693f465ab",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 6, in lambda_handler
\nraise Exception('Dummy error ...')\n"
]
}
Creating the REST API
I have created a REST API with a resource - GET /lambda which is integrated to my above function.
Here is a snapshot of the API that I have created. Kindly note the Lambda proxy integration settings that can be enabled by marking the checkbox in the API Gateway console -
Results with -
A. Lambda proxy integration
2. The output when the function returned an error -
API Gateway matches the errorMessage returned by Lambda in the Integration Response, against the regex pattern configured. If there is a match then Lambda error is sent to the client along with the HTTP status code mapped else it goes along with the default response and status code(200 generally). In case there is no default response configured, the service throws an error.
Mapping the Lambda error
Key Learnings
- You have got an understanding of Proxy and Non-proxy/Custom Lambda integration for API Gateway.
- The behavior of the API response for both the setup when the function returns a normal response as well as error response.
- Also, you got to know how a standard error returned by an integrated function can be mapped to an HTTP Status code on the basis of the errorMessage using Lambda error regex in the Method Response settings of your API.
References
- Set up Lambda proxy integrations in API Gateway - https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
- Set up Lambda custom integrations in API Gateway - https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-custom-integrations.html
- Handle Lambda errors in API Gateway - https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html
Note: Not endorsed by AWS, all my understanding and opinions.
Comments
Post a Comment