0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

アカウントBのEC2にIAMRole+VPCEndpoint構成でアカウントAのS3利用

Last updated at Posted at 2022-01-13

実現させていこと

アカウントAのS3バケットに対して、アカウントBのEC2からアクセスしたい。

構成

準備中

アカウントAのS3バケット

例)
arn:aws:s3:::crossaccount-bucket

アカウントBのIAMロール

例)
arn:aws:iam::012345678912:role/my-ec2-put-s3-crossaccount-bucket-role

アカウントBのEC2

アカウントBのIAMロールを関連付け

アカウントBのVPCにVPC Endpoint

EndpointのタイプはGatewayで、対象サービスはとりあえずS3とする。

Policyの作成

アクセス制御はPolicyで実現させてみる。

アカウントBのIAM Role信頼関係

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

アカウントBのIAM Policy

アカウントBのIAMロールにアクセス権限のポリシーをアタッチする。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:DeleteObject",
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::crossaccount-bucket/*",
                "arn:aws:s3:::crossaccount-bucket"
            ]
        }
    ]
}

アカウントBのVPC Endpoint Policy

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::crossaccount-bucket/*",
                "arn:aws:s3:::crossaccount-bucket"
            ],
            "Condition": {
                "ArnEquals": {
                    "aws:PrincipalArn": "arn:aws:iam::012345678912:role/my-ec2-put-s3-crossaccount-bucket-role"
                }
            }
        }
    ]
}

アカウントAのS3バケット Policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Statement1",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::012345678912:role/my-ec2-put-s3-crossaccount-bucket-role"
            },
            "Action": [
                "s3:DeleteObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::crossaccount-bucket/for_uplad/*",
                "arn:aws:s3:::crossaccount-bucket/for_uplad"
            ]
        },
        {
            "Sid": "Statement2",
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::012345678912:role/my-ec2-put-s3-crossaccount-bucket-role"
            },
            "Action": [
                "s3:DeleteObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::crossaccount-bucket/for_dowmload/*",
                "arn:aws:s3:::crossaccount-bucket/for_dowmload"
            ]
        },
        {
            "Sid": "Statement3",
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::012345678912:role/my-ec2-put-s3-crossaccount-bucket-role"
            },
            "Action": "s3:PutObject",
            "Resource": [
                "arn:aws:s3:::crossaccount-bucket/deny_put/*",
                "arn:aws:s3:::crossaccount-bucket/deny_put"
            ]
        },
        {
            "Sid": "Statement4",
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::012345678912:role/my-ec2-put-s3-crossaccount-bucket-role"
            },
            "Action": "s3:DeleteObject",
            "Resource": [
                "arn:aws:s3:::crossaccount-bucket/deny_delete/*",
                "arn:aws:s3:::crossaccount-bucket/deny_delete"
            ]
        },
        {
            "Sid": "Statement5",
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::012345678912:role/my-ec2-put-s3-crossaccount-bucket-role"
            },
            "Action": "s3:GetObject",
            "Resource": [
                "arn:aws:s3:::crossaccount-bucket/deny_get/*",
                "arn:aws:s3:::crossaccount-bucket/deny_get"
            ]
        }
    ]
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?