I'm using this configuration to deploy to the 'Prod' Stage:
"ApiGatewayApi":
{
"Type": "AWS::Serverless::Api",
"Properties": {
"StageName": "Prod",
"Name" : "MainGateway",
...
I want to deploy different code to the 'Stage' stage. I tried to change 'StageName' to "Stage" but I get this error: "Stage already exists".
How do I deploy different code to different stages?
This solution is based on YAML format same can used in JSON format also.
There is a bug in SAM whenever you creating
StageNameits creating defaultStagealong with stage name which you provided likeProd. First you delete your current one then you can applied this changes.To solve this issue there is two ways by adding
OpenApiVersion: '2.0'in your YAML file :Approach 1: Under properties following to
StageNamecan add this. This properties can be added forAWS::Serverless::Apior other resources likeAWS::Serverless::Lambda.Approach 2: The following to your SAM template at the top level AND be sure you have defined a stage using "StageName" on your AWS::Serverless:Api resource. This will global level if you multiple resource like API or lambda etc.
Note: This solutions works ONLY when one creates API from scratch. If an API was created before, and user adds OpenApiVersion: '2.0' to it, it doesn't remove "Stage" stage. It needs to be added from the beginning.
AWS::Serverless::Apiis a very simple implementation and is not capable of managing multi stage under SAM, better useAWS::ApiGateway::RestApiand multipleAWS::ApiGateway::Stagereferring to RestApi resource.Reference :