4

Simple question:

Does SAM Lambda auto-versioning using AutoPublishAlias work for AWS::Serverless::Function when code CodeUri changes but the actual code that it points to doesn't?

Background

We are deploying several Lambda@Edge functions using the SAM AWS::Serverless::Function resource type.

Our deployment runs via dropping code into an S3 bucket and then triggering a CloudFormation stack update. Our stack contains other things in addition to these Lambdas.

With this, we often have deployments that occur where the lambda code doesn't change.

Every deployment, however, the code URI changes to our newest deployment's bucket which, as intended, triggers a new version attempted to be published which is inline with note in SAM documentation (pasted below for context)

NOTE: AutoPublishAlias will publish a new version only when the CodeUri changes. Updates to other configuration (ex: MemorySize, Timeout) etc will not publish a new version. Hence your Alias will continue to point to old version that uses the old configurations.

The reason I am asking is because we've had some transient CloudFormation deployment issues that I've been trying to root cause that may be related to this. The error we get on the SAM generated version in the CloudFormation stack update is as follows:

A version for this Lambda function exists ( {some number} ). Modify the function to create a new version.

I've taken a look at the SAM code where the version is created and most of their documentation but couldn't find a clear answer.

We could likely manually create the AWS::Lambda::Function and AWS::Lambda::Version with a different logical ID on the version each time which should(?) fix the issue but we were trying to utilize SAM for auto-versioning of our Lambdas.

Any help is appreciated!

Community
  • 1
  • 1
Reed Hermes
  • 1,958
  • 4
  • 16
  • 22

2 Answers2

2

Yes, SAM will create a new version when CodeUri changes, however, Lambda will not allow you to create a new version if there have been no changes.

Brett
  • 2,706
  • 7
  • 33
  • 49
  • Thanks! Ended up doing the following: 1) Using `AWS::Lambda::Function` and `AWS::Lambda::Version` 2) Adding `__REPLACE_WITH_LAMBDA_SHA__` to the name of the `AWS::Lambda::Version` 3) Added some custom Rake build logic to generate a SHA-256 of the Lambda code using `Digest::SHA256.file` and then go and replace `__REPLACE_WITH_LAMBDA_SHA__` with that SHA – Reed Hermes Nov 25 '18 at 23:07
1

To workaround on that, I have a hack which is to create a NOUNCE environment variable with a random value that changes for every deploy. So CloudFormation believes that's a change on the code and deploy a new version of that Lambda and we have no more that version error.

Matheus Maximo
  • 149
  • 1
  • 8