如何使用CloudFormation创建Lambda函数的新版本? [英] How to create a new version of a Lambda function using CloudFormation?
问题描述
我正在尝试使用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后,创建新版本的Lambda函数。 -堆。我可以用CloudFormation做到吗? AWS :: Lambda ::版本是否真的损坏(如此处 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?
更新1/11 / 17
来自Amazon支持的官方回复:
...要发布任何新版本,您需要定义一个附加的(sic) AWS :: Lambda ::版本资源...
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屋!