Managing Human and Machine Identities with AWS STS and MFA
Every request to AWS is made by an identity: a person through a console or CLI, or a machine such as an instance, a container, a function or an external system. The safest identities use temporary credentials and strong authentication. This lesson explains AWS STS operations, instance profiles and IMDSv2, federation for external machines with OIDC and IAM Roles Anywhere, MFA enforcement, IAM Identity Center for people, and protection of the root user.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the main AWS STS operations and when each is used.
- Explain how instance profiles, IMDSv2 and service roles provide temporary credentials to AWS compute.
- Explain how external workloads obtain temporary credentials with OIDC federation and IAM Roles Anywhere.
- Explain how MFA is enforced with IAM conditions and Identity Center, and how the root user is protected.
AWS Security Token Service
AWS STS issues temporary security credentials, consisting of an access key ID, a secret access key and a session token, which expire automatically. Temporary credentials remove the risk of long-lived keys being leaked and reused indefinitely. The main operations are summarised below.
| Operation | Used by | Purpose |
|---|---|---|
| AssumeRole | IAM principals, services, cross-account automation | Obtain a role's permissions, optionally with MFA, external ID, session policy and tags |
| AssumeRoleWithWebIdentity | Workloads with OIDC tokens (GitHub Actions, EKS IRSA, Cognito) | Federate an OIDC identity into a role |
| AssumeRoleWithSAML | Users from a SAML identity provider | Federate corporate users into a role |
| GetSessionToken | IAM users | Obtain temporary credentials, typically after MFA, for CLI use |
| GetCallerIdentity | Anyone | Show which identity the current credentials represent |
Role sessions last from 15 minutes up to the role's maximum session duration of up to 12 hours, and one hour when roles are chained. The sts:SourceIdentity attribute can carry the original user's identity through role sessions, so CloudTrail shows who actually performed actions even after role assumption. Temporary credentials can be revoked early by attaching a policy that denies sessions issued before a time, using the aws:TokenIssueTime condition, which the console's revoke sessions option does.
STS operations
Credentials for AWS Compute
Workloads running on AWS should never store access keys. Each compute service provides role credentials automatically.
- EC2: an instance profile contains a role, and the SDKs obtain its credentials from the instance metadata service. IMDSv2 requires a session token obtained with a PUT request, which defends against server-side request forgery attacks that could steal credentials through IMDSv1. IMDSv2 should be required on all instances, enforced with launch template settings, an account-level default, SCP conditions on
ec2:MetadataHttpTokensor a declarative policy. - Lambda: the execution role.
- ECS: the task role for application code and the task execution role for the agent.
- EKS: EKS Pod Identity or IRSA for pods.
- CodeBuild, CodePipeline, Step Functions and other services: service roles.
Credentials for External Machines
Systems outside AWS also need access: CI/CD systems, on-premises servers, other clouds. Two mechanisms avoid long-term keys.
OpenID Connect federation
A CI system such as GitHub Actions or GitLab issues an OIDC token for each job. An IAM OIDC identity provider in the account trusts the issuer, and a role's trust policy allows AssumeRoleWithWebIdentity with conditions on the token's claims, such as the repository and branch (token.actions.githubusercontent.com:sub). Each job receives short-lived credentials, and only the named repository and branch can assume the role.
{
"Effect": "Allow",
"Principal": {"Federated": "arn:aws:iam::111122223333:oidc-provider/token.actions.githubusercontent.com"},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:acme/orders:ref:refs/heads/main"}
}
}IAM Roles Anywhere
Workloads with X.509 certificates issued by a trusted certificate authority, such as AWS Private CA or an enterprise CA registered as a trust anchor, exchange a signature from their certificate for temporary credentials of a role defined in a profile. It suits on-premises servers and workloads in other clouds that already have a PKI, replacing access keys stored on servers.
Remove long-term keys
Multi-Factor Authentication
MFA requires a second factor in addition to a password, which defeats most credential theft. Phishing-resistant FIDO2 security keys and passkeys are preferred to one-time password apps. MFA is enforced in different ways for different identities.
- Root users of every account must have MFA; with centralised root access management, member accounts can have no root credentials at all.
- IAM Identity Center enforces MFA at sign-in for all users, which covers access to every account through permission sets.
- IAM users, where they still exist, can be required to use MFA with the
aws:MultiFactorAuthPresentcondition, for example denying all actions except managing their own MFA device when MFA is absent; for CLI use, they callGetSessionTokenwith an MFA code. - Sensitive role assumption can require MFA in the role's trust policy with
aws:MultiFactorAuthPresentor a maximumaws:MultiFactorAuthAge.
MFA condition subtleties
Conditions on aws:MultiFactorAuthPresent behave differently with long-term access keys, where the key is absent rather than false. Use BoolIfExists in deny statements, or the key will not match requests made with access keys.
Human Access with IAM Identity Center
IAM Identity Center is the recommended way for people to access AWS accounts. It connects to an external identity provider, such as Microsoft Entra ID or Okta, through SAML and SCIM provisioning, or uses its own directory. Users sign in once, with MFA, and choose an account and permission set, receiving temporary role credentials for the console and the CLI (aws configure sso). Access is managed centrally by group, session duration is limited, and CloudTrail records the user's identity. Long-term IAM users for people should be eliminated, and any that remain should be monitored with credential reports and unused access findings.
The identity source decides where users and groups are managed, and there are three options. The Identity Center directory suits organisations without another directory. An external identity provider suits organisations that already use one for other applications, with SCIM keeping users and groups synchronised automatically. Active Directory suits organisations whose identities live in Microsoft AD. It is connected through AWS Directory Service, either AWS Managed Microsoft AD or AD Connector, a proxy that forwards authentication to an existing on-premises directory without caching identities in AWS. Changing the identity source later can remove existing users, groups and their assignments, so the choice is best made at the start.
Removing Long-Term Keys
A credential report shows 140 IAM users with access keys: engineers' personal keys, a GitHub Actions key used for deployments, keys on 12 on-premises servers and an old key used by a monitoring vendor.
-
People
Move engineers to IAM Identity Center with MFA and permission sets, then deactivate and delete their keys.
-
CI/CD
Configure GitHub OIDC federation with a role restricted to the repository and main branch, and delete the deployment key.
-
Servers
Issue certificates from AWS Private CA and use IAM Roles Anywhere on the on-premises servers.
-
Vendor
Replace the vendor's key with a cross-account role whose trust policy requires an external ID.
Why does IMDSv2 protect instance role credentials better than IMDSv1?
Show answer
IMDSv2 requires a session token obtained with a PUT request and returned in a header, which typical server-side request forgery vulnerabilities and open proxies cannot perform, so attackers cannot simply fetch credentials through a vulnerable application.
Exam Tips
Remember for the exam
- Use temporary credentials from AWS STS: AssumeRole, AssumeRoleWithWebIdentity (OIDC), AssumeRoleWithSAML, GetSessionToken (with MFA).
- Require IMDSv2 for EC2 instance profiles; use task roles, execution roles, Pod Identity and service roles elsewhere.
- External CI uses OIDC federation restricted by token claims; external servers use IAM Roles Anywhere with certificates.
- Enforce MFA through Identity Center, root MFA and aws:MultiFactorAuthPresent (BoolIfExists in denies); people should use IAM Identity Center, not IAM users.
- Revoke active sessions with an aws:TokenIssueTime deny; sts:SourceIdentity preserves the original identity.