Sub topic 14
Automation and CI: when not to use Identity Center sign-in
This chapter assumes you have finished the earlier chapters. You can sign in with aws sso login, you know that the CLI caches an SSO access token and role credentials, and you know the basics of IAM roles and trust policies. It covers where Identity Center sign-in stops being the right tool and what to use instead.
Why interactive sign-in does not fit unattended work
aws sso login authorizes the CLI through a browser, and the person doing it proves who they are, usually with a password and MFA. That works for a developer at a keyboard. It does not work for a nightly job, a build runner, or a web server, for three reasons:
- No human is present. Nobody is there to approve the authorization request when the cached token expires.
- The session is tied to a person. Identity Center sessions represent a workforce user. CloudTrail records the user behind each call, which is what you want for people and wrong for a deploy pipeline.
- The refresh path ends. When the SSO session lifetime runs out, the CLI cannot renew it without another interactive login. A job that works on Monday fails the following week.
The goal of the whole tutorial still holds: use temporary credentials and avoid long-term access keys. For machines, you get temporary credentials from a different source.
The decision in one picture
Where the code runs decides which mechanism you use.
Option 1: roles attached to AWS compute
If the code runs on EC2, ECS, EKS, Lambda, or CodeBuild, attach an IAM role to the compute resource. The service delivers temporary credentials to the workload and rotates them automatically. You configure no profile and run no login command.
For EC2, the role comes through an instance profile. The CLI and SDKs find the credentials by calling the instance metadata service. That is why this source sits late in the credential resolution order you saw earlier: it is used only when nothing earlier in the chain supplies credentials.
# Run on the EC2 instance. No profile and no environment variables are set.
aws sts get-caller-identityThe Arn in the output looks like arn:aws:sts::111122223333:assumed-role/app-instance-role/i-0abc123example. The role name is the one you attached, and the session name is the instance ID.
Give each workload its own role with the narrowest policy that works. A shared role makes audit trails ambiguous and widens the damage if one workload is compromised.
Option 2: OIDC federation for CI systems
Many CI systems, including GitHub Actions and GitLab CI, can issue a short-lived signed token (an OpenID Connect or OIDC token) that identifies the running job. AWS can exchange that token for temporary role credentials through sts:AssumeRoleWithWebIdentity. No secret is stored in the CI system.
Setting it up has two parts. First, register the CI provider as an OIDC identity provider in IAM. Second, create a role whose trust policy accepts tokens from that provider and restricts which repositories and branches may use it.
{
"Version": "2012-10-17",
"Statement": [
{
"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"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}
]
}The workflow then asks for the token and assumes the role. The id-token: write permission lets the job request the token.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::111122223333:role/ci-deploy
aws-region: us-west-2
- run: aws sts get-caller-identityOption 3: IAM Roles Anywhere for servers outside AWS
On-premises servers and workloads in other clouds have no AWS role to attach and no OIDC token. IAM Roles Anywhere gives them one. The server holds an X.509 certificate issued by a certificate authority that you register as a trust anchor. It presents the certificate, signs the request, and receives temporary role credentials.
The aws_signing_helper tool plugs into the CLI through credential_process, the same hook the earlier chapter used with aws configure export-credentials:
[profile onprem]
credential_process = aws_signing_helper credential-process --certificate /etc/pki/app/cert.pem --private-key /etc/pki/app/key.pem --trust-anchor-arn arn:aws:rolesanywhere:us-west-2:111122223333:trust-anchor/EXAMPLE --profile-arn arn:aws:rolesanywhere:us-west-2:111122223333:profile/EXAMPLE --role-arn arn:aws:iam::111122223333:role/onprem-appThe private key now plays the part that an access key did. It has the same storage problem, but the credentials it produces expire, and you can revoke trust at the certificate authority.
Option 4: assuming a role from another identity
Sometimes a workload already has credentials from one of the sources above and needs a role in a different account. Do not create a key for that. Configure a profile that assumes the role from the existing credentials:
[profile cross-account]
role_arn = arn:aws:iam::444455556666:role/deploy-target
credential_source = Ec2InstanceMetadata
region = us-west-2The target role's trust policy must name the source role as a principal. Role chaining, where a role assumes another role using credentials that came from a role, limits the new session to one hour. Setting a longer duration_seconds fails in that case.
Where Identity Center still helps in automation
Two cases remain where Identity Center is reasonable.
- Scripts you run yourself. A script you start from your terminal, such as a report or a one-off migration, should use your SSO profile. The script inherits your cached credentials and your audit identity.
- Tools that need static-looking credentials. If a tool cannot read SSO profiles, use
aws configure export-credentialsorcredential_processas shown earlier. The credentials are still temporary and still depend on your interactive session.
On a remote machine without a browser, aws sso login --use-device-code prints a URL and a code that you open on another device. That lets you sign in over SSH. It does not make the session unattended, because a person still approves it.
Comparison
| Situation | Mechanism | Credential lifetime | Stored secret |
|---|---|---|---|
| Developer at a terminal | Identity Center sign-in | Role credentials up to the permission set's session duration | None, only cached tokens |
| EC2, ECS, EKS, Lambda | Attached role | Rotated by the service | None |
| GitHub Actions, GitLab CI | OIDC federation | Per job, up to the role's maximum session duration | None |
| Server outside AWS | IAM Roles Anywhere | Up to the role's maximum session duration | Certificate private key |
| Role in another account | `role_arn` with `credential_source` | One hour when role chaining | None |
| No other option | IAM user access key | Until rotated or deleted | The key itself |
Common mistakes
- Copying
~/.aws/sso/cacheor exported credentials into a CI secret. The values expire, and you have moved a human's identity into a shared system. - Assigning an Identity Center permission set to a "service user". Identity Center is a workforce directory. It has no non-interactive sign-in path.
- Reusing one CI role across all repositories. Without a narrow
subcondition, any repository on the provider can assume it. - Forgetting the
id-token: writepermission. The action then fails with an error saying the OIDC token could not be requested. - Leaving old access keys active after migrating. Deactivate the key first, watch for failures, then delete it. The IAM console shows the key's last-used time.
When long-term keys are the last resort
Some third-party software accepts only an access key ID and secret. If you must use one, reduce the risk:
- Create a dedicated IAM user for that one integration.
- Attach a policy limited to the exact actions and resources needed, with conditions such as source IP where the tool allows it.
- Store the key in a secrets manager, not in source control or CI variables.
- Rotate it on a schedule and alert on unused keys.
- Record the owner and the reason, and revisit when the tool gains role support.
Exercise
A GitHub Actions workflow currently uses an access key stored as a repository secret to deploy to AWS. Describe how to replace it with temporary credentials, and name the one trust-policy condition you must not omit.
Show answer
Register GitHub's OIDC provider (token.actions.githubusercontent.com) in IAM. Create a deploy role whose trust policy allows sts:AssumeRoleWithWebIdentity from that provider. In the workflow, set permissions: id-token: write and use aws-actions/configure-aws-credentials with role-to-assume. Then delete the stored access key. The condition you must not omit is the one on the sub claim, restricting the role to your repository and, ideally, a branch or environment. Without it, any repository using GitHub's provider can assume the role.
Exercise
A nightly script runs on an EC2 instance and fails every week with an expired SSO session. What is the underlying design error, and what is the fix?
Show answer
The script depends on a developer's interactive Identity Center sign-in, which expires with the SSO session and cannot be renewed without a person. The fix is to attach an IAM role to the instance through an instance profile, delete the SSO profile from the script's environment, and let the CLI pick up the role's credentials from the instance metadata service. The role needs only the permissions the script uses.