Guide to Setting Up OpenID Connect (OIDC) for AWS and GitHub Actions
Overview
OpenID Connect (OIDC) enables secure and temporary credential-based authentication between GitHub Actions and AWS without the need for long-term credentials.
Step 1: Create an IAM OIDC Identity Provider
-
Log in to the AWS Management Console:
- Navigate to IAM > Identity Providers.
-
Add an OIDC Provider:
- Choose Add Provider.
- Select Provider Type as
OpenID Connect
. - Enter the Provider URL:
https://token.actions.githubusercontent.com
- Add Audience:
sts.amazonaws.com
-
Save the Identity Provider:
- Review and confirm the details.
Step 2: Create an IAM Role for GitHub Actions
-
Create a New Role:
- Go to Roles > Create Role.
- Select Web Identity as the trusted entity type.
- Choose the OIDC provider you just created.
-
Define Conditions:
- Add a condition to restrict access to specific repositories and branches using the following trust policy:
{ "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>/<REPO>:ref:refs/heads/<BRANCH>" } } } ] }
- Replace
<AWS_ACCOUNT_ID>
,<OWNER>
,<REPO>
, and<BRANCH>
with your AWS account ID, GitHub organization or user name, repository name, and branch, respectively.
- Add a condition to restrict access to specific repositories and branches using the following trust policy:
-
Attach Permissions to the Role:
- Assign a policy to define what actions the role can perform (e.g., S3 access):
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:*", "Resource": "arn:aws:s3:::<BUCKET_NAME>/*" } ] }
- Replace
<BUCKET_NAME>
with your target S3 bucket name.
- Assign a policy to define what actions the role can perform (e.g., S3 access):
Step 3: Configure Your GitHub Actions Workflow
-
Set Permissions for OIDC: In your GitHub Actions workflow YAML file, add the following permissions:
permissions: id-token: write contents: read
-
Use the
aws-actions/configure-aws-credentials
Action: Configure AWS credentials using theaws-actions/configure-aws-credentials
action:- name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::<AWS_ACCOUNT_ID>:role/<ROLE_NAME> aws-region: us-east-1
- Replace
<AWS_ACCOUNT_ID>
and<ROLE_NAME>
with your AWS account ID and the name of the IAM role created in Step 2.
- Replace
-
Add Deployment Steps: Add subsequent steps to deploy your AWS resources, such as:
- name: Deploy Application run: cdk deploy -- all
Step 4: Test the Workflow
- Push changes to the configured branch.
- Monitor the workflow run in the Actions tab on GitHub.
- Verify AWS resource deployment and ensure the workflow runs successfully.
References
- AWS Documentation: Creating OpenID Connect Identity Providers
- GitHub Actions: About Security Hardening with OIDC
- AWS Actions for GitHub
This document provides a complete guide for setting up OIDC between GitHub Actions and AWS. Keep it handy for future projects or troubleshooting!