Authenticating providers without static keys
Stop pasting access keys into Terraform: use the provider's credential chain, assume a role, and in CI swap a short-lived OIDC token for temporary credentials.
Authenticating providers without static keys
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Never write `access_key` and `secret_key` into a `provider` block. They end up in Git, in shell history and in CI logs, and a saved plan file contains your configuration.
- The AWS provider tries sources in a fixed order: provider arguments, environment, shared credentials and config files, container credentials, then the instance profile.
- `assume_role` makes the provider swap its base identity for a role's temporary credentials. It is how one login reaches many accounts.
- In CI, OIDC replaces stored keys: the CI platform issues a signed token for the job, the cloud checks it against a trust policy, and returns credentials that expire in about an hour.
- Scope the trust policy to one repository and branch or environment, otherwise any workflow in any repository could assume the role.
The key in the repository
A tutorial shows an access_key and a secret_key in the provider block, “just for now”. Someone commits it. Automated scanners look for exactly this pattern in public repositories, so a pushed key should be treated as leaked, and rotated. Even in a private repo, the key is a long-lived password that every developer’s laptop and every CI log has seen.
The fix is not hiding the key better. It is not having a long-lived key at all. The devops lesson on identity makes the general case, and this lesson is the Terraform side.
What the provider already does
Providers look for credentials in a defined order, so most of the time your configuration needs none. For AWS, the provider docs on main list the sources applied in this order:
- parameters in the provider configuration,
- environment variables,
- shared credentials files,
- shared configuration files,
- container credentials,
- instance profile credentials and region.
The docs say this order matches the AWS CLI and the AWS SDK for Go. On a laptop you log in with the CLI, for example with single sign-on to a named profile, and Terraform uses the same session. On an EC2 instance, an ECS or CodeBuild task or an EKS pod with a role, the provider finds the role’s credentials from the environment, with no keys written anywhere. The provider block then says almost nothing:
provider "aws" {
region = "eu-west-1"
}
When something authenticates as the wrong identity, walk down the list. An exported AWS_PROFILE or a leftover AWS_ACCESS_KEY_ID beats the shared files. Check the environment first, and look at aws sts get-caller-identity to see who you really are.
Assuming a role
One login should reach many accounts, but you do not want a user with admin in each. The answer is roles. The assume_role block makes the provider call AWS’s token service and use the temporary credentials that come back:
provider "aws" {
region = "eu-west-1"
assume_role {
role_arn = "arn:aws:iam::111111111111:role/terraform-deploy"
}
}
Written from the AWS provider docs. I did not run it, and the account number is a placeholder. The provider docs say you can chain several roles in order. Combined with aliases from the last lesson, one configuration can reach several accounts, each through a role scoped to what Terraform needs there.
CI: trading a token for credentials
Laptops have people to log in. A pipeline has nobody. The old answer was to store a key as a CI secret. The better answer is OIDC, OpenID Connect:
- The CI platform issues each job a signed identity token that says who it is: which repository, which branch.
- The job presents that token to the cloud’s token service.
- The cloud checks the signature against the platform’s published keys, then checks the role’s trust policy.
- If everything matches, it returns temporary credentials that expire, typically in an hour.
Nothing durable is stored. To see that the trust relies on public, checkable facts, I fetched GitHub Actions’ real discovery document and key set:
$ curl -s https://token.actions.githubusercontent.com/.well-known/openid-configuration | jq '{issuer,jwks_uri,id_token_signing_alg_values_supported,claims_supported}'
{
"issuer": "https://token.actions.githubusercontent.com",
"jwks_uri": "https://token.actions.githubusercontent.com/.well-known/jwks",
"id_token_signing_alg_values_supported": ["RS256"],
"claims_supported": ["sub","aud","iss","ref","repository","environment","job_workflow_ref"]
}
$ curl -s https://token.actions.githubusercontent.com/.well-known/jwks | jq '{keys:(.keys|length)}'
{"keys":4}
I trimmed the claims list to the ones that matter here. GitHub publishes who it is, its signing algorithm and four public keys. AWS needs nothing secret from you. It fetches those keys and verifies each job’s token, then reads the claims. The sub claim names the repository and ref, and the trust policy matches on it.
On the Terraform side the AWS provider takes a token through an assume_role_with_web_identity block, or the AWS_WEB_IDENTITY_TOKEN_FILE environment variable. The docs say values in the block win over environment variables. The AWS side, a trust policy and the role, is written from GitHub’s and AWS’s docs, not run here:
{
"Effect": "Allow",
"Principal": { "Federated": "arn:aws:iam::111111111111: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/infra:ref:refs/heads/main"
}
}
}
The sub condition is the one that matters. Leave it out, or use a wildcard like repo:*, and any repository on GitHub could assume your role. Pin it to one repository, and to a branch or an environment. The git-github roadmap’s OIDC lesson covers the workflow side, and the CI/CD group here returns to it with Terraform pipelines.
Other clouds, same shape
Google Cloud has workload identity federation, Azure has federated credentials on a managed identity or app registration, and HCP Terraform issues its own dynamic credentials. The shape repeats: a signed token, a trust rule, short-lived credentials. I only checked the AWS provider’s docs for this lesson.
What this does not fix
- Secrets other than cloud keys. Database passwords and API tokens for other providers need their own handling, in secrets management and the sensitive-values lesson later.
- Over-powerful roles. Temporary admin is still admin. Scope the role to what the pipeline manages.
- State. The backend needs credentials too, and the same rules apply to them.
About the video
The embed is all of More than Certified’s “GitHub Actions: Configure OIDC Authentication for AWS and Terraform”, October 2024, 10 minutes. The description says it uses OpenID Connect to authenticate Terraform to AWS for deployments through GitHub Actions. It is one part of a longer series, so some context is assumed. The deeper links cover assuming a role through STS from a laptop, and a 2022 tour of the older credential sources. English was judged from title, channel and English auto-captions. I judged the content from the description.
Where this fits
Providers are now installed, configured, pinned and authenticated. The last lesson in this group covers the small utility providers that generate values rather than call a cloud: utility providers, random, time and terraform_data.
Check yourself
A teammate suggests exporting `AWS_ACCESS_KEY_ID` in the CI settings. What is wrong with that, and what replaces it?
It is a long-lived secret that can leak and must be rotated. Use OIDC: the job's identity token is exchanged for temporary credentials by assuming a role, so nothing durable is stored.
What is the role of the trust policy in OIDC authentication?
It states who may assume the role: the token issuer, the audience, and conditions on the subject such as the repository and branch. It is what stops other repositories from using your role.
Why does the provider's order of credential sources matter when debugging?
A stray `AWS_PROFILE` or old environment variable can beat the credentials you thought you were using. Knowing the order lets you find which source won.
Where does the provider get a web identity token when using `assume_role_with_web_identity`?
From `web_identity_token` or from a file named by `web_identity_token_file`, which can also be set with environment variables. The CI platform writes or provides the token for each job.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.