4
0

AのOrganizationのユーザーからBのOrganizationのS3を叩けるIAMとPolicyの設定

Last updated at Posted at 2024-05-29

概要

  • プロジェクト単位やプロダクト単位でOrganizationを分けている
  • AというOrganizationでユーザーを作り、Bで管理しているS3を操作できるように設定してみた
  • ボツになったけどせっかく設定したのでメモしておく

ざっくり

  • Aでユーザーの用意
  • AユーザーにPolicyを用意してアタッチ
  • BのS3のBucket Policyを設定する

これだけ

手順

Aでユーザーの用意

世の中手順はいっぱいあるので省略

AユーザーにPolicyを用意してアタッチ

  • ${bucket_name} には、BのS3 Bucket名が入る
  • あとはS3使う上で一般的なやつだと思う
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"s3:GetObject",
				"s3:PutObject",
				"s3:DeleteObject"
			],
			"Resource": [
				"arn:aws:s3:::${bucket_name}/*"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"s3:ListBucket"
			],
			"Resource": [
				"arn:aws:s3:::${bucket_name}"
			]
		}
	]
}

BのS3のBucket Policyを設定する

  • ${organization_id} には、AのOrganization IDが入る
  • ${A_iam_user_name} には、AのIAMユーザー名が入る
  • ${bucket_name} には、BのS3 Bucket名が入る
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Hoge",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::${organization_id}:user/${A_iam_user_name}"
            },
			"Action": [
				"s3:GetObject",
				"s3:PutObject",
				"s3:DeleteObject"
			],
            "Resource": "arn:aws:s3:::${bucket_name}/*"
        },
        {
            "Sid": "Hoge2",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::${organization_id}:user/${A_iam_user_name}"
            },
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::${bucket_name}/*"
        }
    ]
}

確認方法

$ aws s3 lsだけだと、AのS3のBucket一覧しかでてこないので、指定してあげる

$ aws s3 ls s3://${bucket_name}

わからなかったこと

Conditionを設定してみたけど、なぜか動かなかったのでよくわからずとりあえず供養(誰か教えて)

BのBucket Policyにこんな設定してみたが、うまくいきませんでした。そもそも必要なのかもよくわからない

// ...
        {
            "Sid": "Statement2",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::${organization_id}:user/${A_iam_user_name}"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::${bucket_name}/*",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "${organization_id}"
                },
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:${service_name}:${region_name}:${organization_id}:*"
                }
            }
        }
// ...
4
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
4
0