AWS Builder Center

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 modern cloud development, running  npm run build  and manually dragging files into the Amazon S3 Console isn't viable for production.
The traditional CI/CD workaround was creating an IAM user with 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 liability if leaked or unrotated.
In this guide, we automate building, deploying, and cache invalidation using GitHub Actions with AWS OpenID Connect (OIDC)—meaning zero stored AWS credentials in GitHub.



Architecture Flow

  1. Developer pushes to the  main  branch.
  1. GitHub Actions requests an ephemeral OIDC token from GitHub's token authority.
  1. GitHub Actions assumes a dedicated AWS IAM Role using that token.
  1. AWS verifies the repository identity via STS and grants temporary credentials (valid for 1 hour).
  1. The workflow builds the application, syncs artifacts to Amazon S3, and creates an Amazon CloudFront cache invalidation.



Step 1: Configure OIDC in AWS IAM

1.1 Add GitHub as an Identity Provider

  1. Open AWS IAM Console →\rightarrow→ Identity providers →\rightarrow→ Add provider.
  1. Select OpenID Connect.
  1. Provider URL:  https://token.actions.githubusercontent.com 
  1. Audience:  sts.amazonaws.com 
  1. Click Add provider.

1.2 Create the IAM Role

In IAM, create a role with a Custom trust policy:
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_USER>/<REPO>:ref:refs/heads/main" } } } ]}

1.3 Attach Permissions

Attach an inline policy allowing S3 syncing and CloudFront cache invalidation:
json{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject", "s3:ListBucket", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::<YOUR_BUCKET>", "arn:aws:s3:::<YOUR_BUCKET>/*" ] }, { "Effect": "Allow", "Action": [ "cloudfront:CreateInvalidation" ], "Resource": "arn:aws:cloudfront::<YOUR_ACCOUNT_ID>:distribution/<YOUR_DIST_ID>" } ]}



Step 2: The GitHub Actions Workflow

In your repository, create  .github/workflows/deploy.yml :
yamlname: Deploy Frontend to AWSon: push: branches: - mainpermissions: id-token: write contents: readjobs: 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 & 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 to S3 run: | aws s3 sync dist/ s3://${{ secrets.S3_BUCKET_NAME }} --delete - name: Invalidate CloudFront run: | aws cloudfront create-invalidation \ --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} \ --paths "/*"



Step 3: Add Repository Secrets

In GitHub →\rightarrow→ Settings →\rightarrow→ Secrets and variables →\rightarrow→ Actions, add:
  •  AWS_ROLE_ARN : Your IAM Role ARN
  •  S3_BUCKET_NAME : Your target S3 bucket name
  •  CLOUDFRONT_DISTRIBUTION_ID : Your CloudFront distribution ID



Key Benefits

  1. Zero Long-Lived Secrets: No static keys to leak or rotate.
  1. Strictly Scoped: Only pushes to the designated repository and branch can assume the deployment role.
  1. Automated Edge Flush: CloudFront cache invalidation ensures users instantly see the latest release worldwide.



Join the Discussion:

Are you using AWS OIDC for CI/CD yet, or are you still storing static access keys in GitHub Secrets? Let me know in the comments below!
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