AWS Builder Center
Stop Storing AWS Keys in GitHub: Secure CI/CD with OpenID Connect (OIDC)

Stop Storing AWS Keys in GitHub: Secure CI/CD with OpenID Connect (OIDC)

Still using AWS access keys in GitHub Actions? Learn how to build a more secure CI/CD pipeline using OpenID Connect (OIDC), AWS IAM roles, and temporary credentials. Follow a practical implementation with trust policies, GitHub Actions workflows, and least-privilege permissions.

student

Stop Storing AWS Keys in GitHub: Secure CI/CD with OpenID Connect (OIDC)

Imagine you have built a web application and pushed your code to GitHub. Now you want every update pushed to the main branch to be automatically tested and deployed to AWS.
You create a GitHub Actions workflow, add your AWS Access Key ID and Secret Access Key to GitHub Secrets, and everything works.
But there is a problem.
You are depending on long-lived AWS credentials that must be protected, rotated, and managed. If these credentials are exposed or misused, someone could potentially access AWS resources using the permissions associated with them.
Is there a better approach?
Yes. You can use OpenID Connect (OIDC) to let GitHub Actions authenticate with AWS without storing long-lived AWS access keys in your repository secrets.
In this article, we will understand how OIDC works, configure AWS IAM, create a secure trust relationship, and build a practical GitHub Actions deployment workflow.

1. What Is CI/CD?

Before implementing OIDC, let's understand the pipeline.
CI/CD stands for Continuous Integration and Continuous Delivery or Deployment.
It helps automate the process of testing, building, and releasing software.
A typical workflow looks like this:
  1. A developer writes code.
  1. The code is pushed to GitHub.
  1. GitHub Actions starts a workflow.
  1. Dependencies are installed.
  1. Automated tests are executed.
  1. The application is built.
  1. The application is deployed to the target environment.
For example, imagine you have a React application. Whenever you push code to the main branch, GitHub Actions can install dependencies, run tests, generate the production build, and deploy the result to AWS.
This reduces repetitive manual work and makes releases more consistent.
However, the pipeline needs permission to access AWS. That is where authentication becomes important.

2. The Problem with Storing AWS Access Keys

A traditional GitHub Actions configuration might use two secrets:
  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
The workflow reads these values from GitHub Secrets and uses them to authenticate with AWS.
This is better than directly writing credentials in source code, but it still introduces credential-management responsibilities.
Potential problems include:
  • Credentials may remain valid until they are rotated or disabled.
  • Accidentally exposing credentials can create security risks.
  • Different repositories may require separate credential-management arrangements.
  • Long-lived credentials need regular monitoring and rotation.
  • A workflow with excessive permissions can cause serious damage if compromised.
GitHub Secrets are useful for many kinds of sensitive configuration, but they do not turn long-lived AWS credentials into temporary credentials.
A stronger approach is to use OIDC federation.

3. What Is OpenID Connect (OIDC)?

OpenID Connect is an identity layer built on top of OAuth 2.0.
In this AWS deployment scenario, GitHub acts as an identity provider. It issues a signed JSON Web Token (JWT) describing the workflow identity.
AWS IAM trusts GitHub's OIDC provider. AWS Security Token Service (STS) validates the token and allows the authorized GitHub workflow to assume a specific IAM role.
AWS then provides temporary security credentials for that role session.
The overall process is:
  1. GitHub Actions starts a workflow.
  1. The workflow requests an OIDC identity token from GitHub.
  1. GitHub issues a short-lived JWT.
  1. The workflow presents the token to AWS STS.
  1. AWS validates the token against the IAM OIDC identity provider and role trust policy.
  1. STS returns temporary AWS credentials.
  1. The workflow uses those credentials to access only the AWS resources permitted by the IAM role.
The important difference is that you do not need to store a permanent AWS Access Key ID and Secret Access Key in GitHub Secrets for this authentication flow.

4. Understand the Architecture

The architecture can be represented as:
Developer → GitHub Repository → GitHub Actions → GitHub OIDC Token → AWS IAM Role → AWS STS → AWS Resources
Each component has a specific responsibility.

GitHub Repository

Stores your application source code and workflow configuration.

GitHub Actions

Executes the automation workflow when a configured event occurs, such as a push to the main branch.

GitHub OIDC Provider

Provides an identity token representing the workflow's identity and context.

AWS IAM Identity Provider

Establishes trust between your AWS account and GitHub's OIDC identity provider.

AWS IAM Role

Defines who can assume the role and what permissions the assumed role provides.

AWS Security Token Service (STS)

Validates the federated identity and issues temporary security credentials when the role assumption is authorized.

AWS Resources

The application deployment target, such as Amazon S3, Amazon EC2, or Amazon ECS.

5. Prerequisites

