I want to change the Stage created when creating API Gateway with AWS SAM from Prod to a custom name

0

Hello.

I am creating an API Gateway using AWS SAM.
At that time, I am trying to name "dev" as "StageName".
However, when I actually deploy it, a stage named "Prod" is created.
Is it not possible to set a custom name for an API Gateway stage in AWS SAM?
I checked the GitHub issue below, but the issue was not resolved.
https://github.com/aws/serverless-application-model/issues/191

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Globals:
  Api:
    OpenApiVersion: 3.0.2

Parameters:
  Stage:
    Type: String
    Default: dev

Resources:
  ServerlessRestApi:
    Type: AWS::Serverless::Api
    Properties:
      OpenApiVersion: 3.0.2
      StageName: !Ref Stage
      Name: !Sub "${Stage}-Api"

  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
            RestApiId: !Ref ServerlessRestApi

Outputs:
  CsrfTokenApiUrl:
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/hello"

samconfig.toml is as follows.

version=0.1

[development.deploy.parameters]
stack_name = "dev-api-stack"
resolve_s3 = true
s3_prefix = "dev-api-stack"
region = "ap-northeast-1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
disable_rollback = true
parameter_overrides = [
    "Stage=dev"
]
image_repositories = []

app.py looks like this:

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body":"message: hello world",
    }
1 Answer
3
Accepted Answer

Hi Riku,

Did you look at this thread, it suggests following:

To get around this bug, we added the fix for it behind the OpenApiVersion flag. To opt into this fix, you would need to specify OpenApiVersion flag like here. Regardless of the value (2.0, 3.0.1, etc), the stage stage fix will be applied if the flag is present.

I tried in my lab and it fixed the problem. Please give a try and let me know how it goes.

Comment here if you have additional questions, happy to help.

Abhishek

profile pictureAWS
EXPERT
answered 8 months ago
profile picture
EXPERT
reviewed 8 months ago
profile picture
EXPERT
reviewed 8 months ago
  • Thank you for your reply. The template I'm using already has "OpenApiVersion: 3.0.2" set.

    AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    
    Globals:
      Api:
        OpenApiVersion: 3.0.2
    
    Parameters:
      Stage:
        Type: String
        Default: dev
    
    Resources:
      ServerlessRestApi:
        Type: AWS::Serverless::Api
        Properties:
          OpenApiVersion: 3.0.2
          StageName: !Ref Stage
          Name: !Sub "${Stage}-Api"
    
      HelloWorldFunction:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: hello_world/
          Handler: app.lambda_handler
          Runtime: python3.9
          Architectures:
            - x86_64
          Events:
            HelloWorld:
              Type: Api
              Properties:
                Path: /hello
                Method: get
                RestApiId: !Ref ServerlessRestApi
    
    Outputs:
      CsrfTokenApiUrl:
        Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/hello"
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions