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.

AWS::ApiGateway::Stage

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

sam.yml

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Test
Resources:
  RestApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      DefinitionUri: ./swagger.yaml
      OpenApiVersion: 3.0.1
  ProdStage:
    Type: AWS::ApiGateway::Stage
    Properties:
      StageName: prod
      RestApiId: !Ref RestApi
      DeploymentId: !Ref ApiDeploy
  ApiDeploy:
    Type: 'AWS::ApiGateway::Deployment'
    Properties:
      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...

コメント

タイトルとURLをコピーしました