LoginSignup
2
4

More than 5 years have passed since last update.

AWS+Reactアプリ作成入門(IAM Role編)

Last updated at Posted at 2017-11-09

AWS+Reactアプリ作成入門(Cognito編)
AWS+Reactアプリ作成入門(S3編)
AWS+Reactアプリ作成入門(DynamoDB編)
AWS+Reactアプリ作成入門(IAM Role編)
AWS+Reactアプリ作成入門(ログイン後のAdmin編)

今回作成したアプリ ==>久喜SNS

 AWSのIAMサービスにRoleというものがあります。Roleは、AWSサービスのリソースへのアクセスを制御します。今回のアプリの例で言えば、Conginitoサービスを使っていますので、そのCognitoアプリがどのリソースへアクセスできるかをRoleによって決められています。CognitoアプリのRoleは、非ログインユーザとログインユーザで別のものが用意されていますので、ログイン状態によってユーザはアクセス権限が異なります。ちなみにRoleはPolicyのセットとして定義されます。実際に制御の記述はPolicyに記載します。つまりあるPolicyが複数のRoleに割り当てられることが可能です。但しこの記事の中ではRoleとPolicyを混同して使うことがありますが、文脈から読み取っていただければ幸いです。以下に今回のアプリで使われているRoleを説明していきます。

 ※未確認ですが、以下の複数のRoleでs3:GetObjectをAllowしていますが、バケットポリシーで既にAllowしているので不要かもしれません。

1.Cognito_sandAppIDPoolUnauth_Role

 このRoleは「AWS+Reactアプリ作成入門(Cognito編」でsandAppIDPoolというフェデレーテッドアイデンティティを作成した時に指定したものです。ログイン前のCognitoアプリユーザの権限です。非ログインユーザがどのような権限でS3やDynamoDBのリソースにアクセスできるかを記述しています。

Cognito_sandAppIDPoolUnauth_Role
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "mobileanalytics:PutEvents",
                "cognito-sync:*"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::バケット名/contents/cache.json"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:Query",
                "dynamodb:Update",
                "dynamodb:UpdateItem"
            ],
            "Resource": [
                "arn:aws:dynamodb:ap-northeast-1:xxxxxx:table/掲示板テーブル名",
                "arn:aws:dynamodb:ap-northeast-1:xxxxxx:table/掲示板テーブル名/index/partitionYear-uploadTime-index"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:Query",
                "dynamodb:Put",
                "dynamodb:PutItem"
            ],
            "Resource": [
                "arn:aws:dynamodb:ap-northeast-1:xxxxxx:table/コメントテーブル名"
            ]
        }
    ]
}

2.Cognito_sandAppIDPoolAuth_Role

 このRoleも「AWS+Reactアプリ作成入門(Cognito編」でsandAppIDPoolというフェデレーテッドアイデンティティを作成した時に指定したものです。ログイン後のCognitoアプリユーザの権限です。ログインユーザがどのような権限でS3やDynamoDBのリソースにアクセスできるかを記述しています。

Cognito_sandAppIDPoolAuth_Role
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "mobileanalytics:PutEvents",
                "cognito-sync:*",
                "cognito-identity:*"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::バケット名/contents/images/*",
                "arn:aws:s3:::バケット名/contents/thumbnail/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::バケット名/contents/cache.json"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:PutItem",
                "dynamodb:Put",
                "dynamodb:DeleteItem",
                "dynamodb:Update",
                "dynamodb:UpdateItem",
                "dynamodb:Query"
            ],
            "Resource": [
                "arn:aws:dynamodb:ap-northeast-1:xxxxxx:table/掲示板テーブル",
                "arn:aws:dynamodb:ap-northeast-1:xxxxxx:table/掲示板テーブル/index/partitionYear-uploadTime-index"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:Query",
                "dynamodb:DeleteItem",
                "dynamodb:Put",
                "dynamodb:PutItem"
            ],
            "Resource": [
                "arn:aws:dynamodb:ap-northeast-1:xxxxxx:table/コメントテーブル"
            ]
        }
    ]
}

3.CreateKukiThumbnailAndStoreInDB

 このアプリには画像がS3にアップロードされたタイミングで起動するLambda関数があります。これはオリジナル画像を縮小しサムネイル画像に変換し、再度S3にアップロードするものです。1つの画像に対してS3には常に2つの画像が存在します。オリジナル画像とサムネイル画像です。このRoleはこのLambda関数の定義の時に指定するものです。

CreateKukiThumbnailAndStoreInDB
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::バケット名/contents/images/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::バケット名/contents/thumbnail/*"
            ]
        }
    ]
}

4.xxxPool-SMS-Role

 このRoleも「AWS+Reactアプリ作成入門(Cognito編」でxxxPoolというユーザプールを作成した時に定義したものです。ユーザ登録時とかにSNSサービスを使って確認メールを送信しますが、その権限を規定したものです。

xxxPool-SMS-Role
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sns:publish"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

今回は以上です。

2
4
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
2
4