| 方法 | 説明 |
|---|---|
| バケットポリシーで制御する方法 | AWS CLIでアクセスする際にprofile指定が不要 |
| 接続先のIAMユーザのクレデンシャルを利用する方法 | |
| スイッチロール(sts:AssumeRole)を利用する方法 |
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetObject",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::接続元アカウント:root"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::bucketname/*"
},
{
"Sid": "ListBucket",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::接続元アカウント:root"
},
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::bucketname"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetObject",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::接続元アカウント:role/xxxxxxxxxx-ec2",
"arn:aws:iam::接続元アカウント:role/xxxxxxxxxx-ec2",
]
},
"Action": [
"s3:ListgBucket",
"s3:GetObject",
"s3:DeleteObject",
]
"Resource": [
"arn:aws:s3:::bucketname/XXX/*"
"arn:aws:s3:::bucketname"
]
}
]
}
AWS CLI やプログラムによるアクセスが想定されたもの
テストなら、AmazonS3FullAccessでもよい
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListAllMyBuckets",
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation"
],
"Resource": "arn:aws:s3:::your-bucketname"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::your-bucketname/*"
}
]
}
接続先アカウントで、IAM ユーザ作成し、作成したS3へのアクセスを許可するポリシーを割り当てる
作成されるアクセスキーとシークレットキーを接続元に渡す。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Example permissions",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::接続元:root"
},
"Action": [
"s3:GetLifecycleConfiguration",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::s3-crossaccount-bucket01"
]
}
]
}
[profile crossaccount] aws_access_key_id = access-key aws_secret_access_key = secret-access-key region = ap-northeast-1
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::xxxxxxxxxxxxx:user/cliuser"
},
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::s3-crossaccount-bucket01",
"arn:aws:s3:::s3-crossaccount-bucket01/*"
]
}
]
}
aws s3 ls s3://s3-crossaccount-bucket01 --profile crossaccount
テストなら、AmazonS3FullAccessでもよい
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListAllMyBuckets",
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation"
],
"Resource": "arn:aws:s3:::your-bucketname"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::your-bucketname/*"
}
]
}
以下の信頼関係のロールができます。
“arn:aws:iam::<スイッチ元アカウントのAWSアカウントID>:root”は、ユーザを限定しないで、すべてのユーザを許可する設定
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<スイッチ元アカウントのAWSアカウントID>:root"
},
"Action": "sts:AssumeRole",
"Condition": {}
}
]
}
{
"Version": "2012-10-17",
"Id": "Policy1415115909152",
"Statement": [
{
"Sid": "Access-to-specific-VPCE-only",
"Principal": "*",
"Action": "s3:*",
"Effect": "Deny",
"Resource": ["arn:aws:s3:::S3バケット名",
"arn:aws:s3:::S3バケット名/*"],
"Condition": {
"StringNotEquals": {
"aws:SourceVpce": "VPCエンドポイントID"
}
}
}
]
}
接続元でのS3のポリシーは不要。
スイッチロールした先に割り与えられたポリシーになるため。
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::[接続先のAWSアカウントID]:role/RoleA"
}
}
./aws/credentials
[role-a-profile] role_arn = arn:aws:iam::スイッチ先アカウントID:role/Role-A credential_source=Ec2InstanceMetadata
aws s3 ls s3://{バケット名}/ --profile role-a-profile
[profile default] region = ap-northeast-1 output = json [profile test] role_arn = arn:aws:iam::スイッチ先アカウントID:role/TestSwitchRole source_profile = default
aws s3 ls --profile test
#aws sts assume-roleコマンドでアカウントBのAccountB_roleのアクセスキー・シークレットアクセスキーなどのクレデンシャル情報を表示し、 #変数role_credentialsに格納する role_credentials=$(aws sts assume-role \ --role-arn "arn:aws:iam::アカウントBのアカウントID:role/AccountB_role" \ --role-session-name session-name) #AccountB_roleのアクセスキーを環境変数AWS_ACCESS_KEY_IDに格納する export AWS_ACCESS_KEY_ID=$(echo $role_credentials | jq -r '.Credentials.AccessKeyId') #AccountB_roleのシークレットアクセスキーを環境変数AWS_SECRET_ACCESS_KEYに格納する export AWS_SECRET_ACCESS_KEY=$(echo $role_credentials | jq -r '.Credentials.SecretAccessKey') #セッショントークンを環境変数AWS_SESSION_TOKENに格納する export AWS_SESSION_TOKEN=$(echo $role_credentials | jq -r '.Credentials.SessionToken') #現在の権限(スイッチしたロールの情報)を確認する aws sts get-caller-identity
aws s3 ls
#環境変数の削除 unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN #現在の権限(スイッチしたロールが何か)を確認する aws sts get-caller-identity
Amazon S3は、容量無制限の安価なオブジェクトストレージ
一般向けサイト
ITエンジニア向けサイト
英語サイト
Portfolio
Copyright (c) 2025 クラウドのインフラ技術 All Rights Reserved.