serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

How to add versioning to continuous deployment model

Open jabalsad opened this issue 9 years ago • 27 comments

Hi,

Uncertain where best to propose this question, but this seems like a good place to start. I've been playing with automatically deploying lambda functions using this serverless application model + cloudformation, based off this document [1]. One thing I've noticed is that with every change set that code pipeline creates in cloudformation, I don't get a new version of the lambda function. How do you propose I add some kind of versioning (perhaps correlated with the CodeUri) to the cloudformation change set?

I appreciate your input on the matter.

[1] http://docs.aws.amazon.com/lambda/latest/dg/automating-deployment.html

jabalsad avatar Dec 16 '16 00:12 jabalsad

We are aware of this issue. SAM doesn't support Lambda function versioning yet.

sanathkr avatar Dec 16 '16 01:12 sanathkr

I'm having the same issue after finallybgetting my pipeline working. Should we be able to use cfn native lambda support to version lambdas as a workaround?

stuartstevenson avatar Dec 19 '16 07:12 stuartstevenson

@stuartstevenson yes you can use CFN native lambda resource as a workaround

sanathkr avatar Dec 19 '16 19:12 sanathkr

Thank you for the response @sanathkr. I have just tested out a CFN template with a function, version and alias resource and when I updated the function the version remained the same. I realise now I have made some large and incorrect assumptions, namely that CFN has the same feature set and behaviour of the API, and the lambda version was doing some magic under the hood.

I'm pondering if it would not be too much effort to create a custom resource to check the state of the codesha against the last version, and if it varies then update

stuartstevenson avatar Dec 19 '16 21:12 stuartstevenson

A Lambda version is an immutable resource which is created by AWS::Lambda::Version CFN resource type. To create a new Lambda version, you need to add a new AWS::Lambda::Version resource to your CFN template with a different resource name.

This behavior makes it hard for SAM to automatically create versions for you. SAM template should somehow automatically store all earlier version resources

sanathkr avatar Dec 22 '16 00:12 sanathkr

This article published on the 20th is a nice example of how to build a full CD pipeline using SAM https://aws.amazon.com/blogs/compute/continuous-deployment-for-serverless-applications/. Interestingly it doesn't use versions or aliases. I've noticed that Serverless works in a similar way of creating an api and lambda per 'stage'. Is the aim with SAM to use Versions and Aliases instead of duplicating resources?

stuartstevenson avatar Dec 22 '16 12:12 stuartstevenson

Versions & Aliases are powerful tools to help you build a production service on Lambda. SAM certainly wants to explore the possibility of supporting it

sanathkr avatar Dec 22 '16 18:12 sanathkr

@jabalsad I ran into the same difficulty and resolved it by implementing custom actions in CodePipeline for versioning and aliasing the Lambda Functions that are created/updated by my CloudFormation changeset executions. I published the functions on GitHub but have not yet had time to add documentation or write up a blog post about them. In case they help you in the meantime:

https://github.com/qblu/codepipeline-lambda-versioner https://github.com/qblu/codepipeline-lambda-aliaser

They both use this parent package which provides a generic way to build CodePipeline custom actions as Node functions:

https://github.com/qblu/codepipeline-custom-action

lafiosca avatar Jan 10 '17 19:01 lafiosca

@sanathkr I recently ran into this same issue. Is there an updated ETA on when versioning and aliases will be addressed in SAM? Thanks!

jbankston avatar Apr 15 '17 20:04 jbankston

@sanathkr I am also looking versioning and alias in SAM, wondering does these functionalities are supported by SAM now

aparnamane avatar Apr 21 '17 00:04 aparnamane

Seems like SAM could implement updating Lambda Versions and Aliases the same way it handles ApiGateway Deployments, by generating a new unique logical id each time.

lafiosca avatar May 19 '17 21:05 lafiosca

Any ETA on the feature-request?

gabpaiz3 avatar Jul 10 '17 22:07 gabpaiz3

@lafiosca But that means earlier versions will be deleted, which is undesirable.

