A developer is building a web application that stores user-specific files in a private Amazon S3 bucket. The application uses an Amazon Cognito User Pool for user authentication. The developer wants to authorize users to access their department's files in S3 using temporary AWS credentials. The user's department is stored in a custom attribute named custom:department in the User Pool. The developer has created a separate IAM role for each department. Which approach should the developer use to assign the correct IAM role to each user with the least operational overhead?
- Create an Amazon Cognito Identity Pool and add the User Pool as an identity provider. Configure rules-based role mapping on the identity provider to match the custom:department claim in the ID token to the corresponding IAM role.Cevap
- BConfigure the Amazon Cognito User Pool to issue temporary AWS credentials directly by mapping the custom:department attribute to the AWS STS AssumeRole API parameters in the User Pool app client settings.
- CCreate an Amazon Cognito Identity Pool with a single authenticated role. Use the IAM policy variable ${cognito-identity.amazonaws.com:custom:department} in the role's permission policy to dynamically grant access to the department's S3 folder.
- DConfigure an API Gateway REST API with a custom Lambda authorizer that validates the User Pool ID token and calls the AWS STS AssumeRole API to generate and return temporary credentials to the client.
Cevap
Create an Amazon Cognito Identity Pool, add the User Pool as an identity provider, and configure rules-based role mapping on the identity provider to match the custom:department claim in the ID token to the corresponding IAM role.
The correct solution uses an Amazon Cognito Identity Pool to exchange the ID token from the User Pool for temporary AWS credentials. By configuring rules-based role mapping on the User Pool identity provider within the Identity Pool, the developer can inspect the custom:department claim present in the authenticated user's ID token and dynamically assign the corresponding department-specific IAM role. This requires zero custom code and leverages native AWS features, minimizing operational overhead.
Adım Adım Çözüm
Anahtar Kavram
Role mapping in Amazon Cognito Identity Pools based on Cognito User Pool ID token claims