Specify multiple API stages and Lambda Aliases in SAM template defining a single AWS::Serverless::Function

Viewed 2546

What I am aiming for I'm trying to write a (single) SAM template for obtaining a Lambda function triggered by Api Gateway events.

I want to have multiple API Stages (say "dev", "testing", "prod") and I want each of them to be mapped onto a Lambda alias with the same name.

I'd prefer not to generate a new lambda version at each deployment, and I like manually setting the lambda version to be used by each lambda alias. Of course, "dev" alias is meant to point to $LATEST code version.

What I tried and what I got

I've modified the classic "hello_world" template as follows. Now, when deploying for the first time (say, to "dev") everything seems to work as expected. But if I try to deploy to "testing" stage, the APIs at this new stage respond, while those at "dev" stop responding.

What am I missing?

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

  Sample SAM Template for zzzz

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Parameters:
  StageAliasName:
    Description: stage / alias name to be used in this deploy
    Type: String
    AllowedValues:
      - prod
      - stage
      - dev
      - v1
    Default: dev

Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref StageAliasName

  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      AutoPublishAlias: !Ref StageAliasName
      FunctionName: zzz
      Environment:
        Variables:
          stage: !Ref StageAliasName
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /hello
            Method: GET
#            Auth:
#              ApiKeyRequired: true



Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
#  HelloWorldApi:
#    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
#    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

1 Answers
Related