How to Get Temporary Credentials with STS in AWS Emulator
Running aws sts assume-role with the AWS CLI returns temporary credentials for an IAM role: an access key ID, a secret access key, and a session token. Put these three values in environment variables, and the commands that follow run with the role's permissions. I followed the steps for switching to another role (switching roles) and for passing narrower credentials to a script, using AWS Emulator without an AWS account. The commands and output are copied as they were run in my test environment.
The test environment was Ubuntu 24.04.5 (x86_64), AWS Emulator 1.0.624 (released November 26, 2026, Debian package), and AWS CLI 2.42.4. I did not use an AWS account or any credentials. I did not check how real AWS STS behaves, assuming a role in a different account, MFA, external IDs (ExternalId), session tags, or role chaining (assuming another role from a role). Where I describe real AWS behavior, I am relying on the official documentation. According to the AWS Emulator official documentation, the emulator's behavior is matched against AWS in the US West (Oregon, us-west-2) Region, and global services use us-east-1.
- 1. Connect to the Emulator and Create a Role to Assume
- 2. Get Credentials with aws sts assume-role
- 3. Put the Credentials in Environment Variables and Act with the Role's Permissions
- 4. Choose the Lifetime with –duration-seconds
- 5. Credentials Past Their Expiration Are Refused
- 6. Write a Profile in the AWS CLI Config and Assume the Role Automatically
- 7. When AssumeRole Is Refused
- 8. Use –policy to Get Credentials with Narrower Permissions Than the Role
Connect to the Emulator and Create a Role to Assume
AWS Emulator is an emulator that accepts AWS API calls on your own machine. For installation, see the separate article "How to Install AWS Emulator on Ubuntu and Start Using It." In this article, I sent commands from a shell where the environment variables in the official documentation were set, against an emulator started with aws-emulator -t off. -t off disables sending usage data (telemetry) to AWS.
If the connection works, aws sts get-caller-identity returns a user named emulator, which the emulator provides. I replaced the account ID in the output with <12桁のアカウントID>. I ran some commands together in one go, so their output is shown separately for each command. Also, every code block starts from the emulator user's shell, without the three credential environment variables (such as AWS_ACCESS_KEY_ID, which appear later). If you continue from here, run unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN before the blocks.
$ aws sts get-caller-identity
{
"UserId": "AIDAH5I7GEMO7ADXNFAZU",
"Account": "<12桁のアカウントID>",
"Arn": "arn:aws:iam::<12桁のアカウントID>:user/emulator"
}
In the commands, I store the account ID in a variable named ACCOUNT.
$ export ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
Next, create the role to assume. A role needs a trust policy that says who may assume it. Here, the trust policy allows the emulator user to call sts:AssumeRole.
$ cat > trust.json <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::${ACCOUNT}:user/emulator" },
"Action": "sts:AssumeRole"
}
]
}
EOF
$ cat trust.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::<12桁のアカウントID>:user/emulator" },
"Action": "sts:AssumeRole"
}
]
}
$ aws iam create-role --role-name example-reader --assume-role-policy-document file://trust.json --query "Role.{Arn:Arn,MaxSessionDuration:MaxSessionDuration}"
{
"Arn": "arn:aws:iam::<12桁のアカウントID>:role/example-reader",
"MaxSessionDuration": 3600
}
I attached the AWS managed policy ReadOnlyAccess to the role. The AWS managed policy reference describes it as a policy that grants read-only access to AWS services and resources. To look at the difference between reading and writing later, I also created one bucket as the emulator user.
$ aws iam attach-role-policy --role-name example-reader --policy-arn arn:aws:iam::aws:policy/ReadOnlyAccess
$ aws s3 mb s3://example-bucket
make_bucket: example-bucket
Get Credentials with aws sts assume-role
Pass the ARN of the role to assume to --role-arn, and the session name to --role-session-name. Both are required values in the AssumeRole API reference.
$ date -u +%FT%TZ; aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name demo-session
2026-11-26T06:22:47Z
{
"Credentials": {
"AccessKeyId": "EMULATORAS3YCSMENYPK",
"SecretAccessKey": "bsI1HTd6SM7JvLiXPGegJMoJVAZ1JcNEmKCScqLL",
"SessionToken": "+6BPglZnJNE0Mvv258ourj7pW0pNGMEp…(長いので途中から省略)",
"Expiration": "2026-11-26T07:22:47+00:00"
},
"AssumedRoleUser": {
"AssumedRoleId": "AROABGXEQES5NRCCJJWAN:demo-session",
"Arn": "arn:aws:sts::<12桁のアカウントID>:assumed-role/example-reader/demo-session"
},
"PackedPolicySize": 1,
"SessionTokenUtilization": 17,
"SessionTokenSize": 712
}
The three values in Credentials are the temporary credentials. The SessionToken was 712 bytes long, so I shortened it in the output. According to the AWS Emulator security page, the emulator also accepts keys created by its own IAM and STS, and those keys work only while that run of the emulator is running.
Expiration was exactly one hour after the time the credentials were obtained. According to the API reference, the default when DurationSeconds is not specified is 3600 seconds. The ARN in AssumedRoleUser takes the form assumed-role/role-name/session-name, and the session name you passed appears at the end.
Put the Credentials in Environment Variables and Act with the Role's Permissions
When you put the three values into AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN, the AWS CLI commands that follow are signed with these credentials. I used --query to extract only the three values, built export statements with awk, and ran them with eval.
$ eval "$(aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name demo-session --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" --output text | awk "{print \"export AWS_ACCESS_KEY_ID=\" \$1 \" AWS_SECRET_ACCESS_KEY=\" \$2 \" AWS_SESSION_TOKEN=\" \$3}")"
$ aws sts get-caller-identity
{
"UserId": "AROABGXEQES5NRCCJJWAN:demo-session",
"Account": "<12桁のアカウントID>",
"Arn": "arn:aws:sts::<12桁のアカウントID>:assumed-role/example-reader/demo-session"
}
$ aws s3 ls
2026-11-26 06:22:47 example-bucket
$ aws s3 mb s3://example-bucket2
make_bucket failed: s3://example-bucket2 An error occurred (AccessDenied) when calling the CreateBucket operation: User: arn:aws:sts::<12桁のアカウントID>:assumed-role/example-reader/demo-session is not authorized to perform: s3:CreateBucket on resource: "arn:aws:s3:::example-bucket2" because no identity-based policy allows the s3:CreateBucket action
The ARN from get-caller-identity changed from the user to the role session. I could list the buckets, but creating one was refused with AccessDenied, because the policy attached to the role does not allow s3:CreateBucket.
If you forget to set AWS_SESSION_TOKEN, requests fail even when the access key and secret are correct. In a shell where only the first two were set, I got InvalidClientTokenId.
$ eval "$(aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name demo-session --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" --output text | awk "{print \"export AWS_ACCESS_KEY_ID=\" \$1 \" AWS_SECRET_ACCESS_KEY=\" \$2}")"
$ aws sts get-caller-identity
aws: [ERROR]: An error occurred (InvalidClientTokenId) when calling the GetCallerIdentity operation: The security token included in the request is invalid.
To go back to the original user, unset the three environment variables. I checked this operation on an emulator started again later, after creating the same role and bucket. After listing the bucket contents in the role session (the bucket is empty, so nothing was printed), running unset returned me to the emulator user.
$ eval "$(aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name demo-session --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" --output text | awk "{print \"export AWS_ACCESS_KEY_ID=\" \$1 \" AWS_SECRET_ACCESS_KEY=\" \$2 \" AWS_SESSION_TOKEN=\" \$3}")"
$ aws sts get-caller-identity --query Arn --output text
arn:aws:sts::<12桁のアカウントID>:assumed-role/example-reader/demo-session
$ aws s3 ls s3://example-bucket; echo "exit $?"
exit 0
$ unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
$ aws sts get-caller-identity --query Arn --output text
arn:aws:iam::<12桁のアカウントID>:user/emulator
According to the AWS Emulator security page, some emulator services evaluate IAM policies for IAM users and roles created inside the emulator, but the evaluation is not the same as in AWS. The same page also says that requests signed with the emulator's own keys are not denied. Even so, in my tests, AssumeRole calls made with the emulator's own keys sometimes returned AccessDenied (see "When AssumeRole Is Refused" below). Every AccessDenied in this article is a result from this version of the emulator. The same results may not occur in real AWS, so you need to check your permission settings against real AWS.
Choose the Lifetime with –duration-seconds
With --duration-seconds, you can set the lifetime of the credentials in seconds. The API reference says you can specify a value from 900 seconds (15 minutes) up to the role's maximum session duration (MaxSessionDuration). The maximum session duration can be set from 1 hour to 12 hours, and the default is 1 hour (IAM User Guide, page on updating role settings).
I tried several values. For each one, I show the time just before the call and the Expiration that was returned.
$ for d in 899 900 3600 3601 43201; do echo "--- $d"; date -u +%FT%TZ; aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name d$d --duration-seconds $d --query Credentials.Expiration --output text; echo "exit $?"; done
--- 899
2026-11-26T06:22:51Z
aws: [ERROR]: An error occurred (ParamValidation): Parameter validation failed:
Invalid value for parameter DurationSeconds, value: 899, valid min value: 900
exit 252
--- 900
2026-11-26T06:22:52Z
2026-11-26T06:37:52+00:00
exit 0
--- 3600
2026-11-26T06:22:53Z
2026-11-26T07:22:53+00:00
exit 0
--- 3601
2026-11-26T06:22:53Z
aws: [ERROR]: An error occurred (ValidationError) when calling the AssumeRole operation: The requested DurationSeconds exceeds the MaxSessionDuration set for this role.
exit 254
--- 43201
2026-11-26T06:22:54Z
aws: [ERROR]: An error occurred (ValidationError) when calling the AssumeRole operation: 1 validation error detected: Value '43201' at 'durationSeconds' failed to satisfy constraint: Member must have value less than or equal to 43200
exit 254
900 and 3600 seconds were accepted, and Expiration was exactly 15 minutes and exactly 1 hour after the time of the call. For 899 seconds, the AWS CLI stopped before sending the request, with a ParamValidation error. The AWS CLI return codes page says that 252 means the command was not run because a parameter value was invalid, and 254 means the request was sent to the service and the service returned an error. For 3601 seconds, which is one second over the role's maximum session duration (3600 seconds), the emulator refused with ValidationError (exit code 254). For 43201 seconds, the request was refused with a different message: it exceeded the API limit of 43200 seconds (12 hours).
If you want to use credentials for longer than one hour, first raise the role's maximum session duration. After I ran aws iam update-role --max-session-duration 7200 to set it to 2 hours, I could request 7200 seconds.
$ aws iam update-role --role-name example-reader --max-session-duration 7200
$ date -u +%FT%TZ; aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name d7200 --duration-seconds 7200 --query Credentials.Expiration --output text
2026-11-26T06:22:55Z
2026-11-26T08:22:56+00:00
Credentials Past Their Expiration Are Refused
Even in the emulator, credentials stop working after their Expiration. I saved the credentials obtained with a 900-second lifetime to short.json, put the three values into environment variables, and then ran the same commands right after getting them and again 16 minutes later. The only thing I ran while waiting was sleep 960.
$ aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name short --duration-seconds 900 > short.json; jq -r .Credentials.Expiration short.json
2026-11-26T06:38:03+00:00
$ export AWS_ACCESS_KEY_ID=$(jq -r .Credentials.AccessKeyId short.json) AWS_SECRET_ACCESS_KEY=$(jq -r .Credentials.SecretAccessKey short.json) AWS_SESSION_TOKEN=$(jq -r .Credentials.SessionToken short.json)
$ date -u +%FT%TZ; aws sts get-caller-identity --query Arn --output text
2026-11-26T06:23:03Z
arn:aws:sts::<12桁のアカウントID>:assumed-role/example-reader/short
$ date -u +%FT%TZ; aws sts get-caller-identity --query Arn --output text
2026-11-26T06:39:04Z
aws: [ERROR]: An error occurred (ExpiredTokenException) when calling the GetCallerIdentity operation: The security token included in the request is expired
$ aws s3 ls
aws: [ERROR]: An error occurred (InvalidAccessKeyId) when calling the ListBuckets operation: The AWS Access Key Id you provided does not exist in our records.
The STS call failed with ExpiredTokenException (expired), but the S3 call failed with a different error, InvalidAccessKeyId (the access key does not exist). In this emulator, the same expired credentials produced a different kind of error depending on which service was called. Once the credentials expire, call assume-role again to get new ones.
Write a Profile in the AWS CLI Config and Assume the Role Automatically
Instead of calling assume-role each time and putting the values into environment variables, you can also write a role profile in the AWS CLI config file. According to the AWS CLI user guide page on using IAM roles, when you specify a profile that has role_arn and source_profile, the AWS CLI calls AssumeRole in the background using the credentials of source_profile, and runs the command with the temporary credentials that are returned. If you set role_session_name, it becomes the session name.
The official documentation says not to edit the config file that the emulator writes, but to make a copy and point AWS_CONFIG_FILE at it. I did that and added a profile named reader (<HOME> stands for the home directory path).
$ cp ~/.aws/emulator/9297/config ./config-with-role
$ cat >> ./config-with-role <<EOF
[profile reader]
role_arn = arn:aws:iam::${ACCOUNT}:role/example-reader
source_profile = default
role_session_name = cli-profile
EOF
$ cat ./config-with-role
[default]
region = us-east-1
endpoint_url = https://127.0.0.1:9297
ca_bundle = <HOME>/.aws/emulator/9297/ca.pem
s3 =
signature_version = s3v4
[profile reader]
role_arn = arn:aws:iam::<12桁のアカウントID>:role/example-reader
source_profile = default
role_session_name = cli-profile
With --profile reader, the commands ran as the role session. The permissions were also the role's, and I could not create a bucket.
$ AWS_CONFIG_FILE=./config-with-role aws sts get-caller-identity --profile reader
{
"UserId": "AROABGXEQES5NRCCJJWAN:cli-profile",
"Account": "<12桁のアカウントID>",
"Arn": "arn:aws:sts::<12桁のアカウントID>:assumed-role/example-reader/cli-profile"
}
$ AWS_CONFIG_FILE=./config-with-role aws s3 mb s3://example-bucket2 --profile reader
make_bucket failed: s3://example-bucket2 An error occurred (AccessDenied) when calling the CreateBucket operation: User: arn:aws:sts::<12桁のアカウントID>:assumed-role/example-reader/cli-profile is not authorized to perform: s3:CreateBucket on resource: "arn:aws:s3:::example-bucket2" because no identity-based policy allows the s3:CreateBucket action
According to the AWS CLI user guide, temporary credentials obtained this way are stored in ~/.aws/cli/cache and reused until they expire.
When AssumeRole Is Refused
Session names can contain only letters, digits, and the characters _+=,.@- (API reference). When I included a space, the emulator refused the request and showed the regular expression of the allowed characters.
$ aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name "my session"
aws: [ERROR]: An error occurred (ValidationError) when calling the AssumeRole operation: 1 validation error detected: Value 'my session' at 'roleSessionName' failed to satisfy constraint: Member must satisfy regular expression pattern: [\w+=,.@-]*
A role is also refused when its trust policy does not allow the caller. I created a role that trusts only the Lambda service, and when the emulator user tried to assume it, the result was AccessDenied.
$ cat > trust-lambda.json <<EOF
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}
EOF
$ aws iam create-role --role-name example-lambda-only --assume-role-policy-document file://trust-lambda.json --query Role.Arn --output text
arn:aws:iam::<12桁のアカウントID>:role/example-lambda-only
$ aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-lambda-only --role-session-name demo-session
aws: [ERROR]: An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::<12桁のアカウントID>:user/emulator is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::<12桁のアカウントID>:role/example-lambda-only
When I passed a role name that does not exist, the emulator also returned AccessDenied with the same message. The error text alone does not tell you whether you mistyped the role name or whether the trust policy does not allow the caller.
$ aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/no-such-role --role-session-name demo-session
aws: [ERROR]: An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::<12桁のアカウントID>:user/emulator is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::<12桁のアカウントID>:role/no-such-role
Use –policy to Get Credentials with Narrower Permissions Than the Role
If you pass a JSON policy to --policy, it becomes a session policy. According to the API reference, the session's permissions are the range allowed by both the role's policies and the session policy (their intersection). You cannot add permissions that the role does not have.
Here, I passed a policy that allows only s3:ListAllMyBuckets, which lists buckets. The listing of bucket contents that worked in the role session without a session policy (see "Put the Credentials in Environment Variables and Act with the Role's Permissions" above) was refused in this session, because s3:ListBucket was not allowed.
$ cat > session-policy.json <<EOF
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListAllMyBuckets","Resource":"*"}]}
EOF
$ eval "$(aws sts assume-role --role-arn arn:aws:iam::${ACCOUNT}:role/example-reader --role-session-name narrowed --policy file://session-policy.json --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" --output text | awk "{print \"export AWS_ACCESS_KEY_ID=\" \$1 \" AWS_SECRET_ACCESS_KEY=\" \$2 \" AWS_SESSION_TOKEN=\" \$3}")"
$ aws s3 ls
2026-11-26 06:22:47 example-bucket
$ aws s3 ls s3://example-bucket
aws: [ERROR]: An error occurred (AccessDenied) when calling the ListObjectsV2 operation: User: arn:aws:sts::<12桁のアカウントID>:assumed-role/example-reader/narrowed is not authorized to perform: s3:ListBucket on resource: "arn:aws:s3:::example-bucket" because no session policy allows the s3:ListBucket action
The reason in the error changed from "no identity-based policy allows" to "no session policy allows," which shows that the session policy is what blocked the request.





