如何设置从CodeCommit到Lambda函数的自动部署?



我正在尝试"部署";CodeCommit存储库的内容到Lambda函数(而不是应用程序)。在这个特殊的例子中,它是一个简单的复制/粘贴从源到目标。

我正在努力寻找一个不涉及设置另一个Lambda函数的解决方案。根据我的理解,有一个使用CodeBuild和CloudFormation的解决方案。

有人有解决这个问题的办法吗?或者,你能指出任何好的文档吗?

p。S:

我发现这个问题似乎回答了我的问题,但相关答案中的链接已经过时了。

您可以使用CodeBuild作业构建代码提交管道,其中CodeCommit存储库有如下SAM模板,并运行

sam build && sam deploy

codebuild作业。


AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A sample SAM template for deploying Lambda functions.
Resources:
# Details about the myDateTimeFunction Lambda function
myDateTimeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: myDateTimeFunction.handler
Runtime: nodejs12.x
# Creates an alias named "live" for the function, and automatically publishes when you update the function.
AutoPublishAlias: live
DeploymentPreference:
# Specifies the deployment configuration
Type: Linear10PercentEvery2Minutes

这个文档页描述了Lambda函数的CodeCommit滚动部署

这就是我的解决方案。

我设置了一个以CodeCommit为源和构建阶段(没有部署阶段)的管道。

构建阶段读取构建规范。该文件本身读取名为template.yml的SAM模板。SAM栈是通过CloudFormation创建的。

我创建了一个s3桶来保存构建工件。

下面是示例buildspec。yml文件:

version: 0.2
phases:
install:
commands:
- echo Nothing to do in the install phase...
pre_build:
commands:
- echo Nothing to do in the pre_build phase...
build:
commands:
- aws cloudformation package --template-file template.yml 
--s3-bucket <bucketname>
--output-template-file newtemplate.yml
- aws cloudformation deploy --stack-name <stackname>
--capabilities CAPABILITY_IAM
--template-file newtemplate.yml
--role-arn arn:aws:iam::<account number>:role/CloudFormationServiceRole
post_build:
commands:
- echo Build completed

下面是示例模板。文件:

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: CloudFormation Stack for the lambda function
Resources:
# Details about the Lambda function
<StackName>:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
CodeUri: src/
# Creates an alias named "live" for the function, and automatically publishes when you update the function.
AutoPublishAlias: live
DeploymentPreference:
# Specifies the deployment configuration
Type: AllAtOnce

文件结构为:

.
├── src/
│   ├── node_modules/
│   └── index.js
├── builspec.yml
└── template.yml

确保你为CloudFormation和CodeBuild IAM设置了正确的IAM策略。

最新更新