【发布时间】:2023-03-15 06:25:01
【问题描述】:
因此,我尝试设置一个相当简单的堆栈,其中包含一个订阅 SNS 主题的 Lambda 函数。我想使用具有三个阶段的 CodePipeline:Source (GitHub) -> Build (CodeBuild) -> Deploy (CloudFormation)。
我设法拼凑了一个可以工作的模板和构建规范文件,但我不知道应该如何引用 CodeBuild 在 CloudFormation 模板中创建的输出工件;现在我只有占位符内联代码。
基本上,我应该在 Lambda 函数的 Code: 属性中添加什么以获取 CodeBuild 文件(这是我在 CodePipeline 中的输出工件)?
模板.yml:
AWSTemplateFormatVersion: 2010-09-09
Resources:
SNSTopic:
Type: 'AWS::SNS::Topic'
Properties:
Subscription:
- Endpoint: !GetAtt
- LambdaFunction
- Arn
Protocol: lambda
LambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Runtime: python3.6
Handler: main.lamda_handler
Timeout: '10'
Role: !GetAtt
- LambdaExecutionRole
- Arn
Code:
ZipFile: >
def lambda_handler(event, context):
print(event)
return 'Hello, world!'
LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
LambdaInvokePermission:
Type: 'AWS::Lambda::Permission'
Properties:
FunctionName: !GetAtt
- LambdaFunction
- Arn
Action: 'lambda:InvokeFunction'
Principal: sns.amazonaws.com
SourceArn: !Ref SNSTopic
buildspec.yml:
version: 0.2
phases:
install:
commands:
- pip install -r requirements.txt -t libs
artifacts:
type: zip
files:
- template.yml
- main.py
- lib/*
【问题讨论】:
-
我在您的 CloudFormation 模板中缺少您的
AWS::CodePipeline::Pipeline和AWS::CodeBuild::Project资源。 -
我没有用 CloudFormation 控制它们;还在学习 CF,所以尝试开始很简单。
标签: amazon-web-services aws-codepipeline aws-codebuild amazon-cloudformation