Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I'm using SAM to deploy a Lambda function and make it callable over HTTP with via API Gateway using roughly this template snippet:

MyFunction:

  Type: AWS::Serverless::Function

  Properties:

    …

    Events:

      MyApi:

        Type: Api

        Properties:

          Path: /

          Method: any

This works, but it creates an API stage called "Prod", which has to be used as a prefix for all URLs. I don't want my URLs to be "https://something/Prod/foo", I want them to be "https://something/v1/foo", i.e. something that I choose.

How do I change the name of the stage?

I have tried declaring the API as a separate resource and used the StageName property to set the name of the stage, however, that requires me to also set DefinitionBody, and that seems to be a deep rabbit hole.

MyFunction:

  Type: AWS::Serverless::Function

  Properties:

    …

    Events:

      MyApi:

        Type: Api

        Properties:

          Path: /

          Method: any

          RestApiId: !Ref MyApi

MyApi:

  Type: AWS::Serverless::Api

  Properties:

    StageName: v1

    DefinitionBody:

      ???

I know that ??? in the example above is supposed to be Swagger, but I would prefer to not have to write anything there, the template is getting verbose enough at is. Since I don't have to write this part if I can just live with the stage name "Prod" it seems to me that there must be a way to avoid having to write anything there and just set the stage name.

How can I change the name of the stage without having to write a lot of template code and/or Swagger?

1 Answer

0 votes
by (44.4k points)

There is no need to include DefinitionBody in your example because it is removed in SAM Version 1.7.0

MyFunction:

  Type: AWS::Serverless::Function

  Properties:

    …

    Events:

      MyApi:

        Type: Api

        Properties:

          Path: /

          Method: any

          RestApiId: !Ref MyApi

MyApi:

  Type: AWS::Serverless::Api

  Properties:

    StageName: v1

Browse Categories

...