-
Notifications
You must be signed in to change notification settings - Fork 0
EN_AWS_IAM
How to obtain temporary credentials via STS
sts:AssumeRoleto access cross-account and same-account resources, and how it differs from OIDC/SAML-basedsts:AssumeRoleWithWebIdentity, written as interview Q&A.
One-line answer: You "assume" a role to obtain temporary security credentials (access key, secret access key, session token), letting you access resources in another account — or with different permissions in the same account — without long-term credentials.
- The
sts:AssumeRoleaction allows you to assume a role that exists in another AWS account or within your own account. When you assume a role, you temporarily gain the privileges associated with that role.- Cross-account access: You can assume a role in another AWS account to access resources in that account.
- Within the same account: Assume a role with different permissions than the user or service executing the current command.
One-line answer: Create a role in target Account B that trusts A (trust policy), attach an
sts:AssumeRolepermission policy to requesting Account A, then have A callassume-roleto receive temporary credentials and manage B's resources — 4 steps.
The following explains how to use Assume Role to grant Account A access to Account B's EKS resources.
Create an IAM role in Account B with a trust policy that allows Account A to assume this role. This role grants permission to manage EKS resources in Account B.
- Example trust policy for the AssumeRole role in Account B: this policy allows users or roles in Account A to assume the role.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:root" // Account A ID
},
"Action": "sts:AssumeRole"
}
]
}Alternatively, you can specify a specific role in Account A:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:role/AccountARole" // Replace with specific role in Account A
},
"Action": "sts:AssumeRole"
}
]
}- Attach EKS permissions to the role in Account B:
- Attach the required EKS policies, such as
AmazonEKSClusterPolicyor custom permissions, to the role to manage EKS resources.
- Attach the required EKS policies, such as
To allow a user or service in Account A to assume a role in Account B, create an IAM policy in Account A.
Example IAM policy for Account A:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::111122223333:role/EKSAdminRole" // Role ARN in Account B
}
]
}- Attach this policy to the required users, groups, or roles in Account A.
A user or service in Account A that has permission to assume the role can do so using the AWS CLI, SDKs, or AWS Console.
Example with AWS CLI:
aws sts assume-role \
--role-arn arn:aws:iam::111122223333:role/EKSAdminRole \
--role-session-name eks-session- This command assumes the role in Account B and provides temporary credentials (access key, secret key, and session token) that can be used to access and manage resources.
- Using temporary credentials, you can manage resources in Account B with Account A's permissions.
Example for updating kubeconfig to manage an EKS cluster in Account B:
aws eks --region us-west-2 update-kubeconfig --name eks-cluster \
--role-arn arn:aws:iam::111122223333:role/EKSAdminRole- This lets you use kubectl from Account A to manage the EKS cluster in Account B.
One-line answer:
AssumeRoleuses AWS IAM credentials for cross-account access within AWS;AssumeRoleWithWebIdentityuses an OIDC/SAML federated token to authenticate external identities like GitHub Actions or Kubernetes SAs without long-term keys.
sts:AssumeRoleWithWebIdentity is used to temporarily acquire a role based on a federated identity from an external provider (OIDC or SAML) such as Google, GitHub Actions, AWS Cognito, or an OIDC-compatible identity provider.
- Authentication method: Requires an OIDC token or SAML assertion instead of AWS IAM credentials.
- Key use cases: CI/CD systems such as GitHub Actions, Kubernetes service accounts, AWS Cognito, or external identity providers.
- Enables secure, short-term access to AWS resources without long-term IAM credentials.
- An OIDC provider issues an OIDC token to a user or service, which is included in the
AssumeRoleWithWebIdentityAPI call to access AWS resources. - AWS validates the token against the configured OIDC provider and, if the token and conditions are valid, issues temporary credentials for the role.
| Features | sts:AssumeRole |
sts:AssumeRoleWithWebIdentity |
|---|---|---|
| Authentication Method | AWS IAM Credentials (Access Key and Secret Key) | OIDC token or SAML assertion |
| Use Case | Cross-account access within AWS | Integrated access from external providers |
| Supported ID Types | AWS IAM entities (users, groups, roles) | Federated ID (e.g., Google, GitHub, Cognito) |
| CI/CD Integration | Rarely used | Frequently used in GitHub Actions, Kubernetes, etc. |