Skip to main content
InsightsStep-by-Step Technical GuideLevel: Intermediate

Automating AWS S3 Deployments with GitLab CI/CD and Keyless OIDC Authentication

By Ebere Gideon Emmanuel3 min read · 653 words
Architecture diagram of GitLab CI/CD keyless OIDC authentication with AWS IAM and Amazon S3

Learn how to deploy to Amazon S3 using GitLab CI/CD and AWS OIDC without static access keys. Follow our secure step-by-step technical guide.

Deploying web assets to Amazon S3 via continuous integration often introduces security vulnerabilities when relying on long-lived AWS IAM access keys stored as repository secrets. This technical guide explains how to configure an AWS OIDC identity provider and construct a secure, keyless GitLab CI/CD pipeline.

Understanding Keyless Authentication with OpenID Connect (OIDC)

In traditional cloud deployment workflows, engineering teams frequently store static AWS IAM access keys inside continuous integration tool variables. If compromised, these long-lived secrets grant persistent unauthorized access to cloud infrastructure. OpenID Connect (OIDC) solves this security challenge by allowing GitLab CI/CD to request short-lived, auto-rotating JSON Web Tokens (JWTs) directly from AWS IAM.

Using OIDC authentication eliminates the need to store static AWS credentials in code repositories, significantly reducing the blast radius of potential credential leaks across enterprise multi-cloud environments.

Architecture Overview: How GitLab Talks to AWS IAM

When a pipeline job starts in GitLab, the runner requests a JSON Web Token from the GitLab OIDC issuer. GitLab signs this token cryptographically. The pipeline then assumes an IAM role in AWS by presenting this token. AWS validates the token against a configured Identity Provider (IdP) and issues temporary session credentials.

ComponentRole in OIDC Workflow
GitLab CI/CDOIDC Token Issuer and Pipeline Orchestrator
AWS IAM OIDC ProviderTrust anchor verifying GitLab's JSON Web Tokens
AWS IAM RoleDefines granular permissions granted to the pipeline
Amazon S3 BucketTarget destination for static website assets

Step 1: Setting up Your Amazon S3 Bucket for Static Hosting

Before establishing your pipeline, create a dedicated S3 bucket to hold your static web assets. Ensure that your bucket policy and public access settings align with your corporate security compliance framework.

For structured enterprise guidance on cloud-native application delivery, explore our PookieTech Digital Services or review our core offerings in Web Applications engineering.

Step 2: Creating an OIDC Identity Provider in AWS IAM

Navigate to the IAM console in AWS, select Identity Providers, and click Add Provider. Choose OpenID Connect and configure the parameters as follows:

  • Provider URL: https://gitlab.com
  • Audience: https://gitlab.com

Once created, note the ARN of your new Identity Provider for your IAM role trust policy.

Step 3: Configuring the IAM Role and Trust Policy

Create an IAM role dedicated to your GitLab deployment pipeline. Attach a trust policy that restricts assumption of the role strictly to your specific GitLab project path and branch.

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::YOUR_ACCOUNT_ID:oidc-provider/gitlab.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"gitlab.com:aud": "https://gitlab.com"
},
"StringLike": {
"gitlab.com:sub": "project_path:YOUR_GROUP/YOUR_PROJECT:ref_type:branch:ref:main"
}
}
}
]
}

Step 4: Writing the GitLab CI/CD Pipeline Configuration

Create a .gitlab-ci.yml file in your repository root. The pipeline uses the AWS CLI container image to assume the role via OIDC and synchronize your build directory to Amazon S3.

stages:
- deploy

variables:
AWS_ROLE_ARN: "arn:aws:iam::YOUR_ACCOUNT_ID:role/YourGitLabS3DeployRole`
AWS_REGION: "us-east-1`
S3_BUCKET_NAME: "your-production-bucket-name`

deploy_s3:
stage: deploy
image: amazon/aws-cli:latest
id_tokens:

SIGSTORE_ID_TOKEN: {}
script:
- echo "Assuming AWS IAM Role via OIDC...`
- >
ASSUME_ROLE_OUTPUT=$(aws sts assume-role-with-web-identity \
--role-arn "$AWS_ROLE_ARN" \
--role-session-name "GitLabCI_S3Deploy" \
--web-identity-token "$GITLAB_OIDC_TOKEN" \
--duration-seconds 900)
- export AWS_ACCESS_KEY_ID=$(echo $ASSUME_ROLE_OUTPUT | jq -r '.Credentials.AccessKeyId')
- export AWS_SECRET_ACCESS_KEY=$(echo $ASSUME_ROLE_OUTPUT | jq -r '.Credentials.SecretAccessKey')
- export AWS_SESSION_TOKEN=$(echo $ASSUME_ROLE_OUTPUT | jq -r '.Credentials.SessionToken')
- echo "Syncing assets to Amazon S3...`
- aws s3 sync ./dist s3://$S3_BUCKET_NAME --delete
rules:
- if: '$CI_COMMIT_BRANCH == "main"'

Implementation Checklist

  • Create an Amazon S3 bucket with correct block public access settings.
  • Configure GitLab.com as an OIDC Identity Provider in AWS IAM.
  • Attach a restrictive trust policy limiting assumptions to your exact repository path.
  • Grant precise s3:PutObject, s3:GetObject, and s3:ListBucket permissions to the IAM role.
  • Define the id_tokens block inside your .gitlab-ci.yml pipeline configuration.

For custom engineering workshops, institutional assessment platforms, and advanced cloud developer training, visit PookieTech Academy.

PookieTech Academy — Structured engineering education and institutional assessment platforms.

Learn software fundamentals or enquire about PookieTech Academy training and our institutional online examination platform.

Explore PookieTech Academy Enquire about programmes

Sources & Cited References

  1. How to Deploy to Amazon S3 Using GitLab CI/CD and AWS OIDC Without AWS Access Keys ↗(Ijeoma Igboagu)

Frequently Asked Questions

What is the primary advantage of using OIDC over AWS IAM access keys?
OIDC eliminates the need to store long-lived static credentials in CI/CD environment variables. It relies on short-lived, cryptographically signed JSON Web Tokens issued dynamically for each pipeline run, significantly reducing security risks.
How do I restrict the AWS IAM role so only my specific GitLab repository can assume it?
You restrict the IAM role by defining a trust policy condition that checks the 'gitlab.com:sub' claim, matching your exact organization name, project path, and target deployment branch.
Do I need to store any AWS credentials in GitLab settings when using OIDC?
No. You only need to store non-secret configuration variables such as your AWS Role ARN, AWS Region, and S3 Bucket name in your GitLab CI/CD variables.
What AWS CLI command is used to exchange the OIDC token for temporary session credentials?
The pipeline uses 'aws sts assume-role-with-web-identity' along with the GitLab OIDC token to retrieve temporary AWS access keys, secret keys, and session tokens.

Keep Exploring

More from Insights

Browse all Insights

Industry Context

From PookieTech Media Coverage

Explore PookieTech Media Coverage