CI/CD · · 6 min read
Keyless CI/CD: deploy to AWS from GitLab or GitHub with OIDC
By Rushit Chandegara, Founder & CTO, InfraCloudEdge
Most pipelines that deploy to AWS still hold an IAM user's access key in a CI variable. That key never expires, often has more permissions than it needs, and anyone who can read the project settings or print the variables can copy it. OpenID Connect (OIDC) removes the key altogether.
How it works
Your CI platform signs a short-lived token for each job that says which project, branch and environment it is running for. AWS trusts that platform as an identity provider, checks the token against an IAM role's trust policy, and returns temporary credentials that expire, by default, after an hour.
Set it up in AWS
- Add an IAM OIDC identity provider for your CI platform: gitlab.com (or your self-managed GitLab URL) or token.actions.githubusercontent.com.
- Create an IAM role for deployments with only the permissions the pipeline needs.
- Restrict the role's trust policy to specific projects and branches using the token's sub claim.
The sub condition is the important part. Without it, any project on the same platform could assume your role.
// GitLab: only the main branch of one project
"Condition": {
"StringEquals": { "gitlab.com:aud": "https://gitlab.com" },
"StringLike": { "gitlab.com:sub": "project_path:your-group/your-project:ref_type:branch:ref:main" }
}
// GitHub: only the main branch of one repository
"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" }
}Use it in the pipeline
In GitLab CI, request a token with the id_tokens keyword and exchange it with aws sts assume-role-with-web-identity. In GitHub Actions, give the job permission id-token: write and use the aws-actions/configure-aws-credentials action with role-to-assume.
Lock it down further
- Use a separate role per environment, so a feature branch can never deploy to production.
- Protect the branches and environments the roles trust, so only approved changes reach them.
- Require a manual approval step before production jobs run.
- Delete the old IAM user and its access keys once the pipeline works, and check CloudTrail to confirm nothing else used them.
What you get
No long-lived secrets to rotate or leak, a CloudTrail record of every deployment tied to the pipeline that ran it, and an access model an auditor can follow. For teams working towards SOC 2, it removes one of the most common findings in one change.
