

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

# 跨 AWS 帳戶 和 複製 Amazon ECR 容器映像 AWS 區域
<a name="copy-ecr-container-images-across-accounts-regions"></a>

*Faisal Shahdad，Amazon Web Services*

## 總結
<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 執行配額。

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

**目標堆疊**

模式有四個主要元件：
+ **來源帳戶基礎設施 –** 建立協同運作元件的 CloudFormation 範本
+ **目的地帳戶基礎設施 –** 建立跨帳戶存取角色的 CloudFormation 範本
+ **Lambda 函數 –** 使用 Crane 進行高效率映像複製的 Python 函數
+ **容器映像 –** 使用必要工具封裝 Lambda 函數的 Docker 容器

**目標架構**

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


**Step Functions 工作流程**

Step Functions 狀態機器會協調下列項目，如下圖所示：
+ `PopulateRepositoryList`** –** 掃描 Amazon ECR 儲存庫並填入 Amazon DynamoDB
+ `GetRepositoryList`** –** 從 DynamoDB 擷取唯一儲存庫清單
+ `DeduplicateRepositories`** –** 確保沒有重複的處理
+ `CopyRepositories`** –** 處理儲存庫的平行複製
+ `NotifySuccess`/`NotifyFailure`** –** 根據執行結果的 Amazon Simple Notification Service (Amazon SNS) 通知

![\[alt text not found\]](http://docs.aws.amazon.com/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 AWS CLI 設定檔。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程師、資料工程師、ML 工程師 | 
| 收集必要資訊。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程師、資料工程師、ML 工程師 | 
| 複製儲存庫。 | 將模式的儲存庫複製到本機工作站：<pre>git clone https://github.com/aws-samples/sample-ecr-copy</pre> | DevOps 工程師、資料工程師、ML 工程師 | 

### 部署目的地帳戶的基礎設施
<a name="deploy-infrastructure-for-the-destination-account"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證範本。 | 驗證 CloudFormation 範本：<pre>aws cloudformation validate-template \<br />  --template-body file://"Destination Account cf_template.yml" \<br />  --profile destination-account</pre> | DevOps 工程師、ML 工程師、資料工程師 | 
| 部署目的地基礎設施。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 資料工程師、ML 工程師、DevOps 工程師 | 
| 驗證部署。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程師、ML 工程師、資料工程師 | 

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


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 準備容器建置。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 資料工程師、ML 工程師、DevOps 工程師 | 
| 建置容器映像。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 資料工程師、ML 工程師、DevOps 工程師 | 
| 建立儲存庫並上傳映像。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 資料工程師、ML 工程師、DevOps 工程師 | 
| 驗證映像。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 資料工程師、ML 工程師、DevOps 工程師 | 

### 部署來源帳戶基礎設施
<a name="deploy-the-source-account-infrastructure"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 準備部署參數。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 資料工程師、DevOps 工程師、ML 工程師 | 
| 驗證來源範本。 | 驗證來源 CloudFormation 範本：<pre>aws cloudformation validate-template \<br />  --template-body file://"Source Account Cf template.yml" \<br />  --profile source-account</pre> | 資料工程師、ML 工程師、DevOps 工程師 | 
| 部署來源基礎設施。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 資料工程師、ML 工程師、DevOps 工程師 | 
| 驗證部署並收集輸出。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程師、ML 工程師、資料工程師 | 
| 確認您的電子郵件訂閱。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | 資料工程師、ML 工程師、DevOps 工程師 | 

### 執行和監控複製程序
<a name="run-and-monitor-the-copy-process"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行並監控複製程序。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程師、ML 工程師、資料工程師 | 
| 執行步驟函數。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程師、ML 工程師、資料工程師 | 
| 監控進度。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程師、ML 工程師、資料工程師 | 
| 檢查結果。 | 等待程序完成 （每 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 工程師、ML 工程師、資料工程師 | 
| 驗證映像。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | DevOps 工程師、資料工程師、ML 工程師 | 

## 疑難排解
<a name="copy-ecr-container-images-across-accounts-regions-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 步驟函數無法執行。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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 Elastic Container Registry？](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 容器映像 URI | `${ACCOUNT}.dkr.ecr.${REGION}.amazonaws.com/ecr-copy-lambda:latest` | 