Sub topic 8
Assuming Roles Automatically with CLI Profiles
This chapter assumes you can read IAM policy JSON, know what an ARN is, and have the AWS CLI installed with a working default profile. It also assumes you have already written the trust policy and the caller policy for a role such as ReadOnlyAuditor in account 222222222222. Here you will stop running aws sts assume-role by hand and let the CLI do it for you.
Why use a profile instead of calling AssumeRole by hand
Running aws sts assume-role returns an AccessKeyId, a SecretAccessKey, a SessionToken, and an Expiration. To use them you must copy three values into environment variables, and you must repeat the whole process when they expire. This is slow and error-prone. It also tends to leave secrets in shell history and in scripts.
A role profile moves that work into the CLI. You describe the role once in ~/.aws/config. Each time you run a command with that profile, the CLI does these steps:
- It reads the credentials of the source profile.
- It calls
sts:AssumeRolefor the role you named. - It caches the temporary credentials on disk.
- It signs your command with those credentials.
- It calls
AssumeRoleagain when the cached credentials are close to expiring.
The same behavior is available in the AWS SDKs, because they share the same configuration file and credential resolution logic.
A minimal role profile
Add this profile to ~/.aws/config:
[profile auditor]
role_arn = arn:aws:iam::222222222222:role/ReadOnlyAuditor
source_profile = default
region = us-east-1Each line has a specific job:
role_arnis the role the CLI will assume. The role lives in the trusting account (222222222222).source_profilenames another profile whose credentials make theAssumeRolecall. Those credentials belong to the trusted account (111111111111).regionsets the default Region for commands that use this profile. It does not affect which Region the role lives in, because IAM roles are global.
Confirm that the profile works:
aws sts get-caller-identity --profile auditorA successful response shows an assumed-role ARN in the trusting account, similar to this:
{
"UserId": "AROAEXAMPLEEXAMPLE:botocore-session-1767000000",
"Account": "222222222222",
"Arn": "arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/botocore-session-1767000000"
}Three details in this output are worth noting. The account is 222222222222, not your own. The ARN uses the sts service and the assumed-role resource type, not iam and role. The session name at the end is generated by the CLI, which matters when you read CloudTrail later.
How the CLI resolves the profile
The diagram shows what happens when you run a command with --profile auditor.
The cache lives in ~/.aws/cli/cache. Later commands reuse the cached credentials until shortly before they expire, so you do not call STS on every command. If you want to force a fresh call, delete the cache files in that directory.
Where source credentials can come from
source_profile is not limited to long-term access keys. The source profile can be any profile that resolves to credentials. A common arrangement keeps the keys in ~/.aws/credentials and the role profiles in ~/.aws/config:
[default]
aws_access_key_id = AKIAEXAMPLEEXAMPLE
aws_secret_access_key = exampleSecretAccessKeyDoNotUseInstead of source_profile, you can use credential_source when the CLI runs somewhere that already has credentials. It accepts these values:
| Value | Where the source credentials come from |
|---|---|
| `Environment` | The `AWS_ACCESS_KEY_ID` and related environment variables |
| `Ec2InstanceMetadata` | The instance profile of the EC2 instance |
| `EcsContainer` | The task role of the ECS container |
You set either source_profile or credential_source in a profile, not both. For example, a script on an EC2 instance in account 111111111111 can assume the auditor role like this:
[profile auditor-from-ec2]
role_arn = arn:aws:iam::222222222222:role/ReadOnlyAuditor
credential_source = Ec2InstanceMetadataIn that case the instance profile role in account 111111111111 needs an identity policy that allows sts:AssumeRole on the target role ARN, exactly as a user would.
Optional settings that change the session
You can add more settings to a role profile. These are the ones you will use most:
| Setting | Effect |
|---|---|
| `role_session_name` | Sets the session name that appears in the assumed-role ARN and in CloudTrail |
| `duration_seconds` | Requests a session length, within the role's maximum session duration |
| `external_id` | Sends an external ID with the request, for roles whose trust policy requires one |
| `mfa_serial` | Names the MFA device ARN. The CLI prompts for a code before calling STS |
A profile that uses several of them looks like this:
[profile auditor-strict]
role_arn = arn:aws:iam::222222222222:role/ReadOnlyAuditor
source_profile = default
role_session_name = alice-audit
duration_seconds = 3600
mfa_serial = arn:aws:iam::111111111111:mfa/alice
region = us-east-1Setting role_session_name is a small habit with a large payoff. Without it, every session name looks like botocore-session- followed by a number. With it, CloudTrail events in the trusting account show who was behind the session.
Some constraints apply:
duration_secondscannot exceed the role's maximum session duration. The default maximum is one hour, and an administrator can raise it to 12 hours. A request above the maximum fails with a validation error.mfa_serialonly works if the trust policy has a condition that checks MFA, or if you want the CLI to supply MFA regardless. If the trust policy requires MFA and the profile omitsmfa_serial, the call fails withAccessDenied.external_idmust match the value in the trust policy'ssts:ExternalIdcondition exactly. The comparison is case-sensitive.
Using the profile
Once the profile exists, you use it like any other profile. There are two ways to select it:
# Per command
aws s3 ls --profile auditor
# For the whole shell session
export AWS_PROFILE=auditor
aws ec2 describe-instancesSetting AWS_PROFILE is convenient, but it can also cause mistakes. If you forget the variable is set, you may run a command against the wrong account. A quick aws sts get-caller-identity before any change is a cheap safety check.
Chaining profiles
A role profile can use another role profile as its source. This lets you hop through an intermediate account:
[profile hub]
role_arn = arn:aws:iam::222222222222:role/HubRole
source_profile = default
[profile spoke]
role_arn = arn:aws:iam::444444444444:role/SpokeRole
source_profile = hubWhen you use --profile spoke, the CLI assumes HubRole with the default credentials, then assumes SpokeRole with the HubRole credentials. This is role chaining, and AWS limits it. A chained session lasts at most one hour, regardless of the maximum session duration set on the roles. Setting duration_seconds above 3600 on a chained profile fails. A later chapter covers chaining in more detail.
Common errors and how to read them
| Message | Likely cause |
|---|---|
| `An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::111111111111:user/alice is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::222222222222:role/ReadOnlyAuditor` | Either the caller's identity policy lacks `sts:AssumeRole` for that ARN, or the trust policy does not trust the caller. Both sides must allow it. |
| `The config profile (auditor) could not be found` | The profile name is misspelled, or it is in the wrong file. Role profiles belong in `~/.aws/config`, with the `profile` prefix in the section header. |
| `Error when retrieving credentials from custom-process` or a missing source credentials error | The `source_profile` does not exist or has no credentials. |
| `ValidationError ... DurationSeconds exceeds the MaxSessionDuration` | `duration_seconds` is higher than the role allows. |
| `AccessDenied` only when MFA is involved | The trust policy requires MFA, but the profile has no `mfa_serial`, or the code was wrong. |
One naming rule trips up many people. In ~/.aws/config, a named profile uses the header [profile auditor]. In ~/.aws/credentials, the same profile uses [auditor] with no prefix. If you put a role profile in the credentials file, keep the same names consistent, and prefer the config file for anything with role_arn.
Checking what the profile actually uses
If a profile misbehaves, inspect it before you change policies. These commands are safe to run:
aws configure list --profile auditor
aws configure get role_arn --profile auditor
aws sts get-caller-identity --profile auditoraws configure list shows where the CLI found the credentials and Region. For a role profile it often reports the type as assume-role. aws sts get-caller-identity is the final check, because it asks STS who you are and does not need any permissions.
Exercise
Your profile [profile auditor] has role_arn and source_profile = default. Running aws s3 ls --profile auditor returns AccessDenied on sts:AssumeRole. Name two places you must check, and say which account each one is in.
Show answer
First, the identity policy of the user or role behind the default profile, which is in the trusted account (111111111111). It must allow sts:AssumeRole on the role's ARN. Second, the trust policy of the role ReadOnlyAuditor, which is in the trusting account (222222222222). Its Principal must include the caller or the caller's account. Access needs both sides to allow it.
Exercise
You add duration_seconds = 7200 to a profile that has source_profile = hub, where hub is itself a role profile. The role's maximum session duration is 12 hours. Why does the call still fail?
Show answer
The profile is chained, because its source is another assumed role. AWS limits a chained session to one hour (3600 seconds), regardless of the role's maximum session duration. A value of 7200 is rejected. Use 3600 or less, or avoid chaining by assuming the role directly from long-term or Identity Center credentials.