AWS Builder Center

Day 4: AWS IAM & OIDC for Secure CI/CD Authentication

The focus was on replacing long-lived AWS access keys with short-lived, temporary credentials through OIDC federation.

Modern DevOps is not only about automation. Security must be part of the automation.
As part of my AWS DevOps Community Builders learning journey, Day 4 focused on understanding how to securely connect GitLab CI/CD with AWS using AWS IAM and OpenID Connect (OIDC).
The main goal was to understand how a CI/CD pipeline can access AWS resources without storing long-lived AWS access keys.



🚀 The Problem with Long-Lived AWS Access Keys

A traditional CI/CD setup might store:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
as GitLab CI/CD variables.
While this can work, long-lived credentials create additional security risk.
If credentials are exposed, they may remain valid until they are rotated or revoked.
For automated pipelines, a better approach is to use federated identity with OIDC.
AWS IAM OIDC federation allows an external identity provider to establish trust with AWS and obtain temporary security credentials.



🔐 What is AWS IAM?

AWS Identity and Access Management (IAM) controls access to AWS resources.
IAM allows us to define:
  • Who can access AWS
  • What resources they can access
  • Which actions they can perform
  • Under what conditions access is allowed
For CI/CD, an important IAM concept is the IAM Role.
Unlike an IAM user, an IAM role does not have permanent credentials of its own. An external identity can assume the role when the trust policy allows it.



🌐 What is OIDC?

OpenID Connect (OIDC) is an identity layer built on OAuth 2.0.
In a CI/CD environment, GitLab can issue an OIDC ID token for a job.
The token contains claims describing the workload, such as:
  • Issuer ( iss )
  • Subject ( sub )
  • Audience ( aud )
  • Project information
  • Pipeline-related information
GitLab CI/CD supports ID tokens through the  id_tokens  keyword.



🏗️ GitLab → AWS OIDC Architecture

The overall authentication flow looks like this:
Developer
|
| git push
v
GitLab Repository
|
v
GitLab CI/CD
|
v
GitLab Runner
|
| OIDC ID Token
v
AWS IAM OIDC Provider
|
| Trust Policy Validation
v
AWS IAM Role
|
| AssumeRoleWithWebIdentity
v
AWS STS
|
| Temporary Credentials
v
AWS Resources
The important point is that the pipeline doesn't need to store a permanent AWS secret key.
AWS verifies the OIDC token and, when the role's trust conditions are satisfied, temporary credentials can be issued through AWS STS.



1. Create the IAM OIDC Provider

The first step is establishing trust between GitLab and AWS.
For GitLab.com, the OIDC provider is associated with:
https://gitlab.com
AWS IAM supports creating OIDC identity providers through the AWS Console, CLI, or API.
The provider configuration also includes an audience, which should match the audience expected by the AWS integration.



2. Create an IAM Role

Next, create an IAM role that GitLab CI/CD is allowed to assume.
Conceptually:
GitLab OIDC Provider
|
| Trust
v
AWS IAM Role
|
| Permissions
v
AWS Resources
The role should contain only the permissions required by the pipeline.
For example, a deployment pipeline might require access to:
Amazon ECR
Amazon EKS
Amazon S3
Terraform-related AWS resources
The exact permissions should depend on the job.



3. Trust Policy

The trust policy determines who is allowed to assume the role.
A simplified example is:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::<ACCOUNT_ID>:oidc-provider/gitlab.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"gitlab.com:aud": "sts.amazonaws.com",
"gitlab.com:sub": "project_path:mygroup/myproject:ref_type:branch:ref:main"
}
}
}
]
}
The important part is the conditions.
We don't want every GitLab project or branch to be able to assume the role.
GitLab's current AWS guidance recommends using stable identifiers such as  namespace_id  and  project_id  alongside claims such as  sub  where supported, so trust policies are less dependent on project paths that can change.



4. GitLab CI/CD ID Token

The GitLab job can request an OIDC ID token using  id_tokens .
For example:
deploy:
stage: deploy
id_tokens:
GITLAB_OIDC_TOKEN:
aud: sts.amazonaws.com
script:
- echo "Authenticating with AWS using OIDC..."
GitLab generates the token for the job, and the token can then be presented to AWS for federation.



5. Assume the AWS IAM Role

The token can be exchanged with AWS STS using:
AssumeRoleWithWebIdentity
Conceptually:
GitLab ID Token
|
v
AWS STS
|
| Validate OIDC token
|
v
IAM Role
|
v
Temporary Credentials
Those temporary credentials can then be used by AWS CLI, Terraform, or other AWS-aware tools.
GitLab's AWS integration documentation demonstrates this pattern for retrieving temporary credentials without storing AWS secrets in CI/CD variables.