Before starting the implementation, make sure you have:
  • An AWS account with permission to configure IAM.
  • A GitHub account.
  • A GitHub repository containing your application.
  • A basic understanding of IAM roles and policies.
  • A deployment target, such as an S3 bucket.
  • A GitHub Actions workflow.
For this tutorial, we will use a React or Vite frontend that generates a production build in the  dist  directory and deploy that build to an existing S3 bucket.
The example uses the Asia Pacific (Mumbai) Region:  ap-south-1 .
You can replace the region and resource names with your own configuration.

6. Step 1: Create a GitHub OIDC Identity Provider in AWS

First, AWS must recognize GitHub as a trusted identity provider.
Follow these steps:
  1. Sign in to the AWS Management Console.
  1. Open the IAM service.
  1. Navigate to Identity providers.
  1. Choose Add provider.
  1. Select OpenID Connect as the provider type.
  1. Enter the provider URL: https://token.actions.githubusercontent.com 
  1. Set the audience to: sts.amazonaws.com 
  1. Create the identity provider.
If the GitHub OIDC provider already exists in your AWS account, verify its configuration rather than creating a duplicate.
You have now established the identity-provider relationship between GitHub and AWS IAM.

7. Step 2: Create an IAM Role for GitHub Actions

The next step is to create an IAM role that GitHub Actions can assume.
Open IAM → Roles → Create role.
Choose the Web identity trusted entity type and select the GitHub OIDC provider.
Configure the audience as:
 sts.amazonaws.com 
Restrict the trusted GitHub organization or owner, repository, and branch. Do not leave the repository and branch unrestricted.
Use a meaningful role name, such as:
 GitHubActionsDeployRole 
The role needs two different policy configurations:
  • A trust policy that decides which GitHub workflow can assume the role.
  • A permissions policy that decides what that role can do in AWS.
These two policies solve different problems.

8. Step 3: Configure the IAM Trust Policy

The trust policy is one of the most important parts of this configuration.
It determines which GitHub Actions identities are allowed to assume the role.
Here is an example using the immutable organization and repository ID subject format used by newer GitHub repositories.
Replace the placeholders with your actual values.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::<AWS_ACCOUNT_ID>: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:<OWNER>@<OWNER_ID>/<REPOSITORY>@<REPOSITORY_ID>:ref:refs/heads/main"
}
}
}
]
}
For repositories using the older default subject format, the subject can instead look like:
 repo:<OWNER>/<REPOSITORY>:ref:refs/heads/main 
GitHub introduced the immutable ID suffix format for newly created repositories on July 15, 2026. Older repositories omit those suffixes unless they opt into the newer format. GitHub environment-based workflows can also use a different subject format.
Do not blindly copy either example. Your trust policy's subject must match the actual OIDC subject claim for your workflow. See the official GitHub and AWS documentation for the applicable format.
Why is this important?
Suppose your repository is  my-company/my-portfolio  and you only want deployments from the main branch.
The trust policy should not allow every GitHub repository or every branch to assume your deployment role.
Restricting the subject claim helps prevent unauthorized workflows from obtaining AWS credentials.

9. Step 4: Give the IAM Role Only the Required Permissions

Now configure what the role can do after it is assumed.
For our example, GitHub Actions needs to upload frontend build files to a dedicated S3 deployment bucket.
The following is an illustrative IAM permissions policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListDeploymentBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME"
},
{
"Sid": "ManageDeploymentObjects",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
}
]
}
Replace  YOUR_BUCKET_NAME  with the actual bucket name.
Why are these permissions needed?
  •  s3:ListBucket  allows the deployment command to inspect the destination bucket.
  •  s3:PutObject  allows the workflow to upload files.
  •  s3:DeleteObject  supports cleanup when using the  --delete  option in the synchronization command.
Avoid attaching AdministratorAccess to the deployment role.
If your application requires different permissions, define those permissions according to the actual deployment process.
If the bucket is shared with other applications, scope the permissions and deployment destination to a dedicated prefix instead of granting access to every object.

10. Step 5: Configure GitHub Actions

Now open your GitHub repository.
Navigate to:
 Repository → Actions → New workflow 
Create a workflow file at:
 .github/workflows/deploy.yml 
Add this example:
name: Deploy to AWS using OIDC

on:
push:
branches:
- main

permissions:
contents: read

jobs:
deploy:
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v7.0.1

- name: Set up Node.js
uses: actions/setup-node@v7.1.0
with:
node-version: "22"

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm test --if-present

- name: Build application
run: npm run build

- name: Configure AWS credentials using OIDC
uses: aws-actions/configure-aws-credentials@v6.3.0
with:
role-to-assume: arn:aws:iam::<AWS_ACCOUNT_ID>:role/GitHubActionsDeployRole
aws-region: ap-south-1

- name: Verify AWS identity
run: aws sts get-caller-identity

