CloudFormation doesn't deploy to API gateway stages on update
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Overview
AWS CloudFormation is a comprehensive infrastructure as code (IaC) service that allows users to model, provision, and manage AWS resources and third-party applications. One common issue that arises during CloudFormation deployments is the unintentional overwriting or failing to deploy updates to Amazon API Gateway stages. Understanding the nuances of how CloudFormation interacts with API Gateway stages is critical for maintaining a smooth and predictable deployment process.
API Gateway Basics
Amazon API Gateway is a fully managed service that enables developers to create, publish, maintain, monitor, and secure APIs at scale. API Gateway handles all the aspects of accepting and processing up to hundreds of thousands of concurrent API calls, including traffic management, authorization, and access control, monitoring, and API version management.
Components of API Gateway
- RestApi: Represents a collection of resources and methods.
- Resources: Represent web service entities.
- Methods: Represent the operations allowed on a Resource.
- Stages: Deployments of an API to a specified endpoint (e.g.,
https://api.example.com/v1).
CloudFormation and API Gateway Deployments
When updating API Gateway resources using CloudFormation, a crucial detail to remember is that CloudFormation does not automatically handle the deployment of changes to specific stages in API Gateway. Thus, even though your stack updates successfully, the changes do not reflect in the API unless a deployment to a stage is manually triggered or explicitly defined in the CloudFormation template.
Key Reasons for Deployment Issues
- Stage Deployment Omitted: CloudFormation requires explicit instructions to create or update a deployment associated with a stage. If these steps are missing, updates will not propagate to the stage.
- Immutable Resource Properties: Certain resource properties in API Gateway are immutable, meaning they cannot be updated without resource replacement, potentially impacting deployment if not handled carefully.
- Manual Interventions: Deploying API Gateway changes might necessitate manual interactions through the AWS Management Console or CLI.
Example of Deployments with CloudFormation
Here's a simplified CloudFormation template snippet to illustrate API Gateway resource definition and deployment:
- Explicit Deployment: Always include explicit
AWS::ApiGateway::Deploymentresource in your template. This tells CloudFormation exactly when to deploy changes to a stage. - Stage Update: When an update is necessary, create a new deployment resource rather than updating existing properties on the
AWS::ApiGateway::Deploymentresource because it requires automatic replacement.

