AWS Assume Role
Assume AWS IAM roles using OpenID Connect and web identity federation.
This guide shows how to authenticate with AWS using web identity federation. Your applications get temporary security credentials without embedding long-term credentials.
The temporary credentials returned by AWS consist of an access key ID, a secret access key and a session token, which your applications use to access AWS resources.
Prerequisites
- A useGrant provider with a client
- An AWS account
- The AWS CLI installed on your system
Before you start, plan your security strategy so that credentials are only issued in a predictable way. Define at least one condition on your role's trust policy, so untrusted identities can't request credentials for your cloud resources.
Issuer URL
Your provider's issuer URL has the form https://<PROVIDER_DOMAIN>/<URL_ID> and is shown on the provider's
domain settings page. If you set up a custom domain, the issuer URL is https://<CUSTOM_DOMAIN>.
The examples below use https://auth.example.com/acme-prod.
Step 1: Add an OpenID Connect provider in AWS
- Sign in to the AWS Management Console and open the IAM service.
- In the navigation pane, select Identity providers.
- Click Add provider.
- Select OpenID Connect as the provider type.
- Enter your provider's issuer URL as the provider URL.
- Enter the audience you set when creating the client in useGrant. For AWS, this is typically
sts.amazonaws.com. - Click Add provider.
See the AWS documentation for more details and options.
Step 2: Create a role in AWS
- In the IAM console, select Roles in the navigation pane.
- Click Create role.
- Choose Web identity as the trusted entity type.
- Select the identity provider from the previous step and its audience.
For tighter security, restrict the role's trust policy to your client's subject. For example:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456123456:oidc-provider/auth.example.com/acme-prod"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"auth.example.com/acme-prod:sub": "Z3bXuT3jaHG-ZF9LYt2hmNGi3iAJlUv9",
"auth.example.com/acme-prod:aud": "sts.amazonaws.com"
}
}
}
]
}Replace auth.example.com/acme-prod with your issuer URL without the https:// scheme,
and the subject with the one shown on your client in useGrant.
Step 3: Assume the role with the AWS CLI
Run the following command to assume the role:
aws sts assume-role-with-web-identity \
--role-arn <ROLE_ARN> \
--role-session-name <SESSION_NAME> \
--web-identity-token <USEGRANT_ACCESS_TOKEN>AWS returns temporary credentials with an AccessKeyId, SecretAccessKey and SessionToken,
which you can use to access AWS resources. For example:
{
"Credentials": {
"AccessKeyId": "ASIAIOSFODNN7EXAMPLE",
"SecretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY",
"SessionToken": "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE",
"Expiration": "2026-05-19T18:06:10+00:00"
},
"SubjectFromWebIdentityToken": "Z3bXuT3jaHG-ZF9LYt2hmNGi3iAJlUv9",
"AssumedRoleUser": {
"AssumedRoleId": "AROACLKWSDQRAOEXAMPLE:app1",
"Arn": "arn:aws:sts::123456123456:assumed-role/OIDCRole/app1"
},
"Provider": "arn:aws:iam::123456123456:oidc-provider/auth.example.com/acme-prod",
"Audience": "sts.amazonaws.com"
}By default, the credentials returned by AssumeRoleWithWebIdentity are valid for one hour.
Use the optional --duration-seconds parameter to change this, from 900 seconds (15 minutes)
up to the role's maximum session duration, which can be set between 1 and 12 hours.
Learn more about the AssumeRoleWithWebIdentity API in the AWS documentation.
Using a CloudFormation template
Instead of creating the identity provider and role manually, you can create both with CloudFormation:
AWSTemplateFormatVersion: "2010-09-09"
Description: Configure an OIDC provider and IAM role for web identity federation
Resources:
OIDCProvider:
Type: AWS::IAM::OIDCProvider
Properties:
Url: <ISSUER_URL>
ClientIdList:
- sts.amazonaws.com
IAMRole:
Type: AWS::IAM::Role
Properties:
RoleName: OIDCRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Federated: !Ref OIDCProvider
Action: sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
<ISSUER_HOST_AND_PATH>:sub: <CLIENT_SUBJECT>
<ISSUER_HOST_AND_PATH>:aud: sts.amazonaws.com
ManagedPolicyArns: # replace with the policies you want to attach to the role
- arn:aws:iam::aws:policy/ReadOnlyAccessReplace the placeholders with your own values:
ISSUER_URLis your provider's issuer URL, for examplehttps://auth.example.com/acme-prod.ISSUER_HOST_AND_PATHis the issuer URL without thehttps://scheme, for exampleauth.example.com/acme-prod.CLIENT_SUBJECTis the subject generated when you created the client.- The audience is typically
sts.amazonaws.com, but it must match the audience you set on the client in useGrant.