Search code examples
aws-cloudformationaws-sam

AWS SAM - How can create an API event for a FunctionAlias?


My SAM template is currently using a Lambda function to provide information through an API Gateway. However, I would like to have the API Gateway pointing to an alias instead.

How can I do it with SAM?

LambdaFunction:
    Type: 'AWS::Serverless::Function'
    Properties: 
      Runtime: nodejs14.x
      Handler: index.handler
      CodeUri: ./software
      MemorySize: 128
      FunctionName: Name
      Timeout: 5
      Policies:
      - AmazonDynamoDBReadOnlyAccess
      Events:
        MyApiResource:
          Type: Api
          Properties:
            Path: /path
            Method: get
            RestApiId: !Ref ApiGatewayApi
  FunctionAlias:
    Type: AWS::Lambda::Alias
    Properties:
      FunctionName: !Ref LambdaFunction
      FunctionVersion: !GetAtt FunctionVersion.Version
      Name: prod
  

I tried to cut and past the events to the alias but it didnt work.


Solution

  • When adding the AutoPublishAlias property to the lambda function, the API Gateway will use that alias when creating the integrations (By using FQ Lambda ARN)

    So you just need to add the following to the function:

    AutoPublishAlias: prod
    

    Link here