1
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.

指定のIAMロールを持ったリソースからしかアクセスできないS3バケットポリシー

Last updated at Posted at 2021-07-21

概要

指定のIAMロールを持ったリソースからしかアクセスできないS3バケットポリシーを記載する

IAMロールIDの準備

バケットポリシーに設定するロールIDはAWS Consoleからは参照出来ないため、AWS CLIで取得する

$ rolename=ロール名
$ aws iam list-roles|jq -r '.Roles[]| select (.RoleName == '\"$rolename\"')|.RoleId'|sed "s/$/:*/g"
AROAxxxxxxxxxxxxxxxxx:*

バケットポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "IPAllow",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::bucketname/*",
            "Condition": {
                "StringNotLike": {
                    "aws:userid": [
                        "AROAxxxxxxxxxxxxxxxxx:*",  #許可するロールのロールID
                        "AROAxxxxxxxxxxxxxxxxx:*"
                    ]
                }
            }
        }
    ]
}

appendix

指定IPもしくは指定ロールからのアクセスを許可する場合のバケットポリシー
参考:指定のIPからしかアクセスできないs3バケットポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "IPAllow",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::bucketname/*",
            "Condition": {
                "StringNotLike": {
                    "aws:userid": [
                        "AROAxxxxxxxxxxxxxxxxx:*",  #許可するロールのロールID
                        "AROAxxxxxxxxxxxxxxxxx:*"
                    ]
                },
                "NotIpAddress": {
                    "aws:SourceIp": [
                        "111.111.111.111/32",       # 許可するIPアドレス
                        "222.222.222.222/32"
                    ]
                }
            }
        }
    ]
}

1
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
1
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?