
Secretless CI/CD on AWS: GitHub Actions OIDC, Scoped IAM Roles, and ECR — Zero Static Keys, Anywhere in the Pipeline
A complete, hands-on guide to secretless CI/CD on AWS — GitHub Actions OIDC, scoped IAM roles, and ECR, with zero static keys anywhere in the pipeline.
DevOps Engineer | AWS Community Builder
What You'll Build
- An IAM OIDC trust relationship that lets only your repo/branch assume a role — no access keys stored anywhere.
- A least-privilege IAM role scoped to one ECR repository, used only by CI.
- An EC2 instance profile so the server also never holds a static key (the half most tutorials skip).
- A working GitHub Actions pipeline that builds, pushes to ECR with working layer caching, and deploys over SSH.
Prerequisites
- An AWS account with permission to create IAM roles/policies and an OIDC provider
- An existing ECR repository (or permission to create one)
- An EC2 instance you can SSH into, running Docker + Docker Compose
- A GitHub repo with Actions enabled
- AWS CLI configured locally with an admin/break-glass identity (only to run the one-time setup commands below)

Step 1 — Register GitHub as an OIDC Identity Provider in IAM
This is a one-time, account-level step. Most accounts that already use any GitHub+AWS integration have this provider — check IAM → Identity providers before creating a duplicate.
aws iam create-open-id-connect-provider \
--url "https://token.actions.githubusercontent.com" \
--client-id-list "sts.amazonaws.com" \
--thumbprint-list "6938fd4d98bab03faadb97b34396831e3780aea1"Note: AWS validates the provider's TLS chain automatically now — the thumbprint is a legacy required field, not a real security control. The real control is the trust policy in Step 2.
Step 2 — Create a Scoped IAM Role for CI
2.1 Trust policy — scope this to one repo and one branch
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::<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:<your-org>/<your-repo>:ref:refs/heads/main"
}
}
}
]
}Important: The
sub condition is the actual access boundary — not the permission policy. Never use repo:org/* . If you run separate test/prod pipelines, create two roles, one per environment, so a compromised test pipeline can't touch production.2.2 Create the role
aws iam create-role \
--role-name GitHubActionsDeployRole \
--assume-role-policy-document file://trust-policy.json \
--max-session-duration 3600
Step 3 — Attach a Least-Privilege ECR Policy
{
"Version": "2012-10-17",
"Statement": [
{ "Sid": "ECRAuth", "Effect": "Allow", "Action": "ecr:GetAuthorizationToken", "Resource": "*" },
{
"Sid": "ECRPushPull",
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability", "ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage", "ecr:PutImage", "ecr:InitiateLayerUpload",
"ecr:UploadLayerPart", "ecr:CompleteLayerUpload"
],
"Resource": "arn:aws:ecr:us-east-1:<ACCOUNT_ID>:repository/myapp-*"
}
]
}aws iam put-role-policy \
--role-name GitHubActionsDeployRole \
--policy-name ECRDeployAccess \
--policy-document file://ecr-deploy-policy.jsonNote:
ecr:GetAuthorizationToken cannot be scoped to one repo — AWS requires Resource: "*" for that single action. Everything else should be scoped to your repo ARN pattern.
Step 4 — Make the EC2 Host Secretless Too (the part most guides skip)
Removing keys from CI is half the job. If the EC2 box pulling the image still has a static key in
~/.aws/credentials , you've just moved the risk, not removed it. Give the instance an IAM instance profile instead.4.1 Create a role for the EC2 host with the same scoped ECR-pull permissions, trusted by EC2
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}aws iam create-role --role-name EC2EcrPullRole --assume-role-policy-document file://ec2-trust-policy.json
aws iam put-role-policy --role-name EC2EcrPullRole --policy-name ECRPullOnly --policy-document file://ecr-deploy-policy.json
aws iam create-instance-profile --instance-profile-name EC2EcrPullProfile
aws iam add-role-to-instance-profile --instance-profile-name EC2EcrPullProfile --role-name EC2EcrPullRole
aws ec2 associate-iam-instance-profile \
--instance-id i-xxxxxxxxxxxxxxxxx \
--iam-instance-profile Name=EC2EcrPullProfileNow
aws ecr get-login-password run on the box itself works with no credentials file at all.
Step 5 — The GitHub Actions Workflow
name: Deploy to AWS
on:
push:
branches: [main]
permissions:
id-token: write # required — lets GitHub mint the OIDC token for this job
contents: read
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials via OIDC
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::<ACCOUNT_ID>:role/GitHubActionsDeployRole
aws-region: us-east-1
- uses: aws-actions/amazon-ecr-login@v2
- uses: docker/setup-buildx-action@v3
- name: Build & push image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ secrets.ECR_URI }}/myapp:latest
cache-from: type=registry,ref=${{ secrets.ECR_URI }}/myapp:buildcache
cache-to: type=registry,ref=${{ secrets.ECR_URI }}/myapp:buildcache,mode=max,image-manifest=true,oci-mediatypes=true
- name: Deploy to EC2
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${{ secrets.ECR_URI }}
docker compose pull && docker compose up -d --remove-orphansImportant — the gotcha nobody writes about: without
image-manifest=true,oci-mediatypes=true on cache-to , BuildKit writes a cache manifest format ECR rejects. The export fails as a non-fatal warning, the cache is silently never stored, and every single build becomes a cold build — with no error telling you why your 8-minute builds never got faster.

Security Deep Dive — What Is Actually Protecting You Here
It's worth being explicit about why this is more secure, not just that it is. Four separate security properties are doing the work, and each one closes a specific attack path that static keys leave open.
1. No long-lived secret exists to steal
A static
AWS_SECRET_ACCESS_KEY is a bearer credential — whoever holds the string has full access until someone notices and revokes it. OIDC replaces that with a JWT GitHub mints fresh for every single job run, which AWS STS exchanges for a session credential that expires in as little as 15 minutes (here, 1 hour via max-session-duration ). There is nothing sitting in GitHub Secrets for a leaked log, a malicious Action dependency, or a compromised maintainer account to exfiltrate and reuse later.2. The trust policy is the real access control — and it's conditional, not just identity-based
Classic IAM users answer "who are you?". The OIDC trust policy answers "who are you, from where, doing what?" The
sub condition encodes the GitHub org, repo, and branch; the aud condition pins the audience to sts.amazonaws.com . A token minted for a pull-request build on a fork, or for a different repo entirely, fails the trust evaluation before it ever reaches your permission policy. This is the single biggest security upgrade over static keys: the credential itself is scoped to a context, not just a name.3. Blast radius is contained on both sides of the pipeline
This is why Step 4 matters as much as Steps 1–3. Most write-ups stop once CI is secretless and leave a static key sitting in
~/.aws/credentials on the EC2 box — which just moves the same risk to a machine with a larger attack surface (open ports, installed packages, SSH access). By giving the instance its own IAM instance profile scoped to pull-only on the same repo pattern, a compromise of the CI role can't touch the server's AWS access, and a compromise of the server can't push malicious images or touch anything outside ECR pull. Two narrow blast radii instead of one wide one.4. Every credential use is individually auditable
Because each run mints its own session, every
AssumeRoleWithWebIdentity call is a distinct CloudTrail event carrying the GitHub repo and branch in its request parameters. With a static key, CloudTrail shows you an IAM user acted — it can't tell you which deploy, from which commit. With OIDC, you get a 1:1 mapping between AWS API activity and a specific workflow run, which is what makes incident response and compliance audits actually tractable.Threat-to-mitigation map
Attack Path | Closed By |
|---|---|
Leaked key in a workflow log or compromised Action dependency | No static key exists — only a token that expires within the hour |
Credential reused outside its intended context (wrong repo/branch/fork) | sub / aud conditions in the trust policy reject the assumption |
Over-permissioned CI role used to touch unrelated AWS resources | Permission policy scoped to one ECR repo ARN pattern, not Resource: "*" |
Compromised EC2 host used to pivot into AWS with a stored key | Instance profile instead of a credentials file — nothing on disk to steal |
Test pipeline compromise reaching production resources | Separate IAM roles per environment, not one shared role |
"Who deployed what, when" during an incident | CloudTrail AssumeRoleWithWebIdentity events are per-run and attributable |

A few notes before you publish:
- Replace every
<ACCOUNT_ID>,<your-org>/<your-repo>, and example ARNs with your real values.
- The Security Deep Dive and the EC2-instance-profile step are your strongest "dive deep" differentiators for Builder Center's quality bar — don't cut them if you need to shorten the post.
- Want me to draft the cover diagram (PNG export) or the Pulse form/Space submission blurb next?
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article