How to tackle timeout on Amazon API Gateway using AWS Step Functions and AWS Serverless Application Models (SAM)
How I solved time out errors on my AWS api gateway with serverless
- Amazon API Gateway which acts as a guardian that facilitates and controls access to different services or systems, ensuring that communication is easier and more secure. In my case, my API.
- AWS Lambda is a serverless computing service that runs code in response to events, without needing to manage the underlying infrastructure.
- AWS Step Functions is a service that allows visual and scalable coordination and orchestration of different steps and actions in a workflow, making it easier to create and manage complex applications and processes.
- I found that this function executed 5 tasks sequentially.
- The times for each of these tasks varied intermittently.
- Tasks 2 and 4 made synchronous calls to external services, meaning they waited for the result to complete before continuing with the rest of the code execution.
- The total processing time exceeded the 30-second limit configured in the API Gateway.
- An AWS account: Create a new account.
- A development environment in AWS Cloud9; for this example, you can choose to use a new t2.micro EC2 instance (this is included in AWS's free tier 😉).
1
$ git clone https://github.com/hsaenzG/APITimeoutsFix.git
1
2
cd APITimeoutsFix
$ sam build
1
$ sam deploy
- API Gateway with a Lambda integration. This is an example of how I found the original code, and if you execute this endpoint, you'll see that it returns a timeout error.
- API Gateway with an integration to a Step Function. This endpoint is responsible for executing the step machine, which runs multiple Lambdas in parallel to achieve the same result as the first API, but without performance errors.
- A state machine with the necessary flow configuration to obtain all the required information from the Harry Potter saga.
- Five Lambda Functions with the following characteristics:
LambdaIntegration/app.mjs
→ This function contains the original logic.stepFunctionIntegration/hechizos.mjs
→ This function contains the logic to call the endpoint that returns only the list of spells from the Harry Potter saga.stepFunctionIntegration/libros.mjs
→ This function contains the logic to call the endpoint that returns only the list of books from the Harry Potter saga.stepFunctionIntegration/personajes.mjs
→ This function contains the logic to call the endpoint that returns only the list of characters from the Harry Potter saga.stepFunctionIntegration/info.mjs
→ This function contains the logic to call the endpoint that returns the complete list of spells, books, characters, and general information from the Harry Potter saga.
- The integration of API Gateway with Step Functions is executed synchronously. This means that the API waits for the state machine to finish its execution before returning a response. If I didn't configure it this way, the endpoint response won't contain the desired result because the step machine will run independently of the API Gateway endpoint.Image not found
- For this use case, I used the Express workflow type for my state machine because it's ideal for mobile application backends. In my case, it's used as part of a web application backend, so it worked perfectly. However, you can also apply it to other use cases such as:
- High-volume event processing workloads like IoT data.
- Streaming, transforming, and processing data.
- It's important to note that this type of state machine has a maximum execution time of 5 minutes.
- The Step Functions service is included in the AWS free tier for the Standard workflow type. However, the execution billing for an Express workflow is not and the cost of it is based on the number of executions, execution duration, and memory consumed during execution. For more information on Step Functions costs, you can visit: Step Functions Pricing
1
2
3
Request URL: https://<your API URL>/Prod/LambdaIntegration/
Request Method: POST
Body: {}
WizaringStepFunctionsApi
. You can get the API URL from: API Gateway → WizaringStepFunctionsApi → Stages → v2.1
2
3
Request URL: https://<your API URL>/v2/WizaringWorldParalell
Request Method: POST
Body: {}
1
$ sam delete