AWS Assume Role
Guide to assume roles in AWS using OpenID Connect and Web Identity Federation
This guide will walk you through the process of authenticating with AWS services using Web Identity Federation. By following these steps, you can obtain temporary security credentials without embedding long-term credentials in your applications.
The temporary security credentials returned by the AWS API consist of an access key ID, a secret access key, and a security token. Applications can use these temporary security credentials to securely access AWS Resources.
Prerequisites
- An active client in usegrant.dev
- An AWS account
- AWS CLI installed on your system
Before proceeding, you must plan your security strategy to ensure that access tokens are only allocated in a predictable way. To control how your cloud provider issues access tokens, you must define at least one condition to your openid connect provider, so that untrusted entities can't request access tokens for your cloud resources.
Step 1: Set Up an OpenID Connect Provider in AWS
- Log in to the AWS Management Console and navigate to the IAM service.
- In the navigation pane, select "Identity Providers".
- Click on "Create Provider".
- Select "OpenID Connect" as the provider type.
- Fill in the required details and click "Create Provider".
For the provider URL use the url of your project or the custom domain you set up.
Ensure that the audience matches the one used to create the client in useGrant.dev.
For AWS, the audience is typically sts.amazonaws.com
.
Refer to the AWS documentation for more details and customization options.
Step 2: Create a Role in AWS
- Access the AWS Management Console and go to the IAM service.
- In the navigation pane, click on "Roles".
- Select "Create Role".
- Choose "Web Identity" as the trusted entity.
- Select the provider you set up in the previous step and choose the appropriate audience.
For enhanced security, you can also create a custom trust policy that is more restrictive. For example:
Replace acme-prod.auth.usegrant.dev
with the url of your project or the custom domain you set up.
And also replace the subject with the one from your client.
Step 3: Assume Role with AWS CLI
Run the following command to assume the role:
This would return the temporary security credentials with AccessKeyId, SecretAccessKey and SessionToken. You can use these credentials to securely access AWS Resources.
For example,
By default, the temporary security credentials created by AssumeRoleWithWebIdentity last for one hour. However, you can use the optional DurationSeconds parameter to specify the duration of your session. You can provide a value from 900 seconds (15 minutes) up to the maximum session duration setting for the role. This setting can have a value from 1 hour to 12 hours.
Learn more about the AssumeRoleWithWebIdentity API from the official AWS documentation.
Using CloudFormation Template
You can also use CloudFormation to create the role and the trust policy instead of manually creating them as shown in the previous steps.
Example CloudFormation template:
Copy the template and replace the placeholders like PROJECT_URL_ID
, SUBJECT_FROM_CLIENT
with your own values.
- Audience is typically
sts.amazonaws.com
. But you can also use the audience you used while creating the project in useGrant.dev. SUBJECT_FROM_CLIENT
is the subject from the client, this will be generated after you create the client.
If you have a custom domain set up, replace the Url
with your custom domain.