3

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?

Rony Tesler
  • 1,207
  • 15
  • 25

1 Answers1

3

This solution is based on YAML format same can used in JSON format also.

There is a bug in SAM whenever you creating StageName its creating default Stage along with stage name which you provided like Prod. 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 StageName can add this. This properties can be added for AWS::Serverless::Api or other resources like AWS::Serverless::Lambda.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SAM template with a simple API definition
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: 'V1'
      OpenApiVersion: '2.0'
  ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event
    Type: AWS::Serverless::Function
    Properties:
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /
            Method: get
            RestApiId:
              Ref: ApiGatewayApi
      Runtime: python3.7
      Handler: index.handler
      InlineCode: |
        def handler(event, context):
            return {'body': 'Hello World!', 'statusCode': 200}

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.

Globals:
  Api:
    OpenApiVersion: 3.0.1
    Cors: '*'

Resources:
  ImplicitApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: s3://sam-demo-bucket/member_portal.zip
      Handler: index.gethtml
      Runtime: nodejs12.x
      Events:
        GetHtml:
          Type: Api
          Properties:
            Path: /
            Method: get
  ExplicitApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod

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::Api is a very simple implementation and is not capable of managing multi stage under SAM, better use AWS::ApiGateway::RestApi and multiple AWS::ApiGateway::Stage referring to RestApi resource.

Reference :

Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
  • I understand Stage is created because of a bug. I didn't understand anything from the rest of the answer, sorry. – Rony Tesler May 26 '21 at 12:53
  • you have to add `OpenApiVersion: '2.0' ` in your template to solve this issue – Avinash Dalvi May 26 '21 at 12:54
  • is there some understanding issue about solution @RonyTesler. I have tested this solution and it working. – Avinash Dalvi May 26 '21 at 13:00
  • 1
    I believe you it's working, I just didn't understand the instructions. For example, I don't understand the meaning of this sentence: "This will API or resource level". – Rony Tesler May 26 '21 at 19:22
  • It deleted my Prod stage! – Rony Tesler May 27 '21 at 17:22
  • Means existing stage ? Newly created by you ? – Avinash Dalvi May 27 '21 at 17:24
  • I had a 'Prod' stage (and the 'Stage' stage that was created automatically). I deleted the Stage one, now I want to deploy a 'Staging' stage, but to leave the Prod stage. So I did this:"StageName": "Staging", "OpenApiVersion": "2.0", But then the Prod stage was deleted. – Rony Tesler May 27 '21 at 17:27
  • That issue is still exist https://github.com/aws/serverless-application-model/issues/198. It not allowing to create multiple stages using serverless SAM. but you need you can CloudFormation see this example. https://gist.github.com/aviboy2006/6a72a378c8709e0773a688f00c6927c4. – Avinash Dalvi May 28 '21 at 05:32
  • So why did you tell me to add OpenApiVersion: '2.0'. – Rony Tesler May 28 '21 at 16:39
  • This is for error about default stage getting creating to solve that bug. Even if you don’t use OpenApiVerision existing stage getting replaced. – Avinash Dalvi May 28 '21 at 16:41
  • The global solution worked with me with version 3.1 (Approach 2) but Approach 1 not working – Tarek El-Mallah Dec 01 '21 at 19:54
  • 1
    I don't understand some instructions here either @RonyTesler, and I haven't made any attempt to fabricate an approach based on hints that may be dug out of the text, but it's well to realise there are posters on SO that act like they know what they're talking about even though they haven't got a clue. Some posters also like to post brief answers to questions they've had themselves, or have read elsewhere, which are unrelated to the OPs question. These answers also commonly tend to use poor English. I'm not able to evaluate this one but there is the possibility this is one of those randoms. – NeilG Aug 18 '23 at 02:01