

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

# 將不同 AWS 帳戶中的 CodeCommit 儲存庫與筆記本執行個體建立關聯
<a name="nbi-git-cross"></a>

若要將不同 AWS 帳戶中的 CodeCommit 儲存庫與您的筆記本執行個體建立關聯，請設定 CodeCommit 儲存庫的跨帳戶存取權。

**若要為 CodeCommit 儲存庫設定跨帳戶存取權，請將其與筆記本執行個體建立關聯：**

1. 在包含 CodeCommit 儲存庫的 AWS 帳戶中，建立 IAM 政策，允許包含筆記本執行個體之帳戶中的使用者存取儲存庫。如需相關資訊，請參閱[步驟 1：在 AccountA 中建立儲存庫存取權政策](https://docs.aws.amazon.com/codecommit/latest/userguide/cross-account-administrator-a.html#cross-account-create-policy-a)在 *CodeCommit 使用者指南*。

1. 在包含 CodeCommit 儲存庫的 AWS 帳戶中，建立 IAM 角色，並將您在上一個步驟中建立的政策連接到該角色。如需相關資訊，請參閱[步驟 2：在 AccountA 中為儲存庫存取權建立角色](https://docs.aws.amazon.com/codecommit/latest/userguide/cross-account-administrator-a.html#cross-account-create-role-a)，在 *CodeCommit 使用者指南*。

1. 在使用您在上個步驟中建立之角色的筆記本執行個體中建立設定檔：

   1. 開啟筆記本執行個體。

   1. 在筆記本執行個體開啟終端機。

   1. 透過在終端機輸入以下內容來編輯新的設定檔：

      ```
      vi /home/ec2-user/.aws/config
      ```

   1. 使用以下設定檔資訊來編輯檔案：

      ```
      [profile {{CrossAccountAccessProfile}}]
      region = us-west-2
      role_arn = arn:aws:iam::{{CodeCommitAccount}}:role/{{CrossAccountRepositoryContributorRole}}
      credential_source=Ec2InstanceMetadata
      output = json
      ```

      其中 {{CodeCommitAccount}} 是包含 CodeCommit 儲存庫的帳戶，{{CrossAccountAccessProfile}} 是新設定檔的名稱，而 {{CrossAccountRepositoryContributorRole}} 是您在上個步驟建立的角色名稱。

1. 在筆記本執行個體中，將 git 設定為使用您在上個步驟中建立的設定檔：

   1. 開啟筆記本執行個體。

   1. 在筆記本執行個體開啟終端機。

   1. 在終端機中輸入以下內容來編輯 Git 組態檔案：

      ```
      vi /home/ec2-user/.gitconfig
      ```

   1. 使用以下設定檔資訊來編輯檔案：

      ```
      [credential]
              helper = !aws codecommit credential-helper --profile {{CrossAccountAccessProfile}} $@
              UseHttpPath = true
      ```

      其中 {{CrossAccountAccessProfile}} 是您在上個步驟建立的設定檔名稱。