

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将一个 AWS CodeCommit 存储库 AWS 账户 与另一个账户中的 Amazon A SageMaker I 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*

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

此模式提供了有关如何将一个账户（账户 A）中的 AWS CodeCommit 存储库与另一个账户 AWS 账户 AWS 账户 （账户 B）中的 Amazon A SageMaker I Studio Classic 关联的说明和代码。要设置关联，您必须在账户 A 中创建 AWS Identity and Access Management (IAM) 策略和角色，在账户 B 中创建 IAM 内联策略。然后，使用外壳脚本将 CodeCommit 存储库从账户 A 克隆到账户 B 中的 Amazon A SageMaker I 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 经典版](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，不适用于亚马 RStudio 逊 SageMaker AI。

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

**技术堆栈**
+ 亚马逊 SageMaker AI
+ 亚马逊 A SageMaker I Studio 经典版
+ AWS CodeCommit
+ AWS Identity and Access Management (IAM) 
+ Git

**目标架构**

下图显示了一种架构，该架构将账户 A CodeCommit 中的存储库与账户 B 中的 SageMaker AI Studio Classic 关联起来。

![跨账户关联架构图](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/d40df9eb-6ee2-4cb8-8257-051fa624e52a/images/abb89a66-fc8f-4e72-8f45-f0f44c2ec6ce.png)


下图显示了如下工作流：

1. 用户通过该`MyCrossAccountRepositoryContributorRole`角色在账户 A 中扮演`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) 服务，可帮助您构建和训练机器学习模型，然后将其部署到生产就绪的托管环境中。
+ [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 policy 和 IAM 角色
<a name="create-an-iam-policy-and-iam-role-in-account-a"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在账户 A 中创建用于存储库访问的 IAM 策略。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/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_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 将内联策略附加到账户 B 中您的 SageMaker 域用户的执行角色。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/associate-an-aws-codecommit-repository-in-one-aws-account-with-sagemaker-studio-in-another-account.html) | AWS DevOps | 

### 在 SageMaker AI Studio 经典版中为账户 B 克隆存储库
<a name="clone-the-repository-in-sm-studio-classic-for-account-b"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在 SageMaker AI Studio Classic 中使用账户 B 创建外壳脚本 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/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_cn/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 外壳脚本示例**

如果您使用此示例脚本，请执行以下操作：
+ 将 `<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>
```