Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
为 CodeCommit 来源(Amazon CloudFormation 模板)创建 EventBridge 规则
Amazon CloudFormation 要使用创建规则,请更新您的模板,如下所示。
更新您的管道 Amazon CloudFormation 模板并创建 EventBridge 规则
-
在模板下的模板中Resources
,使用AWS::IAM::Role
Amazon CloudFormation 资源配置允许您的活动启动管道的IAM角色。此条目将创建一个使用两个策略的角色:
-
第一个策略允许代入角色。
-
第二个策略提供启动管道所需的权限。
我为何做出此更改? 添加AWS::IAM::Role
资源可以 Amazon CloudFormation 为创建权限 EventBridge。此资源已添加到您的 Amazon CloudFormation 堆栈中。
- YAML
-
EventRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
-
PolicyName: eb-pipeline-execution
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action: codepipeline:StartPipelineExecution
Resource: !Join [ '', [ 'arn:aws:codepipeline:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref AppPipeline ] ]
- JSON
-
"EventRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"events.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
},
"Path": "/",
"Policies": [
{
"PolicyName": "eb-pipeline-execution",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "codepipeline:StartPipelineExecution",
"Resource": {
"Fn::Join": [
"",
[
"arn:aws:codepipeline:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "AppPipeline"
}
]
...
-
在模板的下方Resources
,使用AWS::Events::Rule
Amazon CloudFormation 资源添加 EventBridge 规则。此事件模式会创建一个事件,以监控向存储库推送更改的操作。当 EventBridge 检测到存储库状态更改时,将在目标管道StartPipelineExecution
上调用该规则。
我为何做出此更改? 添加AWS::Events::Rule
资源 Amazon CloudFormation 即可创建事件。此资源已添加到您的 Amazon CloudFormation 堆栈中。
- YAML
-
EventRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- aws.codecommit
detail-type:
- 'CodeCommit Repository State Change'
resources:
- !Join [ '', [ 'arn:aws:codecommit:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref RepositoryName ] ]
detail:
event:
- referenceCreated
- referenceUpdated
referenceType:
- branch
referenceName:
- main
Targets:
-
Arn:
!Join [ '', [ 'arn:aws:codepipeline:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref AppPipeline ] ]
RoleArn: !GetAtt EventRole.Arn
Id: codepipeline-AppPipeline
- JSON
-
"EventRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"EventPattern": {
"source": [
"aws.codecommit"
],
"detail-type": [
"CodeCommit Repository State Change"
],
"resources": [
{
"Fn::Join": [
"",
[
"arn:aws:codecommit:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "RepositoryName"
}
]
]
}
],
"detail": {
"event": [
"referenceCreated",
"referenceUpdated"
],
"referenceType": [
"branch"
],
"referenceName": [
"main"
]
}
},
"Targets": [
{
"Arn": {
"Fn::Join": [
"",
[
"arn:aws:codepipeline:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Ref": "AppPipeline"
}
]
]
},
"RoleArn": {
"Fn::GetAtt": [
"EventRole",
"Arn"
]
},
"Id": "codepipeline-AppPipeline"
}
]
}
},
-
将更新后的模板保存到本地计算机,然后打开 Amazon CloudFormation
控制台。
-
选择堆栈,然后选择为当前堆栈创建更改集。
-
上传模板,然后查看 Amazon CloudFormation中列出的更改。这些是要对堆栈进行的更改。您应在列表中看到新资源。
-
选择执行。
编辑管道的 PollForSourceChanges参数
许多情况下,当您创建管道时,PollForSourceChanges
参数默认为 true。添加基于事件的更改检测时,必须将参数添加到输出并将其设置为 false 以禁用轮询。否则,您的管道将针对单个源更改启动两次。有关详细信息,请参阅PollForSourceChanges参数的有效设置。