AWS::Serverless::Api (how to deploy to multiple stages)

AWS::Serverless::Api (how to deploy to multiple stages)

When deploying API Gateway with CloudFormation, use the api cloudformation package command.

The StageName in the SAM template is of type String, so multiple stages cannot be specified.


You can deploy the API to multiple stages by writing AWS::ApiGateway::Stage in the SAM template.


AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Test
    Type: AWS::Serverless::Api
      StageName: dev
      DefinitionUri: ./swagger.yaml
      OpenApiVersion: 3.0.1
    Type: AWS::ApiGateway::Stage
      StageName: prod
      RestApiId: !Ref RestApi
      DeploymentId: !Ref ApiDeploy
    Type: 'AWS::ApiGateway::Deployment'
      RestApiId: !Ref RestApi

DeploymentId is required when creating a stage, so AWS::ApiGateway::Deployment must also be written.

Create and deploy dev in AWS::Serverless::Api, create a stage named prod in AWS::ApiGateway::Stage, and create a stage named . /swagger.yaml is deployed to prod in AWS::ApiGateway::Deployment.

Now you can deploy the same API to dev,prod.

aws cloudformation package --template-file sam.yml --s3-bucket <bucket_name> --output-template-file output.yml --profile="default"
aws cloudformation deploy --template-file output.yml --stack-name <stack_name> --profile="default"

However, as shown in the following issue, it is better to use a configuration of one stack and one deployment environment.

Mutliple API stages but in a single document - best practice · Issue #198 · aws/serverless-application-model
Hello - I read the Best Practice guidelines for SAM Online Tech Talk which states to create multiple environments in a s...


Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL