Search code examples
amazon-web-servicesamazon-cloudfrontaws-cloudformationserverless-framework

How to get the latest lambda version in cloudformation yml?


I'm trying to reference an edge lambda for cloudfront distribution in cloudformation.

What I have now is:

          LambdaFunctionARN:
            Fn::GetAtt: [BasicAuthLambdaFunction,Arn]

But I get this error:

An error occurred: GGGCloudFrontDistribution - The function ARN must reference a specific function version. (The ARN must end with the version number.)

So.. is there some kind of technique to reference the latest version of the function?


Solution

  • You can't use the latest version. You have to use a specific version as the documentation you linked states:

    You must specify the ARN of a function version; you can't specify a Lambda alias or $LATEST.

    If you are creating the Lambda function in your template, you can also create a version and use that.

      BasicAuthLambdaFunctionVersion: 
        Type: "AWS::Lambda::Version"
        Properties:
          FunctionName:
            Ref: BasicAuthLambdaFunction
    # ...
          LambdaFunctionARN:
            Fn::GetAtt: [BasicAuthLambdaFunctionVersion,Arn]
    

    Note that when updating the stack, a new version will not be created when the Lambda function code changes. You have to manually create and use a new version by changing the name of BasicAuthLambdaFunctionVersion to BasicAuthLambdaFunctionVersion2 or something else. To automate this, you can edit the template with a script before using it.

    If you are using the Serverless Framework, take a look at:

    https://github.com/silvermine/serverless-plugin-cloudfront-lambda-edge https://github.com/serverless/serverless/issues/3944