title: Specify multiple API stages and Lambda Aliases in SAM template defining a single AWS::Serverless::Function
body: <p><strong>What I am aiming for</strong>
  I'm trying to write a (single) SAM template for obtaining a Lambda function triggered by Api Gateway events.</p>
  <p>I want to have multiple API Stages (say &quot;dev&quot;, &quot;testing&quot;, &quot;prod&quot;) and I want each of them to be mapped onto a Lambda alias with the same name.</p>
  <p>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, &quot;dev&quot; alias is meant to point to $LATEST code version.</p>
  <p><strong>What I tried and what I got</strong></p>
  <p>I've modified the classic &quot;hello_world&quot; template as follows.
  Now, when deploying for the first time (say, to &quot;dev&quot;) everything seems to work as expected.
  <em>But</em> if I try to deploy to &quot;testing&quot; stage, the APIs at this new stage respond, while those at &quot;dev&quot; stop responding.</p>
  <p>What am I missing?</p>
  <pre><code>AWSTemplateFormatVersion: '2010-09-09'
  Transform: AWS::Serverless-2016-10-31
  Description: &gt;
    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: &quot;API Gateway endpoint URL for Prod stage for Hello World function&quot;
  #    Value: !Sub &quot;https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/&quot;
    HelloWorldFunction:
      Description: &quot;Hello World Lambda Function ARN&quot;
      Value: !GetAtt HelloWorldFunction.Arn
    HelloWorldFunctionIamRole:
      Description: &quot;Implicit IAM Role created for Hello World function&quot;
      Value: !GetAtt HelloWorldFunctionRole.Arn
  
  </code></pre>