How to Force Redeployment of AWS API Gateway Using AWS CloudFormation

  • Home /
  • Blog Posts /
  • How to Force Redeployment of AWS API Gateway using AWS CloudFormation

If you have an AWS API Gateway resource, and need it to force a redeployment using CloudFormation, then you can use the TIMESTAMP trick.

Example AWS CloudFormation Extract

template.yaml extract:

APIGatewayStage:
    Type: AWS::ApiGateway::Stage
    Properties:
      StageName: !Sub ${EnvironmentTagName}
      RestApiId: !Ref APIGateway
      DeploymentId: !Ref APIGatewayDeployment__TIMESTAMP__
      TracingEnabled: true
      MethodSettings:
          - DataTraceEnabled: true
            HttpMethod: "*"
            LoggingLevel: INFO
            ResourcePath: "/*"
            MetricsEnabled: true

  APIGatewayDeployment__TIMESTAMP__:
    Type: AWS::ApiGateway::Deployment
    Properties:
      RestApiId: !Ref APIGateway
      Description: !Sub ${EnvironmentTagName} Deployment __TIMESTAMP__

  APIGateway:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: !Ref "AWS::StackName"
      ...

As we can see, on line 15 above, we use the __TIMESTAMP__ string in the APIGatewayDeployment stage.

Example CI/CD Bash Extract

Now we simply make sure to update the template.yaml file with our CI run, replacing the occurence of __TIMESTAMP__ with the TIMESTAMP variable generated:

TIMESTAMP="$(date +%s)"
sed -i "s/__TIMESTAMP__/${TIMESTAMP}/g" template.yaml