Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 212
Daniel Birowsky Popeski Avatar asked Jan 07 '18 13:01

Daniel Birowsky Popeski


People also ask

How do you update Lambda function in CloudFormation?

So, to update the Lambda function, all you need to do is update a stack parameter. But real deployments are much more complex and can have many Lambda functions that may need to be updated. For such cases, you would have to use some advance CloudFormation tricks to update the relevant strings in the template.

What is $latest Lambda version?

When you refer to "version" in lambda you usually mean "published version" (e.g., 1, 2, 3) which is immutable. $LATEST is simply most recent "unpublished version" which you can change at will. You keep working/testing on $LATEST till you are satisfied with it.

Which section of a CloudFormation template will you use to define the Lambda version?

To automatically deploy a Lambda function, we will add information under the Resources section. Create a YAML file for your new template (this tutorial will use YAML, but CloudFormation also supports JSON format). Under the Resources section, we'll start by defining the Identity and Access Management (IAM) Role.


1 Answers

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

like image 158
kichik Avatar answered Sep 19 '22 05:09

kichik