How to Force Redeployment of AWS API Gateway using AWS CloudFormation

0 min read 128 words

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
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags