aws_api_gateway_deployment + Lambda Race Condition HTTP500, 5s pause workaround #17604
Labels
bug
Addresses a defect in current functionality.
service/apigateway
Issues and PRs that pertain to the apigateway service.
Community Note
Terraform CLI and Terraform AWS Provider Version
Affected Resource(s)
aws_api_gateway_deployment
Terraform Configuration Files
Since funkiness with
aws_api_gateway_deployment
is well known, I'm just adding the relevant bit.Expected Behavior
The API Gateway should deploy the stage, and the invoke URL works completely to trigger the backend lambda, and return a HTTP200 to the client.
Output from workaround
Actual Behavior
Invoking the stage's invoke URL correctly passes the BODY of the request to the lambda and is processed correctly by lambda. (evidenced by cloudwatch logs and lambda outputs). However, the invoking client receives an HTTP 500.
Output before workaround
Steps to Reproduce
aws_api_gateway_deployment
andaws_api_gateway_stage
to deploy the api stageYou can easily toggle the deployment from TF and the manual deployment in API > Stages > Deployment History
Why is this a race condition?
The problem isn't about what terraform attempts to create, its WHEN it attempts to create it. By manually deploying after terraform apply, you are just doing the same thing as TF did, except every resource has been fully built and linked internally within AWS.
A workaround is to simply add:
This 5s pause allows something on the AWS backend to finish existing in time for the deployment to build correctly. There is likely a lot of other background issues contributing to this, but its easy to call it a race condition since its solvable with a pause.
I also tried moving the dependency logic to the deployment happens normally, but have the time delay gate the
aws_api_gateway_stage
resource. This always results in the race condition failure so I strongly believed its tied toaws_api_gateway_deployment
References
This lambda and the effective TF config is based on the POC from https://oozio.medium.com/serverless-discord-bot-55f95f26f743.
I am willing to provide a sanitized complete TF if required.
The text was updated successfully, but these errors were encountered: