Join our Newsletter — 33% off our NHI Course
Home› Guides› How to federate CircleCI to AWS: replace stored…
Guide Cloud & Workload Identity

How to federate CircleCI to AWS: replace stored keys with short-lived OIDC credentials

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 19 November 2026 15 min read
On this page

The safest AWS access key in a CircleCI project is the one that does not exist, and CircleCI's OpenID Connect tokens let a job obtain short-lived AWS credentials without any long-lived secret stored in a project or context. Each CircleCI job can be issued a signed OIDC ID token describing the organisation, project, user and, in newer token versions, the branch and configuration it runs from. AWS can be configured to trust that token through an IAM OIDC identity provider and a role trust policy, and to exchange it for temporary credentials through AssumeRoleWithWebIdentity. Done well, this removes stored AWS access keys from CI entirely and ties each role to the specific projects and branches that should use it. Done carelessly, it creates a role that any project in the organisation can assume. This guide walks platform, cloud and security engineers through the set-up step by step, with the conditions that keep the trust narrow, and shows how to verify and monitor it once live.

For CircleCI's wider identity model (organisation roles, contexts, API tokens and audit logs) see the CircleCI Security Guide. This page covers only the AWS federation path.

This is an independent guide from NHI Mgmt Group, based on CircleCI's public documentation as of November 2026. It is not sponsored or reviewed by CircleCI. Product features change, so confirm settings against CircleCI's current documentation.

Key takeaways

  • CircleCI issues OIDC ID tokens to jobs in environment variables ($CIRCLE_OIDC_TOKEN, $CIRCLE_OIDC_TOKEN_V2 and, on CircleCI cloud, $CIRCLE_OIDC_TOKEN_V3, which CircleCI recommends for new set-ups). Tokens expire one hour after issuance.
  • The issuer is https://oidc.circleci.com/org/<organization_id>, and CircleCI's default tokens have a fixed audience: your organisation ID.
  • A trust policy that checks only the audience lets every project in the organisation assume the role. Always add a project condition, using the oidc.circleci.com/project-id condition key AWS documents for CircleCI, or a sub pattern.
  • Add branch conditions for roles that deploy to production, using the V2 or V3 sub claim, so feature branches and forks cannot reach production credentials.
  • AssumeRoleWithWebIdentity returns temporary credentials and needs no AWS credentials to call. Keep the session short and the role's permissions tight.
  • Once federation works, delete the old access keys from projects and contexts and deactivate them in IAM. Federation that runs alongside the old keys removes no risk.

How CircleCI to AWS federation works

The flow has four parts:

  1. Token issue. When a job runs, CircleCI makes signed OIDC ID tokens available as environment variables. The tokens carry the standard claims iss, sub, aud, iat and exp, plus CircleCI custom claims under the oidc.circleci.com/ prefix.
  2. Trust in AWS. An IAM OIDC identity provider in the target AWS account records CircleCI's issuer URL and the audience to accept. AWS verifies the token signature against the provider's published keys.
  3. Role assumption. The job calls AssumeRoleWithWebIdentity with the token. AWS documents that calling it does not require AWS security credentials; instead, the identity of the caller is validated using the token from the identity provider. The role trust policy decides which tokens are accepted.
  4. Temporary credentials. AWS returns an access key ID, a secret access key and a security token, which the job uses to call AWS APIs until the session ends.

The security of the whole arrangement rests on step 3. The IAM provider says "tokens from this CircleCI organisation are genuine"; only the trust policy says "and only these projects, on these branches, may use this role".

Token versions and the subject claim

CircleCI documents three token variables. Their sub claims differ, and that matters for trust policy patterns:

VariableAvailabilitySubject format documented by CircleCI
$CIRCLE_OIDC_TOKENCloud and Server v4.4+org/<organization_id>/project/<project-id>/user/<user_id>
$CIRCLE_OIDC_TOKEN_V2Cloud and Server v4.4+org/<organization_id>/project/<project-id>/user/<user_id>/vcs-origin/<vcs_origin>/vcs-ref/<vcs_ref>
$CIRCLE_OIDC_TOKEN_V3Cloud onlyColon-separated labelled values: organisation, project and user, then checkout source and ref, then config source and ref

Two details from CircleCI's documentation affect policy design. Pipelines triggered by a custom webhook have no repository information, so the V2 subject falls back to the V1 format. In V3, CircleCI replaces any colon inside a value with %3A, sets the config source and ref to unknown when a pipeline has no configuration in a repository, and leaves the value empty when the pipeline has no checkout source. Write patterns with those cases in mind, so a missing value fails closed rather than matching a wildcard.

Custom claims worth knowing

  • oidc.circleci.com/project-id: the project's UUID, present in all token versions. This is the claim AWS exposes as a condition key.
  • oidc.circleci.com/context-ids: CircleCI describes it as an array of UUIDs that identify the contexts used in the job.
  • oidc.circleci.com/ssh-rerun: a boolean that CircleCI says indicates whether the job was started using the SSH rerun feature.
  • oidc.circleci.com/vcs-origin and vcs-ref (V1 and V2), and checkout-ref, checkout-source, config-ref and config-source (V2 and V3): where the code and configuration came from.
  • org-id, workflow-id, job-id and pipeline-id: identifiers useful for tracing a session back to a pipeline.

Only some claims can be used as AWS condition keys. AWS's condition key reference lists the standard aud, sub and amr keys and, for CircleCI specifically, one provider key: oidc.circleci.com/project-id. Other custom claims are useful in logs and in CircleCI-side controls, but you cannot rely on AWS evaluating them.

Before you start

  • Find your IDs. CircleCI documents that the organisation ID is on the organisation settings page and the project ID is under Project Settings, Overview. Both are UUIDs. Record the IDs of every project that will use each role.
  • Decide one role per purpose. Plan a separate IAM role for each distinct job purpose and environment (for example, build artefact upload, staging deploy, production deploy) rather than one role for all CircleCI work.
  • Inventory existing keys. List every AWS access key stored in CircleCI project environment variables and contexts, and which IAM user each belongs to. These are what federation will replace. The AWS Access Keys Security Guide covers finding and retiring them.
  • Choose the token version. For new set-ups on CircleCI cloud, CircleCI recommends V3. On CircleCI Server, use V2 (V3 is cloud only).

Step 1: create the IAM OIDC identity provider

In the target AWS account, create an OpenID Connect identity provider. AWS's console steps are: in IAM choose Identity providers, then Add provider, choose OpenID Connect, enter the provider URL and the audience. The CLI equivalent is aws iam create-open-id-connect-provider.

  • Provider URL: https://oidc.circleci.com/org/<organization_id>. AWS requires the URL to begin with https://, not contain a port, and be unique among OIDC providers in the account.
  • Audience: your CircleCI organisation ID, which CircleCI documents as the fixed default audience of its tokens. AWS states that the token's aud claim must match the Audience value set on the provider.
  • Same account as the role. AWS notes that OIDC providers used in a role trust policy must be in the same account as the role that trusts it, so create the provider in each account the pipeline deploys to.

On certificates, AWS documents that it verifies the identity provider's JWKS endpoint TLS certificate using its library of trusted root certificate authorities, and falls back to the thumbprints in the provider configuration only in specific cases, such as when the certificate is not signed by one of those trusted CAs.

The provider URL contains a path (/org/<organization_id>). AWS documents that when an OIDC provider URL has a path, the path must be included in the oidc-provider ARN, so the provider ARN takes the form arn:aws:iam::<account_id>:oidc-provider/oidc.circleci.com/org/<organization_id>.

Step 2: write a narrow trust policy

The trust policy is where most of the security lives. CircleCI's documentation describes two ways to restrict a role to particular projects, and recommends the first.

Option 1 (CircleCI's recommended approach): audience plus project ID. Use StringEquals on the audience and on the oidc.circleci.com/project-id claim. AWS describes this key as identifying the CircleCI project in which the job is running, with a UUID value, for use to restrict access to specific CircleCI projects. An illustrative trust policy, with placeholders to replace:

1 Sample only, based on the public documentation cited. Assess it independently against the current official documentation, test outside production and back up first.

Sample 1
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::<account_id>:oidc-provider/oidc.circleci.com/org/<organization_id>"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "oidc.circleci.com/org/<organization_id>:aud": "<organization_id>",
          "oidc.circleci.com/org/<organization_id>:oidc.circleci.com/project-id": "<project_id>"
        }
      }
    }
  ]
}

The audience value must be whatever your tokens actually carry. With CircleCI's default tokens that is the organisation ID; if you later issue tokens with a custom audience, update both the provider and this condition together. To allow more than one project, list the project IDs as an array in the same condition rather than switching to a wildcard.

