TERRAFORM · quality-cd
Terraform: OIDC deploy role for a static site on S3 + CloudFront
Keyless GitHub Actions deploys: an OIDC role scoped to one repo and branch, plus the OAC bucket policy that keeps the origin private. The exact pattern that ships this site.
✓ verified 2026-06-29 · stack: terraform, aws, github-actions · source
This is how this site deploys: no long-lived AWS keys in GitHub, a private S3
origin only CloudFront can read, and a deploy role that only the main branch
of one repo can assume.
# Trust GitHub's OIDC issuer once per account.resource "aws_iam_openid_connect_provider" "github" { url = "https://token.actions.githubusercontent.com" client_id_list = ["sts.amazonaws.com"] thumbprint_list = ["6938fd4d98bab03faadb97b34396831e3780aea1"]}
# A role only the main branch of one repo can assume — no keys anywhere.data "aws_iam_policy_document" "deploy_assume" { statement { actions = ["sts:AssumeRoleWithWebIdentity"] principals { type = "Federated" identifiers = [aws_iam_openid_connect_provider.github.arn] } condition { test = "StringEquals" variable = "token.actions.githubusercontent.com:aud" values = ["sts.amazonaws.com"] } condition { test = "StringEquals" variable = "token.actions.githubusercontent.com:sub" values = ["repo:OWNER/REPO:ref:refs/heads/main"] } }}
resource "aws_iam_role" "deploy" { name = "static-site-deploy" assume_role_policy = data.aws_iam_policy_document.deploy_assume.json}
# Least privilege: write the bucket, invalidate the one distribution.data "aws_iam_policy_document" "deploy" { statement { actions = ["s3:ListBucket"] resources = [aws_s3_bucket.site.arn] } statement { actions = ["s3:PutObject", "s3:DeleteObject", "s3:GetObject"] resources = ["${aws_s3_bucket.site.arn}/*"] } statement { actions = ["cloudfront:CreateInvalidation"] resources = [aws_cloudfront_distribution.site.arn] }}
resource "aws_iam_role_policy" "deploy" { role = aws_iam_role.deploy.id policy = data.aws_iam_policy_document.deploy.json}
# Origin Access Control: the bucket stays private; only THIS distribution reads it.data "aws_iam_policy_document" "bucket" { statement { actions = ["s3:GetObject"] principals { type = "Service" identifiers = ["cloudfront.amazonaws.com"] } resources = ["${aws_s3_bucket.site.arn}/*"] condition { test = "StringEquals" variable = "AWS:SourceArn" values = [aws_cloudfront_distribution.site.arn] } }}
resource "aws_s3_bucket_policy" "site" { bucket = aws_s3_bucket.site.id policy = data.aws_iam_policy_document.bucket.json}The workflow side assumes the role with aws-actions/configure-aws-credentials
(needs permissions: id-token: write), then aws s3 sync and
cloudfront create-invalidation.
What it does
- Removes static AWS credentials from CI entirely: the runner mints a
short-lived token GitHub signs, and the
subcondition pins it to one repo and branch, so a fork or a feature branch cannot assume the role. - Keeps the origin bucket fully private: the
AWS:SourceArncondition means only the named CloudFront distribution can read objects, even though the bucket blocks all public access.
Failure modes
subscope drift.repo:OWNER/REPO:ref:refs/heads/maintrusts onlymain. If you deploy from tags or environments, widen it deliberately (ref:refs/tags/*, or anenvironment:sub), never torepo:OWNER/REPO:*, which trusts every branch and PR.- Thumbprint churn. AWS now validates GitHub’s OIDC against trusted CAs, so the thumbprint is largely vestigial, but the argument is still required; a stale value no longer breaks assumption on current accounts.
- OAC needs a modern bucket policy, not an OAI. Origin Access Identity is
the legacy mechanism; this
Serviceprincipal +AWS:SourceArnpolicy is the OAC form. Mixing them silently denies CloudFront.