LoginSignup
11
2

More than 3 years have passed since last update.

S3へのEndpointが設定されているVPCでECS(Fargate)を使う場合の注意点

Last updated at Posted at 2020-10-30

はじめに

先日、S3に向けたVPC Endpointが設定されているVPCにてECS(Fargate)を実行しようとした際、少々ハマったので備忘録です。

起こった事象

S3に向けたVPC Endpointが設定されているVPCにてECS(Fargate)を実行しようとしたところ、下記のようなErrorが発生してFargateの起動が失敗した。

CannotPullContainerError: error pulling image configuration: error parsing HTTP 403 response body: invalid character '<' looking for beginning of value: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AccessDenied</Code><Message>Access Denied</Message>.....

どうやら、何らかの理由でECRからのコンテナイメージの取得に失敗している模様。

確認したこと

該当のSubnetからIGW(Internet Gate Way)へのRouteがあることは確認済み。

事象が起こった原因

調べてみると、FargateがECRにあるContainer imageを取得するためにprod-***REGION***-starport-layer-bucketと言うS3 Bucketにアクセスしている模様。これがECRの実体なのか。
参考: https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/vpc-endpoints.html#ecr-setting-up-s3-gateway

解決策

VPC EndpointのPolicyのResourceセクションに、ECRが使用しているS3 BucketのARNを追記。

sample.json
{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "***SID***",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:ListBucket",
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::***BUCKET***",
                "arn:aws:s3:::prod-ap-northeast-1-starport-layer-bucket/*"
            ]
        }
    ]
}

これでFargateを起動すると、無事コンテナイメージが読み込まれて起動が成功した。

最後に

ECRへのアクセスに関連するような設定が何一つ見当たらなかったので、少々手こずった。ことS3に関してはAWSの他のサービスでもバックエンドで利用しているものが多そうだし、特別な理由がない限りVPC EndpointのPolicyを利用してアクセスコントロールを行うのはアンチパターンな気がする。他にも、例えばYumAmazon Linux系のRepositoryを利用する場合にもうまく接続できなかったりする。

11
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
2