本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
為 CodeCommit 來源建立 EventBridge 規則 (AWS CloudFormation 範本)
若要使用 AWS CloudFormation 建立規則,請更新您的範本,如下所示。
更新您的管道 AWS CloudFormation 範本並建立 EventBridge 規則
-
在 範本的 下Resources
,使用 AWS::IAM::Role
AWS CloudFormation 資源來設定 IAM 角色,讓您的事件啟動管道。此項目會建立一個使用兩個政策的角色:
-
第一個政策允許要承擔的角色。
-
第二個政策提供啟動管道的許可。
為什麼我會做出此變更? 新增 AWS::IAM::Role
資源 AWS CloudFormation 可讓 建立 EventBridge 的許可。此資源會新增至您的 AWS 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
AWS CloudFormation 資源來新增 EventBridge 規則。此事件模式會建立事件,以監控推送變更至您的儲存庫。當 EventBridge 偵測到儲存庫狀態變更時,規則StartPipelineExecution
會調用您的目標管道。
為什麼要進行這項變更? 新增 AWS::Events::Rule
資源 AWS CloudFormation 可讓 建立事件。此資源會新增至您的 AWS 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"
}
]
}
},
-
將更新的範本儲存到本機電腦,然後開啟 AWS CloudFormation
主控台。
-
選擇您的堆疊,然後選擇 Create Change Set for Current Stack (建立目前堆疊的變更集)。
-
上傳範本,然後檢視 AWS CloudFormation中所列的變更。這些是會針對堆疊進行的變更。您應該會在清單中看到新資源。
-
選擇 Execute (執行)。
編輯管道的 PollForSourceChanges 參數
在許多情況下,當您建立管道時,PollForSourceChanges
參數會預設為 true。當新增基於事件的變更偵測時,您必須將該參數新增到輸出,並將其設為 false 以停用輪詢。否則,您的管道會針對單一來源變更啟動兩次。如需詳細資訊,請參閱 PollForSourceChanges 參數的有效設定。