Automating S3 Deployments with GitHub Actions & AWS OIDC
Automate React deployments to S3 & CloudFront via GitHub Actions and AWS OIDC without storing long-lived access keys.
In my previous guide, we covered hosting a production React SPA using Amazon S3 and CloudFront. But running
npm run build and manually dragging files into the AWS S3 Console every time you push code isn't realistic for a real production workflow.The traditional workaround was creating an IAM user, generating static
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY credentials, and pasting them into GitHub Secrets.However, long-lived access keys are a major security risk. If leaked or forgotten, they can lead to unauthorized access.
In this tutorial, we will set up automated CI/CD to build, deploy, and invalidate your CloudFront cache using GitHub Actions with AWS OpenID Connect (OIDC)—meaning zero stored AWS credentials in GitHub.
Architecture Flow
- Developer pushes code to the
mainbranch on GitHub.
- GitHub Actions requests a short-lived OIDC token from GitHub's token authority.
- GitHub Actions assumes a dedicated AWS IAM Role using that token.
- AWS verifies the repository identity and grants temporary AWS STS credentials (valid for only 1 hour).
- GitHub runner builds the app, syncs artifacts to Amazon S3, and creates an Amazon CloudFront cache invalidation.
Step 1: Configure OIDC in AWS IAM
First, we need to allow GitHub to authenticate with your AWS account.
1.1 Add the GitHub Identity Provider
- Open the AWS IAM Console →\rightarrow→ Identity providers →\rightarrow→ Add provider.
- Select OpenID Connect.
- Provider URL:
https://token.actions.githubusercontent.com
- Audience:
sts.amazonaws.com
- Click Add provider.
1.2 Create the IAM Role for GitHub Actions
- In IAM, go to Roles →\rightarrow→ Create role.
- Select Custom trust policy and paste:
json{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Federated": "arn:aws:iam::<YOUR_ACCOUNT_ID>:oidc-provider/token.actions.githubusercontent.com" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": { "token.actions.githubusercontent.com:aud": "sts.amazonaws.com" }, "StringLike": { "token.actions.githubusercontent.com:sub": "repo:<GITHUB_USERNAME>/<REPO_NAME>:ref:refs/heads/main" } } } ]}(Replace
<YOUR_ACCOUNT_ID> , <GITHUB_USERNAME> , and <REPO_NAME> with your details).1.3 Attach Permissions Policy
Attach an inline policy allowing the role to sync to S3 and invalidate CloudFront:
json{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject", "s3:ListBucket", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::<YOUR_BUCKET_NAME>", "arn:aws:s3:::<YOUR_BUCKET_NAME>/*" ] }, { "Effect": "Allow", "Action": [ "cloudfront:CreateInvalidation" ], "Resource": "arn:aws:cloudfront::<YOUR_ACCOUNT_ID>:distribution/<YOUR_DISTRIBUTION_ID>" } ]}Copy the Role ARN (e.g.,
arn:aws:iam::123456789012:role/github-actions-deploy-role ).
Step 2: Create the GitHub Actions Workflow
In your GitHub repository, create a workflow file at
.github/workflows/deploy.yml :yamlname: Deploy Frontend to AWSon: push: branches: - mainpermissions: id-token: write # Required for requesting the JWT OIDC token contents: read # Required for actions/checkoutjobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v4 - name: Set up Node.js uses: actions/setup-node@v4 with: node-version: 20 cache: 'npm' - name: Install Dependencies & Build run: | npm ci npm run build - name: Configure AWS Credentials via OIDC uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} aws-region: us-east-1 - name: Sync Build Output to S3 run: | aws s3 sync dist/ s3://${{ secrets.S3_BUCKET_NAME }} --delete - name: Invalidate CloudFront Cache run: | aws cloudfront create-invalidation \ --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} \ --paths "/*"
Step 3: Add Repository Secrets in GitHub
Navigate to your GitHub Repository →\rightarrow→ Settings →\rightarrow→ Secrets and variables →\rightarrow→ Actions and add these three repository secrets:
-
AWS_ROLE_ARN: The IAM Role ARN created in Step 1.
-
S3_BUCKET_NAME: Name of your target S3 bucket.
-
CLOUDFRONT_DISTRIBUTION_ID: Your CloudFront distribution ID.
Notice that none of these are private keys or passwords—even if an ARN is visible, it cannot be assumed without GitHub's signed OIDC token!
Step 4: Verification & Testing
Make a small change to your frontend code and push to
main :bashgit commit -m "feat: trigger automated deployment"git push origin mainHead over to the Actions tab in GitHub. You’ll see the workflow execute:
- Authenticates seamlessly via AWS STS without storing keys.
- Deploys the latest bundle to S3.
- Flushes the edge cache so users worldwide immediately see the new release.
Key Takeaways
- Zero Long-Lived Credentials: If your GitHub account is ever compromised, there are no static AWS secret keys to revoke or leak.
- Least Privilege Principle: Scoping the IAM trust policy to a specific repository and branch (
ref:refs/heads/main) prevents arbitrary forks from assuming your production role.
- Instant Cache Propagation: Automating the CloudFront cache invalidation guarantees that users never receive stale bundles after a deployment.
Join the Discussion:
Are you already using GitHub OIDC with AWS, or are you still relying on static IAM access keys in your repositories? Let me know in the comments below!
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article