How to create a new version of a Lambda function using CloudFormation?

后端 未结 11 647
清歌不尽
清歌不尽 2020-12-08 03:44

I\'m trying to create a new version of a Lambda function using CloudFormation.

I want to have multiple versions of the same Lambda function so that I can (a) point a

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 04:38

    Answer updated for February 2018

    You can use AWS SAM (Serverless Application Model), and its sam package and sam deploy commands to update Lambda. They are similar to aws cloudformation package and aws cloudformation deploy commands, but also let you update Lambda versions automatically.

    SAM can package your code (or take ZIP package you created otherwise), upload it to S3, and update the $LATEST Version of the Lambda from it. (If this is all you need, this can also be done with aws cloudformation, without SAM; code examples are same as below, but only use CloudFormation's standard declarations). Then, with SAM, if configured accordingly, you can also automatically publish a Version and update an Alias to point to it. It can also, optionally, use AWS CodeDeploy to gradually move traffic from previous Version to new one, and rollback in case of errors. All this is explained in Safe Lambda deployments.


    Technically, the idea is that every time you update the stack, you need your AWS::Lambda::Function's Code to point to the new package in S3. This will ensure that when you update the stack, Lambda's $LATEST version will be updated from the new package. Then, you can also automate the publishing of new Version and switch an Alias to it.

    For it, create a SAM template, which is similar to (a superset of) CloudFormation template. It may include SAM-specific declarations, like the one for AWS::Serverless::Function below. Point the Code to source code directory (or a prepackaged ZIP), and set the AutoPublishAlias property.

    ...
    
    MyFunction:
        Type: AWS::Serverless::Function
        Properties:
          ...  # all usual CloudFormation properties are accepted 
          AutoPublishAlias: dev  # will publish a Version and create/update Alias `dev` to point to it
          Code: ./my/lambda/src
    ...
    

    Run:

    $ sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket my-bucket
    

    This packages source directory contents as a ZIP (if Code is not a ZIP already), uploads it to S3 under new autogenerated key, and generates final CloudFormation template to packaged.yaml, putting for you the proper Code reference into it; like this:

    ...
    MyFunction:
        Properties:
          Code:
            S3Bucket: my-bucket
            S3Key: ddeeaacc44ddee33ddaaee223344
    ...
    

    Now you can use generated packaged.yaml with SAM, to create function Version:

    sam deploy --template-file packaged.yaml --stack-name my-stack [--capabilities ...]
    

    This will update Lambda's $LATEST version, and, if AutoPublishAlias was defined, publish it as a new Version and update the Alias to point to the newly published Version.

    See the examples in SAM GitHub repo for a complete template code.

提交回复
热议问题