

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 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 リポジトリを選択的にレプリケートすることもできます。詳細については、「[Cross-Region replication in Amazon ECR has landed](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>

**前提条件**
+ 2 つ以上のアクティブ AWS アカウント (少なくとも 1 つのソースアカウントと 1 つの送信先アカウント)
+ すべてのアカウントの適切な 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>

**ターゲットスタック**

このパターンには、次の 4 つの主要コンポーネントがあります。
+ **レプリケート元アカウントのインフラストラクチャ – **オーケストレーションコンポーネントを作成する CloudFormation テンプレート
+ **レプリケート先アカウントインフラストラクチャ –** クロスアカウントアクセスロールを作成する CloudFormation テンプレート
+ **Lambda 関数 – **Python ベースの関数であり、Crane を使用してイメージを効率的にコピー
+ **コンテナイメージ – **必要なツールとともに Lambda 関数をパッケージ化する Docker コンテナ

**ターゲットアーキテクチャ**

![\[alt text not found\]](http://docs.aws.amazon.com/ja_jp/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 Notiﬁcation Service (Amazon SNS) 通知

![\[alt text not found\]](http://docs.aws.amazon.com/ja_jp/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 Notiﬁcation Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) は、ウェブサーバーやメールアドレスなど、パブリッシャーとクライアント間のメッセージの交換を調整および管理するのに役立ちます。
+ [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/) は、オペレーティングシステムレベルの仮想化を使用してソフトウェアをコンテナで配信する、Platform as a Service (PaaS) 製品のセットです。

**コードリポジトリ**
+ このパターンのコードは、GitHub 内の [sample-ecr-copy repository](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)」と「[IAM でのセキュリティのベストプラクティス](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>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
|  AWS CLI プロファイルを設定します。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/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/ja_jp/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>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| テンプレートの検証 | 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/ja_jp/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/ja_jp/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>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| コンテナビルドの準備 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/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/ja_jp/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/ja_jp/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/ja_jp/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | データエンジニア、ML エンジニア、DevOps エンジニア | 

### レプリケート元アカウントインフラストラクチャをデプロイする
<a name="deploy-the-source-account-infrastructure"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| デプロイパラメータの準備 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/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/ja_jp/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/ja_jp/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/ja_jp/prescriptive-guidance/latest/patterns/copy-ecr-container-images-across-accounts-regions.html) | データエンジニア、ML エンジニア、DevOps エンジニア | 

### コピープロセスを実行してモニタリングする
<a name="run-and-monitor-the-copy-process"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| コピープロセスを実行してモニタリングする | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/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/ja_jp/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/ja_jp/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/ja_jp/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/ja_jp/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)
+ [What is 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)
+ [What is 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` | E メール通知 | `abc@xyz.com` | 
| `RepositoryList` | コピーするリポジトリ | `repo1,repo2,repo3` | 
| `LambdaImageUri` | Lambda コンテナイメージの URI | `${ACCOUNT}.dkr.ecr.${REGION}.amazonaws.com/ecr-copy-lambda:latest` | 