I don't have an ETA yet. We are working with CloudFormation to find a workaround to the problem I described earlier.

sanathkr avatar Jul 11 '17 06:07 sanathkr

@sanathkr why would the old versions need to be deleted? Seems like we just publish a new version and then re-point the API resource to use these. I really like the approach described in https://stackoverflow.com/a/41455577/2962507 (1st option), similar to what @lafiosca described.

I.e. we DON'T use the inconvenient AWS::Lambda::Version resource. Rather, during transform:

  1. we generate a "template version" parameter and set its default value to a new random GUID.
  2. add a global function (one per template) that takes a function Arn and publishes a new version of that function. It also exports the new Version so that it can be used elsewhere in the template.
  3. for each serverless function, we add a custom resource whose service token points to (2), and takes in the function Arn as well as the template version from (1). The reason to specify the latter is such that the custom resource is updated (and thus a new function version is published) every time the stack is updated.

Elsewhere in the template, we can refer both to the function Arn attribute, as well as the new Version attribute (which can actually be set to VersionArn for convenience). This way, we can support both the "old" behavior where we just point to function Arn (corresponding to the $LATEST version), and the "new" behavior where we refer to the versioned Arn. When the user uses the "new" behavior (where they point to VersionArn instead of Arn), API Gateway resource switches to the new version in production only at the very last step, when the stage is updated. Prior to that moment, all of our functions still point to the "old" version and continue working coherently during the stack update (my understanding is, currently SAM replaces the code for all of the functions first, and then updates the stage, which may introduce temporary incosistencies).

Are there any show stoppers for this? Seems like a simple enough approach that is both directly implementable using existing CloudFormation capabilities (e.g. custom resources), and doesn't break existing behavior.

dinvlad avatar Jul 13 '17 17:07 dinvlad

@sanathkr I haven't thought this through fully yet, but would DeletionPolicy: Retain help with that?

lafiosca avatar Jul 20 '17 16:07 lafiosca

@dinvlad Yeah, custom resources will solve the "I want to always point to latest version" problem. But if you want to track and point to multiple versions, the template definition becomes less intuitive. Instead of GUID, we can use Lambda function's CodeSHA if you want to auto-publish on every change.

sanathkr avatar Aug 01 '17 19:08 sanathkr

Agreed, a code hash would be preferable. I actually ended up calculating and passing it to my template as a parameter.

dinvlad avatar Aug 01 '17 21:08 dinvlad

+1. The SHA-256 code hash is how others like Terraform calculate changes.

jbankston avatar Aug 01 '17 21:08 jbankston

It's clunky, but I managed to solve this issue this way:

AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
  SomeFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      FunctionName: !Sub "${AWS::StackName}-someFunc"
      Handler: "index.handler"
      CodeUri: "."
      Runtime: "nodejs6.10"
      MemorySize: 256
      Timeout: 5
      Environment:
        Variables:
          ANOTHER_FUNC_VERSION: !Sub "${AnotherFunctionV2.Version}"
      Policies:
      - AWSLambdaBasicExecutionRole
  AnotherFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      FunctionName: !Sub "${AWS::StackName}-anotherFunc"
      Handler: "magic.handler"
      CodeUri: "."
      Runtime: "nodejs6.10"
      MemorySize: 256
      Timeout: 5
      Policies:
      - AWSLambdaBasicExecutionRole
  AnotherFunctionV2:
    Type: "AWS::Lambda::Version"
    Properties:
      FunctionName: !Ref AnotherFunction

So in this scenario, SomeFunction might have some code that invokes AnotherFunction using a specific version. Any time I update the AnotherFunc code, I rename the AWS::Lambda::Version resource to the next version up, e.g. AnotherFunctionV3, then package and deploy.

ffxsam avatar Oct 23 '17 05:10 ffxsam

This is now available in SAM. Checkout: https://github.com/awslabs/serverless-application-model/releases/tag/1.3.0

sanathkr avatar Nov 29 '17 01:11 sanathkr