Access to Role 'cognito role' is forbidden

2019-07-24 14:38发布

问题:

I'm getting this strange error Access to Role 'cognito role' is forbidden.

while creating a cloud formation stack here is my cloud formation file in yaml format.

I'm basically creating a s3 bucket and a cognito identity to facilate client side access to my bucket the whole thing is working fine except this error Access to Role 'phototest-cognitoRole-1AMKUVXUXAJ5H' is forbidden. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: NotAuthorizedException

AWSTemplateFormatVersion: 2010-09-09

Resources:

photoBucket:
    Type: AWS::S3::Bucket 
    Properties:
        BucketName: it-academy-photos-bucket 
        CorsConfiguration: 
            CorsRules:
                - AllowedHeaders: ['*']
                  AllowedMethods: [GET,POST,PUT]
                  AllowedOrigins: ['*']
                  Id: myrules   


cognitoRole:
    Type: AWS::IAM::Role
    Properties:
         AssumeRolePolicyDocument: 
            Version: "2012-10-17"
            Statement:
                - 
                    Effect: Allow
                    Principal:
                     Federated:
                       - "cognito-identity.amazonaws.com"

                    Action:
                       - sts:AssumeRoleWithWebIdentity 


         Policies:
            -
                PolicyName: "photo_client_policy"
                PolicyDocument:
                    Version: '2012-10-17'
                    Statement:

                        - 
                         Sid: VisualEditor1
                         Effect: Allow
                         Action:
                           - s3:PutObject
                           - s3:GetObjectAcl
                           - s3:GetObject
                           - s3:GetObjectTorrent
                           - s3:GetObjectVersionAcl
                           - s3:PutObjectVersionTagging
                           - s3:GetObjectTagging
                           - s3:PutObjectTagging
                           - s3:PutBucketCORS
                           - s3:PutObjectAcl
                           - s3:GetObjectVersion
                         Resource: "*"


cognitoIdentityPool:
    Type: AWS::Cognito::IdentityPool
    Properties:
        IdentityPoolName: "photo_bucket"
        AllowUnauthenticatedIdentities: true

cognitoIdentityPoolRoleAttachment:
    Type: AWS::Cognito::IdentityPoolRoleAttachment
    Properties:
        IdentityPoolId: !Ref cognitoIdentityPool
        Roles:
            unauthenticated: !Ref cognitoRole                               

回答1:

I was having the same problem and figured out the unauthenticated role needs the Arn and not the logical ID. This should fix the issue:

cognitoIdentityPoolRoleAttachment:
    Type: AWS::Cognito::IdentityPoolRoleAttachment
    Properties:
        IdentityPoolId: !Ref cognitoIdentityPool
        Roles:
            unauthenticated: !GetAtt cognitoRole.Arn