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?

1 Answer 1

4

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 :

Sign up to request clarification or add additional context in comments.

12 Comments

I understand Stage is created because of a bug. I didn't understand anything from the rest of the answer, sorry.
you have to add OpenApiVersion: '2.0' in your template to solve this issue
is there some understanding issue about solution @RonyTesler. I have tested this solution and it working.
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".
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.