

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

# 将 Amazon ECR 容器镜像复制到各处 AWS 账户 AWS 区域
<a name="copy-ecr-container-images-across-accounts-regions"></a>

*Faisal Shahdad，Amazon Web Services*

## Summary
<a name="copy-ecr-container-images-across-accounts-regions-summary"></a>

此模式向您展示了如何使用无服务器方法将带标签的图像从现有的 Amazon Elastic Container Registry (Amazon ECR) 存储库复制到其他和。 AWS 账户 AWS 区域该解决方案 AWS Step Functions 用于管理复制工作流程和复制大型容器映像的 AWS Lambda 功能。

Amazon ECR 使用原生[跨区域](https://docs.aws.amazon.com/AmazonECR/latest/userguide/registry-settings-examples.html#registry-settings-examples-crr-single)和[跨账户](https://docs.aws.amazon.com/AmazonECR/latest/userguide/registry-settings-examples.html#registry-settings-examples-crossaccount)复制功能，可以跨区域和账户复制容器映像。但是这些功能只能从开启复制的那一刻起复制映像。目前没有机制能够将现有映像复制到不同区域和账户。

这种模式可以帮助人工智能 (AI) 团队向其他账户和区域分发容器化机器学习 (ML) 模型、框架（例如 PyTorch TensorFlow、和 Hugging Face）以及依赖关系。这可以帮助您克服服务限制并优化 GPU 计算资源。您也可以选择性地从特定来源账户和地区复制 Amazon ECR 存储库。有关更多信息，请参阅 [Amazon ECR 中的跨区域复制已实现](https://aws.amazon.com/blogs/containers/cross-region-replication-in-amazon-ecr-has-landed/)。

## 先决条件和限制
<a name="copy-ecr-container-images-across-accounts-regions-prereqs"></a>

**先决条件**
+ 两个或更多活跃账户 AWS 账户 （至少有一个源账户和一个目标账户）
+ 所有账户都有相应的 AWS Identity and Access Management (IAM) 权限
+ 用于构建 Lambda 容器映像的 Docker
+ AWS Command Line Interface (AWS CLI) 已为所有账户配置

**限制**
+ **未标记的映像排除-** 该解决方案仅复制具有显式标签的容器映像。它会跳过摘要中存在的未加标签的 `SHA256` 映像。
+ **Lambda 执行超时限制 — AWS Lambda 限制**为最长 15 分钟的执行超时，这可能不足以复制大型容器映像或存储库。
+ **手动管理容器映像 —** `crane-app.py` Python 代码需要重新构建和重新部署 Lambda 容器映像。
+ **并行处理容量有限 —** `MaxConcurrency` 状态设置限制了您可以同时复制的存储库数量。但是，您可以在来源账户的 AWS CloudFormation 模板中修改此设置。请注意，较高的并发值可能会导致您超过服务速率限制和账户级 Lambda 执行配额。

## 架构
<a name="copy-ecr-container-images-across-accounts-regions-architecture"></a>

**目标堆栈**

该模式有四个主要组成部分：
+ **源账户基础架构 —** 创建编排组件的 CloudFormation 模板
+ **目标账户基础设施-** 创建跨账户访问角色的 CloudFormation 模板
+ **Lambda 函数 —** 基于 Python 的函数，使用 Crane 进行高效的映像复制
+ **容器映像 – **将 Lambda 函数与所需工具打包在一起的 Docker 容器

**目标架构**

![](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/787185e7-664b-4ed8-b30f-1d9507f13377/images/cc7d9823-3dc8-4090-a203-910b1ac4447c.png)


**Step Function 工作流**

Step Functions 状态机会编排以下内容，如下图所示：
+ `PopulateRepositoryList`**—** 扫描 Amazon ECR 存储库并填充 Amazon DynamoDB
+ `GetRepositoryList`**—** 从 DynamoDB 检索唯一的存储库列表
+ `DeduplicateRepositories`**—** 确保没有重复处理
+ `CopyRepositories`**—** 处理存储库的并行复制
+ `NotifySuccess`/`NotifyFailure`** –** 基于执行结果的 Amazon Simple Notification Service（Amazon SNS）通知

![](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/787185e7-664b-4ed8-b30f-1d9507f13377/images/1b740084-ba2b-4956-aa12-ebbf52be5e7d.png)


## 工具
<a name="copy-ecr-container-images-across-accounts-regions-tools"></a>

**Amazon 工具**
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 可帮助您实时监控您的 AWS 资源和运行的应用程序 AWS 的指标。
+ [Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) 是一项完全托管式 NoSQL 数据库服务，可提供快速、可预测、可扩展的性能。
+ [Amazon Simple Notification Service（Amazon SNS）](https://docs.aws.amazon.com/sns/latest/dg/welcome.html)可帮助您协调和管理发布者与客户端（包括 Web 服务器和电子邮件地址）之间的消息交换。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 通过控制谁经过身份验证并有权使用 AWS 资源，从而帮助您安全地管理对资源的访问权限。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，无需预调配或管理服务器。它只在需要时运行您的代码，并自动进行扩展，因此您只需为使用的计算时间付费。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html)是一项无服务器编排服务，可帮助您组合 Lambda 函数和其他函数 AWS 服务 来构建业务关键型应用程序。

**其他工具**
+ [Crane](https://michaelsauter.github.io/crane/index.html) 是一款 Docker 编排工具。它与 Docker Compose 类似，但还有其他功能。
+ [Docker](https://www.docker.com/) 是一组平台即服务（PaaS）产品，它们使用操作系统级别的虚拟化技术在容器中交付软件。

**代码存储库**
+ 此模式的代码可在 GitHub [sample-ecr-copy 存储库](https://github.com/aws-samples/sample-ecr-copy)中找到。您可以使用存储库中的 CloudFormation 模板来创建底层资源。

## 最佳实践
<a name="copy-ecr-container-images-across-accounts-regions-best-practices"></a>

遵循最低权限原则，并授予执行任务所需的最低权限。有关详情，请参阅 IAM 文档中的[授予最低权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#grant-least-priv)和[安全最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

## 操作说明
<a name="copy-ecr-container-images-across-accounts-regions-epics"></a>

### 准备 环境
<a name="prepare-your-environment"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 配置 AWS CLI 配置文件。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程师、数据工程师、机器学习工程师 | 
| 收集所需信息。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程师、数据工程师、机器学习工程师 | 
| 克隆存储库。 | 将项目的 存储库克隆到本地工作站。<pre>git clone https://github.com/aws-samples/sample-ecr-copy</pre> | DevOps 工程师、数据工程师、机器学习工程师 | 

### 为目标账户部署基础设施
<a name="deploy-infrastructure-for-the-destination-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 验证模版。 | 验证 CloudFormation 模板：<pre>aws cloudformation validate-template \<br />  --template-body file://"Destination Account cf_template.yml" \<br />  --profile destination-account</pre> | DevOps 工程师、机器学习工程师、数据工程师 | 
| 部署目标基础设施。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 数据工程师、机器学习工程师、 DevOps 工程师 | 
| 验证部署。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程师、机器学习工程师、数据工程师 | 

### 构建和部署 Lambda 容器映像
<a name="build-and-deploy-the-lam-container-image"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 准备容器构建。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 数据工程师、机器学习工程师、 DevOps 工程师 | 
| 构建容器映像。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 数据工程师、机器学习工程师、 DevOps 工程师 | 
| 创建存储库并上传映像。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 数据工程师、机器学习工程师、 DevOps 工程师 | 
| Verify the image. | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 数据工程师、机器学习工程师、 DevOps 工程师 | 

### 部署源账户基础设施
<a name="deploy-the-source-account-infrastructure"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 准备部署参数。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 数据工程师、 DevOps 工程师、机器学习工程师 | 
| 验证源模版。 | 验证源 CloudFormation 模板：<pre>aws cloudformation validate-template \<br />  --template-body file://"Source Account Cf template.yml" \<br />  --profile source-account</pre> | 数据工程师、机器学习工程师、 DevOps 工程师 | 
| 部署源基础设施。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 数据工程师、机器学习工程师、 DevOps 工程师 | 
| 验证部署并收集输出。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程师、机器学习工程师、数据工程师 | 
| 确认电子邮件订阅。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 数据工程师、机器学习工程师、 DevOps 工程师 | 

### 运行并监控复制过程
<a name="run-and-monitor-the-copy-process"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 运行并监控复制过程 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程师、机器学习工程师、数据工程师 | 
| 运行 Step Function。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程师、机器学习工程师、数据工程师 | 
| 监控进度。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程师、机器学习工程师、数据工程师 | 
| 检查结果。 | 等待该过程完成（每 30 秒更新一次）：<pre>while true; do<br />  STATUS=$(aws stepfunctions describe-execution \<br />    --execution-arn $EXECUTION_ARN \<br />    --profile source-account \<br />    --region $SOURCE_REGION \<br />    --query 'status' \<br />    --output text)<br />  <br />  echo "Current status: $STATUS"<br />  <br />  if [[ "$STATUS" == "SUCCEEDED" || "$STATUS" == "FAILED" || "$STATUS" == "TIMED_OUT" || "$STATUS" == "ABORTED" ]]; then<br />    break<br />  fi<br />  <br />  sleep 30<br />done<br /><br />echo "Final execution status: $STATUS"</pre> | DevOps 工程师、机器学习工程师、数据工程师 | 
| 验证映像。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程师、数据工程师、机器学习工程师 | 

## 问题排查
<a name="copy-ecr-container-images-across-accounts-regions-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| Step Function 无法运行。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 

## 相关资源
<a name="copy-ecr-container-images-across-accounts-regions-resources"></a>
+ [Crane 文档](https://github.com/google/go-containerregistry/blob/main/cmd/crane/doc/crane.md)
+ [什么是 Amazon 弹性容器注册表？](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html)
+ [什么是 AWS Lambda？](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)
+ [什么是 Step Functions？](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html)

## 附加信息
<a name="copy-ecr-container-images-across-accounts-regions-additional"></a>

**配置参数**


| 
| 
| 参数 | 说明 | 示例 | 
| --- |--- |--- |
| `SourceAccountId` | 来源 AWS 账户 ID | `11111111111` | 
| `DestinationAccountId` | 目的地 AWS 账户 ID | `22222222222` | 
| `DestinationRegion` | 目标 AWS 区域 | `us-east-2` | 
| `SourceRegion` | 来源 AWS 区域 | `us-east-1` | 
| `NotificationEmail` | 电子邮件通知 | `abc@xyz.com` | 
| `RepositoryList` | 要复制的存储库 | `repo1,repo2,repo3` | 
| `LambdaImageUri` | Lambda 容器映像 | `${ACCOUNT}.dkr.ecr.${REGION}.amazonaws.com/ecr-copy-lambda:latest` | 