OpenID Connect (OIDC) enables GitHub Actions to authenticate with AWS without relying on long-lived credentials. By generating short-lived, temporary tokens for each workflow run, OIDC strengthens security and simplifies credential management, eliminating the need to store sensitive secrets in your repository.
OIDC is the most secure and recommended method for authenticating GitHub Actions with AWS. It removes the risk of exposing AWS access keys by replacing them with automatically generated, short-lived tokens. This approach minimizes credential exposure while ensuring seamless, secure access to AWS resources.
To allow AWS to trust GitHub as an identity provider, you need to create an IAM OIDC identity provider in your AWS account.
Go to the IAM Console , select Identity providers, and click Add provider.
https://token.actions.githubusercontent.com.sts.amazonaws.com.Click Add provider.
Go to Roles and click Create role.
sts.amazonaws.com.* for all repositories).* for all branches, I recommend to use main to enforce least privilege).
Attach the necessary policies to the role (AdministratorAccess for temporary testing, or a custom policy with least-privilege permissions for production).
Restrict which GitHub repositories and branches can assume this role. I recommend you explicitly specify the branches you want to allow, main and dev for example. You should get a similar policy as below:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRoleWithWebIdentity",
"Principal": {
"Federated": "arn:aws:iam::<ACCOUNT_ID>:oidc-provider/token.actions.githubusercontent.com"
},
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": [
"<REPOSITORY>"
]
}
}
}
]
}
<ACCOUNT_ID> should be your AWS account ID and <REPOSITORY> the Github repository. The repository must be in the form repo:ORG-NAME/REPO-NAME:ref:refs:/heads/BRANCH or repo:ORG-NAME/REPO-NAME:* to allow any branch.Name the role GitHubActionsRole and create it.
Once your AWS Account is set up, let's create a workflow file .github/workflows/deploy.yml in your repository as follows:
name: Verify AWS OIDC
on:
push:
branches:
- main
permissions:
id-token: write # This is required for GitHub to request a JWT
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::<ACCOUNT_ID>:role/GitHubActionsRole
aws-region: us-east-1
- name: Verify AWS identity
run: aws sts get-caller-identity
<ACCOUNT_ID> with your AWS account ID.aws-actions/configure-aws-credentials action handles the OIDC token exchange and configures the AWS CLI.Commit the workflow file to the main branch. The workflow will run automatically.
In the GitHub Actions log, you should see the output of aws sts get-caller-identity, confirming that the workflow has successfully assumed the IAM role previously created.
Now that you have successfully linked your AWS account to your GitHub repository, be sure to respect these best practices:
If you encounter any issues when running your workflow, it might come from:
id-token: write permission is set and the OIDC provider is correctly configured in AWS.