Option 2: subject pattern. Use StringLike on the sub claim. CircleCI documents the V1 and V2 pattern org/<organization_id>/project/<project-id>/user/* and, for V3, org:<organization_id>:project:<project-id>:user:*. Make sure the pattern matches the token version your jobs present: a V2 pattern will not match a V3 token, and the failure will look like an access denied error.

Restricting production roles to a branch

Project conditions stop other projects using the role; they do not stop a feature branch in the same project from deploying to production. For production roles, CircleCI's documentation gives StringLike examples on the sub claim that limit the role to the main branch. Its V2 example checks the VCS origin and ref in the subject; its V3 example checks both the checkout source and ref and the config source and ref. The V3 form is stronger, because it confirms that the pipeline configuration as well as the code came from the protected branch. Copy the exact pattern from CircleCI's documentation for your token version rather than writing it from memory, and keep the project ID condition alongside it.

Branch conditions are only as strong as the branch protection behind them. Pair them with repository rules that stop direct pushes to the protected branch, and remember that webhook-triggered pipelines produce a V2 subject without repository information, so a branch pattern will correctly refuse them.

Step 3: scope permissions and session length

Attach a permissions policy to the role that allows only the actions and resources the job needs. CircleCI's own guidance is to restrict the role's permissions to what the job needs, and the AWS IAM Policies and Roles Guide covers writing that policy. Consider a permissions boundary on CI roles (see the AWS IAM Permissions Boundaries Guide) so that a pipeline that can create IAM resources cannot create anything more powerful than itself.

Session length is set by the caller within limits the role allows. AWS documents that DurationSeconds defaults to 3,600 seconds and can range from 900 seconds up to the role's maximum session duration, which itself can be set from 1 to 12 hours. Most build and deploy jobs finish well within an hour; leave the role maximum at its lowest practical value so a leaked session expires quickly.

AWS also lets the caller pass session policies with Policy or PolicyArns, and documents that the resulting permissions are the intersection of the role's policy and the session policies, so session policies can only reduce what a session can do. They are a useful way for a single role to be narrowed further for a specific step.

Step 4: configure the CircleCI job

CircleCI's documentation shows the circleci/aws-cli orb doing the exchange: a job runs the orb's aws-cli/setup step with the role_arn and region, and can also set profile_name, role_session_name and session_duration, which default to the job name and 3,600 seconds. CircleCI notes that with this approach you do not need to create or store AWS credential files manually. Pin the orb to the version you have reviewed, as CircleCI's example does, rather than a floating version.

  • Role session name. Set a session name that identifies the project and job, so CloudTrail entries can be traced back to the pipeline. AWS allows 2 to 64 characters of alphanumerics and =,.@- plus underscores.
  • One role per job. Give each job only the role ARN it needs. A workflow with build and deploy jobs should use different roles, so the build job never holds deploy credentials.
  • Keep role ARNs non-secret but controlled. A role ARN is not a credential, but storing it in a restricted context makes changes reviewable.

Step 5: remove the stored keys

Federation reduces risk only when the old long-lived keys are gone. For each project migrated:

  1. Run the pipeline end to end on the federated role and confirm every AWS step succeeds.
  2. Delete the AWS access key variables from the project's environment variables and from any contexts the project uses.
  3. Deactivate the corresponding access keys in IAM, wait through at least one full release cycle, then delete them and, where the IAM user existed only for CI, delete the user.
  4. Record the role ARN, trusted project IDs and branch conditions in your non-human identity inventory, with an owner.

Verify and monitor

Test the trust policy from the negative side as well as the positive. Confirm that a job in an unlisted project, and a job on an unprotected branch for production roles, receive an access denied response. If they succeed, the conditions are not doing what you think.

AWS documents that calling AssumeRoleWithWebIdentity can result in a CloudTrail entry that includes the subject of the web identity token. For CircleCI that subject carries the organisation, project and user identifiers, and in V2 and V3 the source and ref, which makes each session attributable to a pipeline. Useful monitoring, built on the AWS CloudTrail Guide:

  • Alert on successful AssumeRoleWithWebIdentity calls to CI roles whose subject contains a project ID not on the role's approved list (this should be impossible if the policy is right, so any hit is a policy error).
  • Alert on use of production roles from a ref other than the protected branch.
  • Review IAM for remaining active access keys belonging to CI users, and for new ones being created.
  • Periodically review all OIDC providers and the roles that trust them; see How to audit AWS IAM Roles Anywhere and OIDC identity providers.

These alert descriptions are illustrative; adapt field names to how your log platform parses CloudTrail.

Common failure patterns

  • Audience-only trust. The policy checks only aud, which is the same for every project in the organisation, so any project, including one created tomorrow, can assume the role.
  • Wildcard project patterns. A sub pattern such as org/<organization_id>/* has the same effect as audience-only trust.
  • Version mismatch. A V2 sub pattern written for jobs that later move to V3 tokens fails, and the quick fix is often to loosen the pattern rather than update it.
  • No branch condition on production. Any contributor who can push a branch can run a job that deploys to production.
  • Keys left in place. Federation is configured but the old access keys remain in a context "as a fallback", so the exposure is unchanged.
  • One role for everything. A single broadly permissioned CI role makes every project's compromise a full account compromise.
  • Long sessions. A 12-hour role maximum for jobs that run for minutes gives a stolen session far longer than it needs.

Practitioner checklist

  • Create the IAM OIDC provider with issuer https://oidc.circleci.com/org/<organization_id> and the audience your tokens carry, in every target account.
  • Restrict every trust policy to named project IDs with oidc.circleci.com/project-id or an exact sub pattern; never trust the audience alone.
  • Add V3 (or V2) branch conditions to every production role, and enforce branch protection in the repository.
  • Use one role per job purpose and environment, with least-privilege permissions and a permissions boundary.
  • Keep role maximum session duration at the lowest practical value and set meaningful session names.
  • Pin the circleci/aws-cli orb to a reviewed version.
  • Delete AWS keys from projects and contexts, then deactivate and delete them in IAM.
  • Test that unlisted projects and unprotected branches are denied.
  • Monitor AssumeRoleWithWebIdentity events for CI roles and review OIDC trust at least quarterly.

Standards and references

Related NHI Mgmt Group resources: CircleCI Security Guide · AWS Access Keys Security Guide · AWS IAM Policies and Roles Guide · How to audit AWS IAM Roles Anywhere and OIDC identity providers

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.