Sub topic 8
Assuming roles automatically with CLI profiles
This chapter assumes you have already created the ReadOnlyAuditor role in account 222222222222, with a trust policy for account 111111111111 and a matching sts:AssumeRole permission on the caller. It also assumes you have run aws sts assume-role by hand. Here you will let the AWS CLI do that work for you.
Why use a profile
Calling aws sts assume-role by hand returns an access key, a secret key, and a session token. You would then have to export all three as environment variables, and repeat the process when they expire. A role profile moves this work into the CLI. When you use the profile, the CLI calls AssumeRole, caches the temporary credentials, and requests new ones after they expire.
Defining a role profile
Add a profile to ~/.aws/config:
[profile auditor]
role_arn = arn:aws:iam::222222222222:role/ReadOnlyAuditor
source_profile = default
region = us-east-1The settings work like this:
role_arnis the role to assume.source_profilenames the profile whose credentials make theAssumeRolecall. Those credentials belong to the identity in account 111111111111, so that identity needs the caller policy from the earlier chapter.regionsets the default Region for calls made with this profile.
Credentials for the default profile stay in ~/.aws/credentials as usual. The role profile stores no secrets.
Checking that it works
Run an identity check with the profile:
aws sts get-caller-identity --profile auditorThe Arn in the output should look like arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/botocore-session-.... It shows the role's account and an assumed-role identity. If you see your original user ARN, the CLI did not use the profile.
Then run a real command. It uses the role's permissions, not your user's:
aws s3 ls --profile auditorTo use the profile for a whole shell session, set AWS_PROFILE=auditor instead of repeating --profile.
What happens behind the scenes
The CLI keeps the temporary credentials in a cache under ~/.aws/cli/cache. Later commands reuse them until they are close to expiring.
Optional settings
You can add more settings to the profile:
| Setting | Purpose |
|---|---|
| `role_session_name` | Sets the session name that appears in CloudTrail. |
| `duration_seconds` | Requests a session length, up to the role's maximum session duration. |
| `external_id` | Passes an external ID when the trust policy requires one. |
| `mfa_serial` | Prompts for an MFA code before assuming the role, when the trust policy requires MFA. |
A named session is useful when you audit activity, because it tells you which person or tool used the role.
Common mistakes
- Wrong source profile. If
source_profilepoints to a profile without permission to callsts:AssumeRoleon that role ARN, the call fails withAccessDenied. - Typo in the role ARN. A wrong account ID or role name produces
AccessDenied, not "not found". - Profile name collisions. In
~/.aws/config, non-default profiles need theprofileprefix. In~/.aws/credentials, they do not. - Expired source credentials. If the source profile uses temporary credentials, such as an SSO session, refresh them first.
Exercise
A role profile has source_profile = default, and the default profile's user has no sts:AssumeRole permission. What happens when you run aws s3 ls --profile auditor, and which policy do you fix?
Show answer
The CLI's AssumeRole call fails with AccessDenied, so the S3 command never runs. Fix the caller policy: add an identity policy to the user in account 111111111111 that allows sts:AssumeRole on the role's ARN.