はじめに
閉域網内に、ECS + ECRの構成では、以下の通り、VPC Endpointの作成が必要です。
- Interface Endpoint
- ecr.api
- ecr.dkr
- S3 Gateway Endpoint
なぜS3 Gateway Endpointが必要なのかを調べてみました。
For your Amazon ECS tasks to pull private images from Amazon ECR, you must create a gateway endpoint for Amazon S3.
The gateway endpoint is required because Amazon ECR uses Amazon S3 to store your image layers.
When your containers download images from Amazon ECR, they must access Amazon ECR to get the image manifest and then Amazon S3 to download the actual image layers.
要するに、ECSは、S3から実際のimage layersを取得するため、ECS⇒S3が必要です。
リソース
S3 Gateway Endpointを利用できない場合
ECSからS3 Gateway Endpointを利用できない場合、CannotPullContainerErrorが発生します。
CannotPullContainerError
route設定なしにより、ECS task起動エラーが発生しました。
CannotPullContainerError:
The task cannot pull 123456789000.dkr.ecr.ap-northeast-1.amazonaws.com/test:NviE_Ss@sha256:6ecf45eff21e095ad8d60b724a6bbcax9df49da3b39fe80237ad7d688723c8706c3c from the registry 123456789000.dkr.ecr.ap-northeast-1.amazonaws.com/test:NviE_Ss@sha256:6ecf45eff21e095ad8d60b724a6bbcax9df49da3b39fe80237ad7d688723c8706c3c.
There is a connection issue between the task and the registry.
Check your task network configuration. : failed to copy: httpReadSeeker: failed open: failed to do request: Get 123456789000.dkr.ecr.ap-northeast-1.amazonaws.com/test:NviE_Ss@sha256:6ecf45eff21e095ad8d60b724a6bbcax9df49da3b39fe80237ad7d688723c8706c3c: dial tcp 52.219.172.118:443: i/o timeout
52.219.172.118はS3関連のEndpointみたいです。
nslookup 52.219.172.118
名前: s3-ap-northeast-1-r-w.amazonaws.com
Address: 52.219.172.118
要するに、S3に接続できてず、imageを取得できなかったです。
S3 Gateway Endpointを利用できる場合
ECS task起動できた
NACL設定
NACLがStateLessであることを忘れてはいけないです。
以下のように、ECS subnetに付与されるNACLのOutboundにはALL Allowになっているが、Inbound ruleには、VPC内部の通信のみが許可されています。
そうすると、ECS⇒S3はOKで、戻り通信S3⇒ECSはblockされるため、同様に CannotPullContainerError が発生します。