Asaf Shay
Asaf Shay

Reputation: 31

How to deploy to aws using sam to spesific stage and not affecting other stages?

I want to be able to deploy to which stage I want without affecting other stages, for example, I want to deploy all the time to dev, and then deploy to prod without effect the dev stage

Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      EndpointConfiguration: REGIONAL
      StageName: Dev
      OpenApiVersion: "3.0"
      Name: asaf_api_second
      Description: "my api from sam"

if I change the StageName to Prod the stage Dev will delete, how can I prevent that?

Upvotes: 1

Views: 665

Answers (1)

Guy
Guy

Reputation: 21

The required solution includes the following steps:

  1. You will need to define the stage as a parameter in the root level:

    Parameters: 
      stage:
        Default: dev
        Description: StageName of API Gateway deployment
        Type: String
    
    Resources...
    
  2. Add a reference using (!Ref) to the stage parameter name in the resource definition:

    Resources:
      ApiGatewayApi:
        Type: AWS::Serverless::Api
        Properties:
          EndpointConfiguration: REGIONAL
          StageName: !Ref stage
          OpenApiVersion: "3.0"
          Name: asaf_api_second
          Description: "my api from sam"
    
  3. When you want to deploy to prod, override the required stage parameter in the deploy command using cli or in the toml file:

    sam deploy --parameter-overrides stage=prod
    

Upvotes: 1

Related Questions