

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

# 步驟 5：在 CodePipeline 中建立管道
<a name="other-services-cp-chef12-pipeline"></a>

**重要**  
 AWS OpsWorks Stacks 此服務已於 2024 年 5 月 26 日終止，並已針對新客戶和現有客戶停用。我們強烈建議客戶盡快將其工作負載遷移至其他解決方案。如果您對遷移有任何疑問，請透過 [AWS re：Post](https://repost.aws/) 或透過 [AWS Premium Support](https://aws.amazon.com/support) 聯絡 AWS 支援 團隊。

在 Stacks 中設定了具有 layer OpsWorks 和至少一個執行個體的堆疊之後，請使用 OpsWorks Stacks 在 CodePipeline 中建立管道，以將應用程式或 Chef 技術指南部署到您的 OpsWorks Stacks 資源。

**建立管道**

1. 前往 [https://console.aws.amazon.com/codepipeline/](https://console.aws.amazon.com/codepipeline/) 開啟 CodePipeline 主控台。

1. 選擇 **Create pipeline (建立管道)**。

1. 在 ** CodePipeline 入門**頁面上，輸入 **MyOpsWorksPipeline**或您的帳戶唯一的任何其他管道名稱，然後選擇**下一步**。

1. 在 **Source Location (來源位置)** 頁面上，從 **Source provider (來源提供者)** 下拉式清單選取 **Amazon S3**。

1. 在 **Amazon S3 詳細資訊**區域中，以 格式輸入您的 Amazon S3 儲存貯體路徑**s3://*bucket-name*/*file name***。參考您在[步驟 3：將應用程式碼上傳至 Amazon S3 儲存貯體](other-services-cp-chef12-s3.md)的步驟 9 中記下的連結。在本演練中，路徑為 `s3://my-appbucket/opsworks-nodejs-demo-app.zip`。選擇 **下一個步驟**。  
![\[AWS CodePipeline 來源和提供者\]](http://docs.aws.amazon.com/zh_tw/opsworks/latest/userguide/images/cp_integ_source12.png)

1. 在 **Build (組建)** 頁面上，從下拉式清單選擇 **No Build (無組建)**，然後選擇 **Next step (下一步)**。

1. 在 **Deploy (部署)** 頁面上，選擇 **OpsWorks Stacks** 做為部署提供者。  
![\[Deploy configuration form for AWS OpsWorks Stacks with fields for stack, layer, and app selection.\]](http://docs.aws.amazon.com/zh_tw/opsworks/latest/userguide/images/cp_integ_cpprovider12.png)

1. 在 **Stack (堆疊)** 欄位中輸入 `CodePipelineDemo`，或您在[步驟 1：在 Stacks 中建立堆疊、 layer OpsWorks 和執行個體](other-services-cp-chef12-stack.md)中建立的堆疊名稱。

1. 在 **Layer** 欄位中輸入 `Node.js App Server`，或您在[步驟 1：在 Stacks 中建立堆疊、 layer OpsWorks 和執行個體](other-services-cp-chef12-stack.md)中建立的 layer 名稱。

1. 在**應用程式**欄位中，選取您在 中上傳到 Amazon S3 的應用程式[步驟 3：將應用程式碼上傳至 Amazon S3 儲存貯體](other-services-cp-chef12-s3.md)，然後選擇**下一步**。

1. 在**AWS 服務角色**頁面上，選擇**建立角色**。

   新的視窗隨即開啟，其中包含 IAM 主控台頁面，說明將為您建立的角色：`AWS-CodePipeline-Service`。從 **Policy name (政策名稱)** 下拉式清單，選擇 **Create new policy (建立新政策)**。請確認政策文件包含下列內容。如有需要，請選擇 **Edit (編輯)** 來變更政策文件。

   ```
   {
       "Statement": [
           {
               "Action": [
                   "s3:GetObject",
                   "s3:GetObjectVersion",
                   "s3:GetBucketVersioning"
               ],
               "Resource": "*",
               "Effect": "Allow"
           },
           {
               "Action": "opsworks:*",
               "Resource": "*",
               "Effect": "Allow"
           }
       ]
   }
   ```

   完成政策文件的變更後，選擇 **Allow (允許)**。您的變更會顯示在 IAM 主控台中。  
![\[IAM role summary with AWS-CodePipeline-Service role and policy document editor.\]](http://docs.aws.amazon.com/zh_tw/opsworks/latest/userguide/images/cp_integ_iamrole.png)
**注意**  
如果角色建立失敗，可能是因為您已經有名為 **AWS-CodePipeline-Service** 的 IAM 角色。如果您在 2016 年 5 月之前已使用 **AWS-CodePipeline-Service** 角色，該角色可能沒有使用 OpsWorks Stacks 做為部署提供者的許可。在本例中，您必須更新此政策陳述式，如本步驟中所示。若您看到錯誤訊息，請回到此步驟的開頭，並選擇 **Use existing role (使用現有的角色)** 而非 **Create role (建立角色)**。如果您使用現有的角色，該角色應會連接含有此步驟中所示許可的政策。如需服務角色及其政策陳述式的詳細資訊，請參閱[編輯 IAM 服務角色的政策](https://docs.aws.amazon.com/codepipeline/latest/userguide/access-permissions.html#how-to-custom-role)。

1. 如果角色建立程序成功，IAM 頁面會關閉，而且您會返回**AWS 服務角色**頁面。選擇 **下一個步驟**。

1. 在 **Review your pipeline (檢閱管道)** 頁面上，確認頁面顯示的選項，然後選擇 **Create pipeline (建立管道)**。

1. 當管道就緒時，其應會開始尋找來源碼，並自動將應用程式部署到堆疊。此程序需要幾分鐘的時間。