CI/CD pipelines are among the most privileged identities in any AWS organisation. They create infrastructure, change IAM, and deploy to production—often with a long-lived access key attached to an admin user, stored as a CI secret for years. If that key leaks through a log, a compromised dependency, or a malicious pull request, the attacker inherits everything the pipeline can do. This guide replaces standing admin keys with short-lived, scoped, auditable access using GitHub OIDC, separate plan and apply roles, and guardrails that cap the damage any single role can do.
The goals
- No long-lived AWS access keys stored in CI.
- Credentials that work only for a specific repository, branch, or deployment environment.
- Read-only access for plans and pull requests; write access only after approval.
- A hard ceiling on what any pipeline role can do, even if its policy is too broad.
- Every action traceable back to a commit and a run.
1. Trust GitHub's OIDC provider
GitHub Actions can issue a signed OpenID Connect token for each job. AWS STS exchanges that token for temporary credentials if the token matches an IAM role's trust policy. Register GitHub's OIDC provider once per account.
resource "aws_iam_openid_connect_provider" "github" {
url = "https://token.actions.githubusercontent.com"
client_id_list = ["sts.amazonaws.com"]
# Recent AWS provider versions no longer require thumbprint_list for this
# provider; older versions need GitHub's published thumbprint here.
}2. Write trust policies that check the subject
The token's subject claim identifies exactly what is running: the repository plus a branch, tag, pull request, or deployment environment. The most common and most dangerous mistake is a trust policy that checks only the audience—or uses a wildcard such as repo:my-org/*—which lets any repository in the organisation, or any branch, assume the role. Match the subject precisely.
data "aws_iam_policy_document" "plan_trust" {
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"]
}
# Any branch or PR in this one repository may plan (read-only)
condition {
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:my-org/platform-infra:*"]
}
}
}
data "aws_iam_policy_document" "apply_trust" {
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"]
}
# Only jobs running in the protected "production" environment
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:my-org/platform-infra:environment:production"]
}
}
}How does your cloud measure up?
Score your setup across foundation, delivery, reliability, security, and cost in 3 minutes—instant results, no email needed.
3. Separate plan from apply
Planning needs to read almost everything but change nothing. Applying needs to change things but should happen only after review. Splitting these into two roles means a malicious pull request can at worst read resource configuration, never modify it. Configure the production environment in GitHub with required reviewers, so the apply job—and therefore the apply role—cannot run until a person approves it.
name: terraform
on:
pull_request:
push:
branches: [main]
permissions:
id-token: write
contents: read
pull-requests: write
jobs:
plan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::111122223333:role/gha-platform-plan
aws-region: ap-south-1
- uses: hashicorp/setup-terraform@v3
- run: terraform init -input=false
- run: terraform plan -input=false -out=tfplan
apply:
if: github.ref == 'refs/heads/main'
needs: plan
runs-on: ubuntu-latest
environment: production # required reviewers gate this job
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::111122223333:role/gha-platform-apply
aws-region: ap-south-1
- uses: hashicorp/setup-terraform@v3
- run: terraform init -input=false
- run: terraform apply -input=false -auto-approveIn practice, save the plan file as a workflow artefact and apply exactly that plan, so what was reviewed is what gets applied. Pin third-party actions to a full commit SHA rather than a tag, because the workflow runs with the role's permissions.
4. Scope permissions to what the pipeline really does
The plan role can usually start from the AWS-managed ReadOnlyAccess policy plus read access to the Terraform state bucket and lock table. The apply role is harder: it needs write access to the services the repository manages, and nothing else. Split large estates into several repositories or workspaces with their own roles—networking, data, Kubernetes, and application deploys—so each role's scope stays small enough to reason about.
IAM Access Analyzer can generate a policy from the role's actual CloudTrail activity. Run the pipeline with a broader policy in a non-production account for a few weeks, generate a policy from what was used, review it, and replace the broad policy. Repeat periodically and remove permissions that are never used.
5. Cap every role with a permissions boundary
Pipelines that manage IAM are a privilege-escalation risk: a role that can create roles can create one more powerful than itself. A permissions boundary sets the maximum permissions a role can ever have, regardless of its policies, and you can require that any role the pipeline creates carries the same boundary.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowManagedServices",
"Effect": "Allow",
"Action": ["ec2:*", "eks:*", "ecr:*", "s3:*", "rds:*", "logs:*", "cloudwatch:*", "iam:PassRole"],
"Resource": "*"
},
{
"Sid": "CreatedRolesMustCarryBoundary",
"Effect": "Allow",
"Action": ["iam:CreateRole", "iam:PutRolePermissionsBoundary"],
"Resource": "*",
"Condition": {
"StringEquals": {
"iam:PermissionsBoundary": "arn:aws:iam::111122223333:policy/pipeline-boundary"
}
}
},
{
"Sid": "DenyBoundaryTampering",
"Effect": "Deny",
"Action": [
"iam:DeleteRolePermissionsBoundary",
"iam:CreatePolicyVersion",
"iam:DeletePolicy",
"iam:SetDefaultPolicyVersion"
],
"Resource": "arn:aws:iam::111122223333:policy/pipeline-boundary"
}
]
}6. Add organisation-level guardrails
Service control policies in AWS Organizations apply to every principal in an account, including pipeline roles. Use a short list of SCPs to block actions no pipeline should ever take: disabling CloudTrail or GuardDuty, leaving approved regions, deleting the organisation's logging resources, or creating IAM users with access keys. SCPs are the last line of defence when everything else is misconfigured.
7. Monitor and keep a break-glass path
- Alert on pipeline roles being assumed from unexpected subjects, at unusual times, or failing trust-policy checks repeatedly.
- Alert on IAM changes made outside the pipeline in production accounts.
- Add the commit SHA and run ID as session tags or role session names, so CloudTrail entries trace back to a specific run.
- Keep a separate break-glass role for humans, protected by MFA and alerting, and test it quarterly—pipelines should never be the only way to fix production.
- Delete any remaining IAM users with access keys once pipelines have migrated, and block their creation with an SCP.
Migration plan from static keys
- Inventory pipelines and the access keys they use; check CloudTrail for what each key actually does.
- Register the GitHub OIDC provider and create a read-only plan role per repository.
- Move pull-request plans to the plan role and confirm they work.
- Create apply roles scoped to each repository's services, with a permissions boundary.
- Protect production environments with required reviewers and switch apply jobs to OIDC.
- Deactivate the old access keys, watch for failures for a week, then delete them.
- Generate tightened policies from CloudTrail with Access Analyzer and repeat every quarter.
Least privilege for pipelines is not a one-time policy; it is a set of defaults that make the safe path the easy one. Once OIDC, split roles, and boundaries are in place, adding a new repository takes minutes and no secret ever needs rotating. This is part of what we set up in our Secure Delivery Pipeline and Cloud Landing Zone modules.
Key takeaways
- CI/CD roles should deploy specific stacks—not AdministratorAccess.
- Prefer OIDC federation from GitHub/GitLab over long-lived access keys.
- Separate plan/apply and environment roles to limit blast radius.
FAQ
How do we start shrinking CI permissions?
Capture what the pipeline actually uses via access advisor or CloudTrail, then replace wildcards with resource-scoped actions and conditions.
Should each repo have its own deploy role?
Usually yes for production. Shared mega-roles turn every repository compromise into an estate-wide incident.