18
21

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 5 years have passed since last update.

特定の IAM Role を持った別アカウントの EC2 インスタンスから S3 へのアクセスを許可する Bucket Policy

Last updated at Posted at 2014-05-14

タイトル長すぎ!

IAM ロールを使用したクロスアカウント API アクセスのウォークスルー - AWS Identity and Access Management を読んでみたんだけど、やたら面倒だったり、若干やりたいこととずれてたりしたので、bucket policy でなんとかしてみた。

+--AWS Account A------+              +---AWS Account B----+
|    EC2 Instance   --+------------+----> S3 Bucket      |
|     (IAM Role)      |            |   (Bucket Policy)  |
+---------------------+              +-------------------+
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Principal": {
				"AWS": "arn:aws:iam::XXXXXXXXXXXX:role/SomeRole"
			},
			"Action": "s3:ListBucket",
			"Resource": [
				"arn:aws:s3:::bucket-name"
			]
		},
		{
			"Effect": "Allow",
			"Principal": {
				"AWS": "arn:aws:iam::XXXXXXXXXXXX:role/SomeRole"
			},
			"Action": [
				"s3:GetObject",
				"s3:PutObject",
				"s3:DeleteObject"
			],
			"Resource": [
				"arn:aws:s3:::bucket-name/*"
			]
		}
	]
}

Principal には、アクセスする側(アクセスを許可される側、AWS Account A)の Role の ARN を指定する。

iam.png

アクセスする側の Role に対しては、S3 へのアクセスを許可する Policy を書いてあげる。おおざっぱに書くならこんな感じ。バケット指定さぼってます。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:ListBucket", "s3:GetObject"
      ],
      "Resource": [
        "arn:aws:s3:::*"
      ]
    }
  ]
}

これで別アカウントの EC2 インスタンスから aws s3 コマンドでもにょもにょできた。

参考:

18
21
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
18
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?