0
0

クロスAWSアカウントでS3オブジェクトにアクセスする

Posted at

やりたいこと

AWSアカウントA:
・S3バケットにVPCエンドポイントしかアクセスできないように制限をかけています
・S3バケットにはRefererアクセス制限をかけています

AWSアカウントB:
EC2などからAWSアカウントAのS3バケットへアクセスしたい

おまかの流れ

AWS記事に書いているとおり
https://repost.aws/ja/knowledge-center/s3-instance-access-bucket

大変なところ

S3バケットのポリシーを書く

この記事にはだいたい書いています
https://tech.nri-net.com/entry/considered_Amazon_s3_bucket_policies_by_scenario

実際にどうやって書くのか

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Deny get requests not through vpc endpoint",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": [
              "${s3_bucket_arn}",
              "${s3_bucket_arn}/*"
            ],
            "Condition": {
                "StringNotLike": {
                    "aws:userid": [
                        "ABxxxxxxxxxxxxN:*",
                        "ACxxxxxxxxxxxxY:*"
                    ]
                },
                "StringNotEquals": {
                    "aws:sourceVpce": "vpce-xxxxxxxx"
                }
            }
        },
        {
            "Sid": "Deny get requests outside *.-hoge.com",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": [
                "${s3_bucket_arn}",
                "${s3_bucket_arn}/*"
            ],
            "Condition": {
                "StringNotLike": {
                    "aws:userid": [
                        "ABxxxxxxxxxxxxN:*",
                        "ACxxxxxxxxxxxxY:*"
                    ],
                    "aws:Referer": [
                        "https://*-hoge.com/*"
                    ]
                }
            }
        },
        {
            "Sid": "Allow cross account access",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::xxxxxx:role/hage-role"
                ]
            },
            "Action": [
                "s3:ListBucket",
                "s3:GetObject"
            ],
            "Resource": [
                "${s3_bucket_arn}",
                "${s3_bucket_arn}/*"
            ]
        }
    ]
}

どうやって情報を埋めるか

aws:userid は以下のコマンドを打って RoleID を転記だけ

aws iam get-role --profile xxxxxxx --role-name yyyyyyy

以上

0
0
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
0
0