6. Verify the Authentication

A simple verification command is:
aws sts get-caller-identity
If the authentication succeeds, AWS returns information about the identity being used.
This is extremely useful when troubleshooting CI/CD authentication.
For example:
GitLab Runner
|
v
OIDC Token
|
v
AWS IAM Role
|
v
AWS STS
|
v
aws sts get-caller-identity



🔄 Complete GitLab → AWS Workflow

Putting everything together:
Developer
|
| git push
v
GitLab
|
v
.gitlab-ci.yml
|
v
GitLab CI/CD
|
v
GitLab Runner
|
| Request ID Token
v
GitLab OIDC
|
v
AWS IAM OIDC Provider
|
| Validate claims
v
AWS IAM Role
|
v
AWS STS
|
| Temporary Credentials
v
AWS Resources
This is a much cleaner authentication model for modern CI/CD.



🛡️ Least Privilege

OIDC alone does not automatically make a pipeline secure.
The IAM role still needs properly designed permissions.
For example, if a pipeline only needs to push images to ECR, don't give it unrestricted administrator permissions.
Instead:
Pipeline
|
v
IAM Role
|
+── ECR permissions
For an infrastructure pipeline:
Pipeline
|
v
Terraform IAM Role
|
+── Required AWS permissions only
The principle is:
Give the pipeline only the permissions it needs to perform its job.



🔒 Why OIDC is Important for DevOps

OIDC provides several advantages:

1. No Long-Lived AWS Keys

The pipeline doesn't need permanent AWS access keys stored as project secrets.

2. Temporary Credentials

AWS STS can provide temporary credentials after successful federation.

3. Fine-Grained Trust

The IAM trust policy can restrict access based on GitLab claims such as project, namespace, branch, or other supported attributes.

4. Better CI/CD Security

Authentication becomes tied to the workload identity instead of a static secret.

5. Better Automation

The pipeline can securely interact with AWS without manually rotating long-lived credentials.



⚠️ OIDC Does Not Replace Authorization

One important distinction:
OIDC = Authentication / Federation
IAM Policies = Authorization
In simple terms:
OIDC
↓
Who are you?
IAM Policy
↓
What are you allowed to do?
Both are important.
A secure CI/CD architecture needs both.



🏗️ Using OIDC with Terraform

This becomes particularly useful when Terraform is running inside GitLab CI/CD.
The architecture can look like:
Developer
↓
GitLab
↓
GitLab Runner
↓
OIDC
↓
AWS IAM Role
↓
Terraform
↓
AWS Infrastructure
Terraform can then provision resources such as:
VPC
ECR
EKS
IAM
S3
Security Groups
RDS
without requiring a long-lived AWS access key to be embedded in the pipeline.



🔥 My Biggest Takeaway

The biggest lesson from Day 4 is that CI/CD security should be designed into the pipeline from the beginning.
Instead of:
GitLab
↓
Long-Lived AWS Access Keys
↓
AWS
we can build:
GitLab
↓
OIDC ID Token
↓
AWS IAM
↓
Trust Policy
↓
AWS STS
↓
Temporary Credentials
↓
AWS
This creates a stronger foundation for secure automation.



🚀 Where This Fits in My DevOps Journey

The previous days are now connecting together:
Day -1
AWS DevOps Foundation
↓
Day 1
Amazon EKS Fundamentals
↓
Day 2
GitLab CI/CD → AWS EKS
↓
Day 3
Terraform for AWS Infrastructure
↓
Day 4
AWS IAM & OIDC for CI/CD
The next step is to combine secure authentication with container delivery.

Day 5

Docker → Amazon ECR → EKS
The goal will be to understand how an application is containerized, pushed to Amazon ECR, and deployed to Amazon EKS through an automated CI/CD workflow.



Final Thoughts

DevOps automation without security can create a bigger problem faster.
That's why secure identity should be part of the pipeline architecture.
My Day 4 takeaway:
No long-lived keys → OIDC → IAM Role → Temporary Credentials → Least Privilege → Secure CI/CD
Learn → Build → Secure → Automate → Deploy → Improve
#AWS #AWSCommunityBuilders #AWSIAM #OIDC #GitLab #GitLabCICD #DevOps #DevSecOps #Terraform #AmazonEKS #CloudSecurity #CICD #CloudComputing #LearningInPublic
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