為 Amazon S3 來源建立 EventBridge 規則 (CLI) - AWS CodePipeline

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

為 Amazon S3 來源建立 EventBridge 規則 (CLI)

若要建立 AWS CloudTrail 追蹤並啟用記錄

若要使用 AWS CLI 建立軌跡,請呼叫指create-trail令,並指定:

  • 線索名稱。

  • 您已套用 AWS CloudTrail​ 儲存貯體政策的儲存貯體。

要取得更多資訊,請參閱〈使用 AWS 指令行介面建立系統線〉。

  1. 呼叫 create-trail 命令,並包含 --name--s3-bucket-name 參數。

    為什麼我會做出此變更? 這會建立 S3 來源儲存貯體所需的 CloudTrail追蹤。

    以下命令使用 --name--s3-bucket-name,來建立名為 my-trail 的線索,以及名為 amzn-s3-demo-source-bucket 的儲存貯體。

    aws cloudtrail create-trail --name my-trail --s3-bucket-name amzn-s3-demo-source-bucket
  2. 呼叫 start-logging 命令並加入 --name 參數。

    我為什麼要進行這項變更? 此命令會啟動來源值區的 CloudTrail 記錄,並將事件傳送至 EventBridge。

    範例:

    以下命令範例會使用了 --name,以在名為 my-trail 的線索上啟動日誌記錄。

    aws cloudtrail start-logging --name my-trail
  3. 呼叫 put-event-selectors 命令,並包含 --trail-name--event-selectors 參數。使用事件選取器來指定您希望追蹤記錄來源儲存貯體的資料事件,並將事件傳送至 EventBridge 規則。

    我為什麼要進行這項變更? 此命令會篩選事件。

    範例:

    以下命令範例使用 --trail-name--event-selectors,來指定來源儲存貯體和字首的資料事件,名為 amzn-s3-demo-source-bucket/myFolder

    aws cloudtrail put-event-selectors --trail-name my-trail --event-selectors '[{ "ReadWriteType": "WriteOnly", "IncludeManagementEvents":false, "DataResources": [{ "Type": "AWS::S3::Object", "Values": ["arn:aws:s3:::amzn-s3-demo-source-bucket/myFolder/file.zip"] }] }]'
使用 Amazon S3 做為事件來源和目標建立 EventBridge 規則 CodePipeline ,並套用許可政策
  1. 授與用 EventBridge CodePipeline 於呼叫規則的權限。如需詳細資訊,請參閱在 Amazon EventBridge 使用以資源為基礎的政策

    1. 使用下列範例建立信任原則,以 EventBridge 允許擔任服務角色。將其命名為 trustpolicyforEB.json

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
    2. 使用下列命令來建立 Role-for-MyRule 角色,並連接信任政策。

      為什麼我會做出此變更? 將此信任原則新增至角色會建立的權限 EventBridge。

      aws iam create-role --role-name Role-for-MyRule --assume-role-policy-document file://trustpolicyforEB.json
    3. 針對名為的管線建立權限原則JSON,如下所示MyFirstPipeline。將許可政策命名為 permissionspolicyforEB.json

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "codepipeline:StartPipelineExecution" ], "Resource": [ "arn:aws:codepipeline:us-west-2:80398EXAMPLE:MyFirstPipeline" ] } ] }
    4. 使用執行以下命令,將新的 CodePipeline-Permissions-Policy-for-EB 許可政策連接到您所建立的 Role-for-MyRule 角色。

      aws iam put-role-policy --role-name Role-for-MyRule --policy-name CodePipeline-Permissions-Policy-For-EB --policy-document file://permissionspolicyforEB.json
  2. 呼叫 put-rule 命令,並包含 --name--event-pattern--role-arn 參數。

    以下範例命令會建立名為 MyS3SourceRule 的規則。

    aws events put-rule --name "MyS3SourceRule" --event-pattern "{\"source\":[\"aws.s3\"],\"detail-type\":[\"AWS API Call via CloudTrail\"],\"detail\":{\"eventSource\":[\"s3.amazonaws.com\"],\"eventName\":[\"CopyObject\",\"PutObject\",\"CompleteMultipartUpload\"],\"requestParameters\":{\"bucketName\":[\"amzn-s3-demo-source-bucket\"],\"key\":[\"my-key\"]}}} --role-arn "arn:aws:iam::ACCOUNT_ID:role/Role-for-MyRule"
  3. 若要新增 CodePipeline 為目標,請呼叫put-targets指令並包含--rule--targets參數。

    以下命令指定名為 MyS3SourceRule 的規則,該目標 Id 是由數字 1 組成,指出在規則的目標清單中,這是目標 1。此命令也會指定管道的範例 ARN。儲存庫中若發生變更,管道就會啟動。

    aws events put-targets --rule MyS3SourceRule --targets Id=1,Arn=arn:aws:codepipeline:us-west-2:80398EXAMPLE:TestPipeline
若要編輯管線的 PollForSourceChanges參數
重要

當您使用這個方法建立管道時,如果沒有明確設為 false,則 PollForSourceChanges 參數會預設為 true。當新增基於事件的變更偵測時,您必須將該參數新增到輸出,並將其設為 false 以停用輪詢。否則,您的管道會針對單一來源變更啟動兩次。如需詳細資訊,請參閱 PollForSourceChanges 參數的有效設定

  1. 執行指get-pipeline令,將配管結構複製到JSON檔案中。例如,針對名為 MyFirstPipeline 的管道,執行下列命令:

    aws codepipeline get-pipeline --name MyFirstPipeline >pipeline.json

    此命令不會傳回任何內容,但您建立的檔案應該會顯示在您執行命令的目錄中。

  2. 在任何純文字編輯器中開啟JSON檔案,並amzn-s3-demo-source-bucket將名為值區的PollForSourceChanges參數變更為,以編輯來源階段false,如本範例所示。

    為什麼我會做出此變更? 將此參數設為 false 會關閉定期檢查,因此您只能使用事件型變更偵測。

    "configuration": { "S3Bucket": "amzn-s3-demo-source-bucket", "PollForSourceChanges": "false", "S3ObjectKey": "index.zip" },
  3. 如果使用get-pipeline指令擷取的配管結構,則必須從JSON檔案中移除這些metadata行。否則,update-pipeline 命令無法使用它。移除 "metadata": { } 行,以及 "created""pipelineARN""updated" 欄位。

    例如,從結構中移除下列幾行:

    "metadata": { "pipelineArn": "arn:aws:codepipeline:region:account-ID:pipeline-name", "created": "date", "updated": "date" },

    儲存檔案。

  4. 若要套用變更,請執行指update-pipeline令,並指定管線JSON檔案:

    重要

    請確認在檔案名稱之前包含 file://。這是此命令必要項目。

    aws codepipeline update-pipeline --cli-input-json file://pipeline.json

    此命令會傳回所編輯管道的整個結構。

    注意

    update-pipeline 命令將終止管道。若在您執行 update-pipeline 命令時有修訂正在透過管道執行,該執行將停止。您必須手動啟動管道,以透過更新的管道執行該修訂。使用 start-pipeline-execution 命令來手動啟動您的管道。