

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

# 將儲存 AWS CodeCommit 庫與另一個帳戶中 AWS 帳戶 的 Amazon SageMaker AI Studio Classic 建立關聯
<a name="associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account"></a>

*Laurens van der Maas 和 Aubrey Oosthuizen，Amazon Web Services*

## 總結
<a name="associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account-summary"></a>

此模式提供如何將一個 AWS 帳戶 （帳戶 A) 中的 AWS CodeCommit 儲存庫與另一個 AWS 帳戶 （帳戶 B) 中的 Amazon SageMaker AI Studio Classic 建立關聯的說明和程式碼。若要設定關聯，您必須在帳戶 A 中建立 AWS Identity and Access Management (IAM) 政策和角色，並在帳戶 B 中建立 IAM 內嵌政策。然後，您可以使用 shell 指令碼，將 CodeCommit 儲存庫從帳戶 A 複製到帳戶 B 中的 Amazon SageMaker AI Classic。

## 先決條件和限制
<a name="associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account-prereqs"></a>

**先決條件 **
+ 兩個 [AWS 帳戶](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/)，一個包含 CodeCommit 儲存庫，另一個包含具有使用者的 SageMaker AI 網域
+ 佈建的 [SageMaker AI 網域和使用者](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-studio-onboard.html)，可透過網際網路存取或透過虛擬私有網路 AWS Security Token Service (VPC AWS STS) 端點存取 CodeCommit 和 ()
+ 對 [IAM](https://docs.aws.amazon.com/iam/?id=docs_gateway) 的基本了解
+ 對 [SageMaker AI Studio Classic](https://docs.aws.amazon.com/sagemaker/latest/dg/studio.html) 的基本了解
+ 對 [Git](https://git-scm.com/) 和 [CodeCommit ](https://docs.aws.amazon.com/codecommit/index.html)的基本了解

**限制 **

此模式僅適用於 SageMaker AI Studio Classic，不適用於 Amazon SageMaker AI 上的 RStudio。

## Architecture
<a name="associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account-architecture"></a>

**技術堆疊**
+ Amazon SageMaker AI
+ Amazon SageMaker AI Studio Classic
+ AWS CodeCommit
+ AWS Identity and Access Management (IAM) 
+ Git

**目標架構**

下圖顯示將 CodeCommit 儲存庫從帳戶 A 與帳戶 B 中的 SageMaker AI Studio Classic 建立關聯的架構。

![跨帳戶關聯的架構圖](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/d40df9eb-6ee2-4cb8-8257-051fa624e52a/images/abb89a66-fc8f-4e72-8f45-f0f44c2ec6ce.png)


該圖顯示以下工作流程：

1. 使用者透過角色擔任帳戶 A 中`MyCrossAccountRepositoryContributorRole`的角色`sts:AssumeRole`，同時使用帳戶 B 中 SageMaker AI Studio Classic 中的 SageMaker AI 執行角色。擔任的角色包含 CodeCommit 許可，可複製並與指定的儲存庫互動。

1. 使用者從 SageMaker AI Studio Classic 中的系統終端機執行 Git 命令。

**自動化和擴展**

此模式包含可使用 [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/?id=docs_gateway)、 [AWS CloudFormation](https://docs.aws.amazon.com/cloudformation/?id=docs_gateway)或 [Terraform](https://www.terraform.io/) 自動化的手動步驟。

## 工具
<a name="associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account-tools"></a>

**AWS 工具**
+ [Amazon SageMaker AI](https://docs.aws.amazon.com/sagemaker/?id=docs_gateway) 是一種受管機器學習 (ML) 服務，可協助您建置和訓練 ML 模型，然後將模型部署到生產就緒的託管環境中。
+ [Amazon SageMaker AI Studio Classic](https://docs.aws.amazon.com/sagemaker/latest/dg/studio.html) 是適用於機器學習的 Web 型整合開發環境 (IDE)，可讓您建置、訓練、偵錯、部署和監控機器學習模型。
+ [AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html) 是一種版本控制服務，可協助您私下存放和管理 Git 儲存庫，而無需管理您自己的來源控制系統。

  **注意**： AWS CodeCommit 不再提供給新客戶。的現有客戶 AWS CodeCommit 可以繼續正常使用服務。[進一步了解](https://aws.amazon.com/blogs/devops/how-to-migrate-your-aws-codecommit-repository-to-another-git-provider/)
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。

**其他工具**
+ [Git](https://git-scm.com/) 是一種分散式版本控制系統，可在軟體開發期間追蹤原始程式碼的變更。

## 史詩
<a name="associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account-epics"></a>

### 在帳戶 A 中建立 IAM 政策和 IAM 角色
<a name="create-an-iam-policy-and-iam-role-in-account-a"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在帳戶 A 中建立儲存庫存取的 IAM 政策。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account.html)最佳實務是將 IAM 政策的範圍限制為使用案例所需的最低許可。 | AWS DevOps | 
| 在帳戶 A 中建立儲存庫存取的 IAM 角色。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account.html) | AWS DevOps | 

### 在帳戶 B 中建立 IAM 內嵌政策
<a name="create-an-iam-inline-policy-in-account-b"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 將內嵌政策連接至帳戶 B 中連接至 SageMaker 網域使用者的執行角色。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account.html) | AWS DevOps | 

### 在帳戶 B 的 SageMaker AI Studio Classic 中複製儲存庫
<a name="clone-the-repository-in-sm-studio-classic-for-account-b"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在帳戶 B 的 SageMaker AI Studio Classic 中建立 shell 指令碼。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account.html) | AWS DevOps | 
| 從系統終端機叫用 shell 指令碼。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account.html)您已在 SageMaker AI Studio 跨帳戶中複製 CodeCommit 儲存庫。您現在可以從系統終端機執行所有 Git 命令。 | AWS DevOps | 

## 其他資訊
<a name="associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account-additional"></a>

**IAM 政策範例**

如果您使用此範例政策，請執行下列動作：
+ `<CodeCommit_Repository_Region>` 將 取代 AWS 區域 為 儲存庫的 。
+ `<Account_A_ID>` 將 取代為帳戶 A 的帳戶 ID。
+ `<CodeCommit_Repository_Name>` 將 取代為帳戶 A 中 CodeCommit 儲存庫的名稱。

```
{
"Version": "2012-10-17",		 	 	 
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "codecommit:BatchGet*",
            "codecommit:Create*",
            "codecommit:DeleteBranch",
            "codecommit:Get*",
            "codecommit:List*",
            "codecommit:Describe*",
            "codecommit:Put*",
            "codecommit:Post*",
            "codecommit:Merge*",
            "codecommit:Test*",
            "codecommit:Update*",
            "codecommit:GitPull",
            "codecommit:GitPush"
        ],
        "Resource": [
            "arn:aws:codecommit:<CodeCommit_Repository_Region>:<Account_A_ID>:<CodeCommit_Repository_Name>"
        ]
    }
]
}
```

**SageMaker AI shell 指令碼範例**

如果您使用此範例指令碼，請執行下列動作：
+ `<Account_A_ID>`將 取代為帳戶 A 的帳戶 ID。
+ `<Account_A_Role_Name>`將 取代為您先前建立的 IAM 角色名稱。
+ `<CodeCommit_Repository_Region>`將 取代 AWS 區域 為 儲存庫的 。
+ `<CodeCommit_Repository_Name>`將 取代為帳戶 A 中 CodeCommit 儲存庫的名稱。

```
#!/usr/bin/env bash
#Launch from system terminal
pip install --quiet git-remote-codecommit

mkdir -p ~/.aws
touch ~/.aws/config

echo "[profile CrossAccountAccessProfile]
region = <CodeCommit_Repository_Region>
credential_source=EcsContainer
role_arn = arn:aws:iam::<Account_A_ID>:role/<Account_A_Role_Name>
output = json" > ~/.aws/config

echo '[credential "https://git-codecommit.<CodeCommit_Repository_Region>.amazonaws.com"]
        helper = !aws codecommit credential-helper $@ --profile CrossAccountAccessProfile
        UseHttpPath = true' > ~/.gitconfig
        
git clone codecommit::<CodeCommit_Repository_Region>://CrossAccountAccessProfile@<CodeCommit_Repository_Name>
```