- name: Deploy to S3
run: |
aws s3 sync ./dist/ s3://YOUR_BUCKET_NAME/ --delete
The example uses the official GitHub checkout, Node.js setup, and AWS credentials actions.
For a project using another frontend framework or package manager, adjust the build commands and output directory accordingly.

Understand the important workflow permissions

 contents: read  allows the workflow to check out repository contents.
 id-token: write  allows the workflow to request an OIDC token from GitHub.
An important clarification:  id-token: write  does not give the workflow general write access to GitHub repositories or automatically grant permissions in AWS.
AWS access is controlled separately by the IAM role and its permission policies.
The AWS credentials action exchanges the OIDC token for temporary credentials through the configured role.
For high-assurance production repositories, consider pinning third-party actions to verified full commit SHAs and updating those pins deliberately.

11. Step 6: Verify the Deployment

Once your workflow file is committed to the main branch, GitHub Actions should start the workflow.
Open your repository's Actions tab.
You should be able to inspect the individual steps:
  1. Checkout repository.
  1. Set up Node.js.
  1. Install dependencies.
  1. Run tests.
  1. Build application.
  1. Configure AWS credentials.
  1. Verify AWS identity.
  1. Deploy to S3.
The command:
 aws sts get-caller-identity 
helps verify which AWS identity the workflow is using.
If the role assumption is successful, the output should identify the assumed IAM role session.
After deployment, check the target S3 bucket and verify that the latest build files were uploaded.
If you use CloudFront in front of the S3 bucket, also verify that the distribution serves the updated content. An additional cache invalidation may be required depending on your caching strategy.
A successful workflow is not enough. Verify that the deployed application actually works.

12. Common Errors and How to Fix Them

Error 1: Not authorized to perform sts

Possible reasons:
  • Incorrect OIDC provider ARN.
  • Incorrect audience.
  • Trust policy subject does not match the GitHub workflow.
  • Incorrect organization, repository, or branch configuration.
Check the OIDC provider and the trust policy conditions carefully.

Error 2: Could not assume role with web identity

Verify:
  • The workflow includes  id-token: write .
  • The AWS role ARN is correct.
  • GitHub's OIDC provider exists in IAM.
  • The role trusts the correct GitHub identity.

Error 3: AccessDenied when uploading to S3

Successful role assumption does not mean the role can access every AWS resource.
Check the IAM permissions policy, target bucket ARN, object ARN, and any applicable bucket or encryption-key policies.

Error 4: Build directory does not exist

Check the output directory generated by your application.
For example, a Vite application commonly produces  dist , while other frameworks may use different build directories.
Verify the build step before running the S3 synchronization command.

Error 5: Deployment works on one branch but fails on another

Your trust policy may only permit the main branch.
That is intentional when you want to restrict production deployments. Configure the appropriate role and trust conditions for other deployment environments rather than making the production role accessible to every branch.

13. Security Best Practices

A working pipeline is not automatically a secure pipeline.
Follow these practices:
  1. Avoid long-lived AWS credentials: Prefer OIDC and temporary credentials for GitHub Actions.
  1. Restrict the trust policy: Allow only the intended GitHub repository and branch or protected environment.
  1. Apply least privilege: Give the deployment role only the permissions required for the deployment.
  1. Protect the production branch: Require reviews and tests before merging deployment changes.
  1. Use protected environments: Add approval requirements and deployment-branch restrictions for production workflows.
  1. Trust your workflow dependencies: Review third-party actions and pin them to verified commit SHAs where appropriate.
  1. Avoid untrusted pull-request deployments: Do not let untrusted code obtain a production deployment role.
  1. Monitor activity: Review AWS CloudTrail events and GitHub Actions execution history.
OIDC improves credential management, but it does not eliminate every security risk. A compromised workflow with permission to assume a powerful role can still misuse that role's permissions.
Security depends on the entire pipeline, not just the authentication method.

Final Thoughts

CI/CD is an important part of modern cloud engineering, but authentication should not become an afterthought.
GitHub Actions and AWS OIDC provide a way to establish a trust relationship between your repository workflows and your AWS account without depending on permanent AWS access keys stored in GitHub Secrets.
The key concepts are simple:
  • GitHub provides an identity token.
  • AWS IAM verifies whether the identity is trusted.
  • AWS STS issues temporary credentials.
  • IAM policies determine what the workflow can access.
Start with a small deployment, restrict the permissions, test the workflow, and inspect the resulting AWS activity.
A good CI/CD pipeline does more than deploy code automatically. It deploys code securely, consistently, and with only the permissions it needs.

Let's Discuss

Imagine you maintain a GitHub repository that automatically deploys an application to AWS.
Would you continue using IAM access keys stored in GitHub Secrets, or switch to OIDC with an IAM role? What security concern matters most to you?
Share your experience, implementation challenges, or questions in the comments. Please never post real AWS keys, account credentials, or private tokens.

Further Reading

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article