如何使用 CloudFormation 创建新版本的 Lambda 函数? [英] How to create a new version of a Lambda function using CloudFormation?

查看:28
本文介绍了如何使用 CloudFormation 创建新版本的 Lambda 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 CloudFormation 创建新版本的 Lambda 函数.

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

我想拥有同一个 Lambda 函数的多个版本,以便我可以 (a) 将别名指向不同版本 - 如 DEV 和 PROD - 并且 (b) 能够回滚到早期版本

I want to have multiple versions of the same Lambda function so that I can (a) point aliases at different versions - like DEV and PROD - and (b) be able to roll back to an earlier version

这是我的 Lambda 版本的定义:

This is the definition of my Lambda version:

LambdaVersion:
  Type: AWS::Lambda::Version
  Properties:
    FunctionName:
      Ref: LambdaFunction

运行aws cloudformation create-stack"时会创建一个版本;但随后的aws cloudformation update-stack"命令不做任何事情.没有创建新的 Lambda 版本.

A version gets created when running "aws cloudformation create-stack" but the subsequent "aws cloudformation update-stack" commands don't do anything. There are no new Lambda versions created.

在我将新的 zip 文件上传到 S3 然后运行update-stack"后,我正在尝试获取创建的 Lambda 函数的新版本.我可以使用 CloudFormation 吗?AWS::Lambda::Version 真的坏了吗(正如这里提到的 https://github.com/hashicorp/terraform/issues/6067#issuecomment-211708071) 或者我只是没有得到什么?

I'm trying to get a new version of the Lambda function created after I upload new zip file to S3 and then run "update-stack". Can I do it with CloudFormation? Is AWS::Lambda::Version really broken (as mentioned here https://github.com/hashicorp/terraform/issues/6067#issuecomment-211708071) or am I just not getting something?

17 年 1 月 11 日更新亚马逊支持官方回复:...对于要发布的任何新版本,您需要定义一个附加(sic) AWS::Lambda::Version 资源..."

Update 1/11/17 Official reply from Amazon support: "...for any new version to be published you need to define an addition (sic) AWS::Lambda::Version resource..."

AWS CloudFormation/Lambda 团队,如果您正在阅读本文 - 这是不可接受的.修复它.

AWS CloudFormation/Lambda team, if you're reading this - this is unacceptable. Fix it.

推荐答案

AWS::Lambda::Version 没有用.您必须为每个 Lambda 版本添加一个新资源.如果你想为每个 Cloudformation 更新发布一个新版本,你必须破解系统.

AWS::Lambda::Version is not useful. You have to add a new resource for every Lambda version. If you want to publish a new version for every Cloudformation update, you have to hack the system.

我解决了这个问题,创建了一个 Lambda 支持的自定义资源,每次部署都会触发该资源.在这个 Lambda 中,我正在为参数中给出的 Lambda 函数创建一个新版本.

I solved this issue creating a Lambda backed custom resource which is triggered for every deployment. Inside this Lambda, I am creating a new version for the Lambda function given in parameter.

对于 Lambda 的来源,您可以查看 http://serverless-arch-eu-west-1.s3.amazonaws.com/serverless.zip

For the Lambda's source you can check http://serverless-arch-eu-west-1.s3.amazonaws.com/serverless.zip

以下是使用此部署 Lambda 函数的 Cloudformation 示例(您可能需要进行一些修改):

Here is the example Cloudformation using this Deployment Lambda function (You might need some modification):

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "DeploymentTime": {
      "Type": "String",
      "Description": "It is a timestamp value which shows the deployment time. Used to rotate sources."
    }
  },
  "Resources": {
    "LambdaFunctionToBeVersioned": {
      "Type": "AWS::Lambda::Function",
       ## HERE DEFINE YOUR LAMBDA AS USUAL ##
    },
    "DeploymentLambdaRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path": "/",
        "ManagedPolicyArns": [
          "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
        ],
        "Policies": [
          {
            "PolicyName": "LambdaExecutionPolicy",
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                    "lambda:PublishVersion"
                  ],
                  "Resource": [
                    "*"
                  ]
                }
              ]
            }
          }
        ]
      }
    },
    "DeploymentLambda": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Role": {
          "Fn::GetAtt": [
            "DeploymentLambdaRole",
            "Arn"
          ]
        },
        "Handler": "serverless.handler",
        "Runtime": "nodejs4.3",
        "Code": {
          "S3Bucket": {
            "Fn::Sub": "serverless-arch-${AWS::Region}"
          },
          "S3Key": "serverless.zip"
        }
      }
    },
    "LambdaVersion": {
      "Type": "Custom::LambdaVersion",
      "Properties": {
        "ServiceToken": {
          "Fn::GetAtt": [
            "DeploymentLambda",
            "Arn"
          ]
        },
        "FunctionName": {
          "Ref": "LambdaFunctionToBeVersioned"
        },
        "DeploymentTime": {
          "Ref": "DeploymentTime"
        }
      }
    }
  }
}

(免责声明:此代码是我书中的一部分,有关 Lambda 和 API 网关的更多信息,您可以查看:https://www.amazon.com/Building-Serverless-Architectures-Cagatay-Gurturk/dp/1787129195)

(Disclaimer: This code is a part of my book, for more information about Lambda & API Gateway you can check: https://www.amazon.com/Building-Serverless-Architectures-Cagatay-Gurturk/dp/1787129195)

这篇关于如何使用 CloudFormation 创建新版本的 Lambda 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