When you're learning AWS, Amazon S3 is one of the services you're likely to come across early. Storage is one of the core components of cloud computing, and S3 makes it easy to store and manage files in the cloud.

If you've worked with S3 before, you probably know how to create a bucket and manually upload files through the AWS Management Console. But what happens when you need to deploy files regularly? Would you want to keep uploading them manually every time you make a change? This is where automation comes in.

In this tutorial, I'll show you how to automate the deployment of files to an Amazon S3 bucket using GitLab CI/CD. We'll also configure OpenID Connect (OIDC) to establish a secure connection between GitLab and AWS without storing long-lived AWS access keys in GitLab CI/CD variables.

By the end of this tutorial, you'll know how to configure a GitLab CI/CD pipeline, connect GitLab to AWS using OIDC, and automatically deploy files to an S3 bucket whenever you push changes to your GitLab repository.

Let's get started!

let get started with tutorial

Table of Contents

Prerequisites

Before getting started, make sure you have the following:

  • A basic understanding of cloud computing and AWS.
  • An AWS account.
  • A GitLab account to store your code and automate deployment.
  • A code editor like VS Code, Atom, or Sublime

Note: I'll be using GitLab for this tutorial. If you use GitHub instead, you can also get the same result using GitHub Actions. Both platforms support automated deployment.

Before setting up Amazon S3 for automated deployment, let's take a step back and understand why this approach is worth learning.

You might be wondering, why should I automate file deployment? What are the advantages? These are valid questions, and understanding the answers will help you see where automation fits into your cloud workflow.

Let's look at them before moving on to the practical steps.

Why Should I Automate My Deployments?

Imagine your website is hosted on Amazon S3. Every time you make a change, you have to log in to the AWS Console, open your S3 bucket, and manually upload the new files.

That is fine for a small project that rarely changes. But what if you need to update your site often?

Uploading files by hand repeatedly gets boring and takes some time.

This is where automation helps. With GitLab CI/CD or GitHub Actions, you can set up a pipeline that sends your files to S3 for you. Every time you push changes to GitLab, the pipeline runs and deploys your site.

So instead of uploading files yourself, GitLab does it for you.

What Are the Advantages of This?

Using GitLab CI/CD to deploy files to Amazon S3 has several advantages:

  1. Faster deployment: Once the pipeline is set up, you just push your changes to GitLab, and the deployment starts on its own.
  2. Fewer mistakes: The pipeline follows the same steps every time, so you're less likely to forget a file or upload the wrong version.
  3. Less manual work: You don't have to repeat the upload process through the AWS Console whenever you update your files.
  4. Version control: Your website files live in GitLab, so you can track changes, look at old commits, and see what was updated.

What is CI/CD, and Why Does it Matter?

CI/CD stands for Continuous Integration and Continuous Delivery or Continuous Deployment.

In simple terms, it's a way to automate the steps of testing and deploying your code, so you don’t have to do each step by hand. Think of it as giving a set of instructions to follow whenever you push changes to your repository.

In this tutorial, GitLab CI/CD will deploy your files to Amazon S3. When you push changes to your GitLab repository, GitLab starts a pipeline. The pipeline follows the steps you wrote in your .gitlab-ci.yml file and uploads your updated files to your S3 bucket.

As I said earlier, CI/CD makes deployment easier, saves you from repeating the same work, and keeps your deployment process the same every time.

What is OpenID Connect (OIDC), and Why Use it?

We'll use OpenID Connect (OIDC) in this tutorial to connect GitLab to AWS safely. But what is OIDC, and why do we need it?

OIDC is a way for one service to prove who it is to another service. In our case, it lets GitLab prove its identity to AWS.

Without OIDC, you would have to save your AWS access keys in GitLab. These keys don't expire, so if someone gets hold of them, they can use your AWS account.

With OIDC, you don't save any keys. When your pipeline runs, GitLab proves its identity to AWS, and AWS gives it a temporary key. This key stops working after the job is done. This is safer, and you have no long-term keys to store or manage.

Advantages of Using OIDC

  • You don't need to store permanent AWS access keys in GitLab.
  • AWS provides credentials that expire after a set period.
  • OIDC reduces the risk of exposing long-term AWS credentials.

Are There Any Disadvantages?

The initial setup requires some additional configuration in AWS IAM and GitLab. You need to configure the identity provider, IAM role, and permissions before the connection can work.

But once it's configured, you can reuse this authentication setup for your deployment pipeline.

Now that you understand why we're using GitLab CI/CD and OIDC, let's move on to the practical part and set everything up.

Step 1: Prepare Your Website Files (Code) and Upload Them to GitLab

Before you can deploy your website to Amazon S3, you need to create the website files and upload them to your GitLab repository.

Open your preferred code editor, such as VS Code and create a new project folder named s3-website.

Inside the folder, create two HTML files:

  • index.html: This will be the homepage visitors see when they open your website.
  • error.html: This will be displayed when a visitor encounters an error.

Write your HTML code in each file and save your changes.

Then open index.html in your browser to check that the homepage displays correctly. Test error.html to make sure it displays as expected.

Once you're satisfied with the files, open your GitLab repository and upload both HTML files to the repository. Then commit your changes to the main branch.

Your GitLab repository should now contain:

practice-action-aws/ ├── index.html └── error.html 

These are the files your GitLab CI/CD pipeline will automatically deploy to the S3 bucket.

Step 2: Create an Amazon S3 Bucket

The next step is to create an S3 bucket where GitLab will deploy your files.

To do this, sign in to the AWS Management Console, search for S3, and open the service. Click Create bucket.

Enter a unique name for your bucket. For example, gitlab-s3-deployment-demo-bucket. You'll also need to select the AWS Region where you want to create the bucket. Remember the region because you'll need it later when configuring GitLab.

Creation of AWS S3 bucket

Step 3: Give Your S3 Bucket Public Access

Now that you've created your S3 bucket, the next step is to give it public access.

We're doing this for learning purposes here so we can demonstrate how publicly accessible files work. In a real-world production environment, you should avoid making your S3 bucket public unless there's a specific requirement and appropriate security controls are in place.

To give it public access, click on the bucket you created. Then click the Permissions tab at the top.

Giving the AWS S3 bucket permission

Locate Block public access (bucket settings) and click Edit. Uncheck Block all public access. AWS will display a warning. Read it, acknowledge the change, and click Save changes.

Next, scroll down to the bucket policy and click Edit. Paste the bucket policy below, replacing the existing content if necessary.

{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::gitlab-s3-deployment-demo-bucket/*" } ] } 

Click Save changes.

This is to allow public read of our bucket

The next step is to enable static website hosting. This is important because we don't just want to deploy files to S3 automatically. We want to make our website accessible through a browser.

Step 4: Enable Static Website Hosting

Now that you've configured public access, let's enable static website hosting for your S3 bucket.

To start, click on the Properties tab. Scroll down to the Static website hosting section and click Edit.

Under Static website hosting, select Enable. Then select Host a static website as the hosting type.

Under the Index document, enter index.html. Same for the error field error.html. Click Save changes.

Once saved, scroll back to the Static website hosting section. You should see your website endpoint.

This endpoint is the URL through which visitors can access your website.

next step show how to get endpoint url

Step 5: Configure AWS IAM for GitLab OIDC Authentication

Now that your S3 bucket is set up for static website hosting, the next step is to set up AWS IAM.

We need to permit GitLab to upload files to your S3 bucket whenever you push changes to your repository.

To do this, we'll use OIDC. It lets GitLab connect to AWS and get temporary credentials, so you don't have to save long-term AWS access keys in GitLab.

How to Create an OIDC Identity Provider in AWS

Search for IAM and open it. In the left menu, click Identity providers. Then click Add provider.

Select OpenID Connect as the provider type and enter these details:

Field Value
Provider URL https://gitlab.com
Audience sts.amazonaws.com
  1. Click Add provider.
Adding identity provider

This tells AWS to trust GitLab as an identity provider. In the next step, you'll create an IAM role that uses it.

Step 6: Create an IAM Role for GitLab

Now that you've created the OIDC identity provider, you'll need to create an IAM role that GitLab can assume to access your S3 bucket.

This role will define what GitLab is allowed to do in your AWS account.

To start, click Roles in the left navigation panel or follow the on-screen banner, then click Create role.

  • Under Trusted entity type, select Web identity
  • Under Identity Provider, select gitlab.com
  • Under Audience, select sts.amazonaws.com
  • Under Group type, in your username

The other fields are optional.

Then click Next and Next. Finally, click the Create role button.

Creating the roles for AWS to connect to gitlab

Step 7: Attach S3 Permissions to the IAM Role

Select the role you just created and open the Permissions tab. Click Add permissions. Then select Create Inline Policy and select the JSON tab.

Replace the existing content with this policy:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "ListS3Bucket", "Effect": "Allow", "Action": "s3:ListBucket", "Resource": "arn:aws:s3:::gitlab-s3-deployment-demo-bucket" }, { "Sid": "UploadAndDeleteObjects", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:DeleteObject" ], "Resource": "arn:aws:s3:::gitlab-s3-deployment-demo-bucket/*" } ] } 

Note: Replace the bucket name in the Resource ARN above with your own S3 bucket name. The bucket name used in this tutorial is for demonstration purposes.

Then click Next. Enter the policy name and click Create policy.

AWS roles created this allow OIDC

So what do these permissions mean?

Permission Purpose
s3:ListBucket Allows GitLab to list objects in the bucket.
s3:PutObject Allows GitLab to upload files to the bucket.
s3:DeleteObject Allows GitLab to delete objects when synchronizing files with the --delete option.

Step 8: Configure GitLab CI/CD Variables

Now that you've created your IAM role and attached the required S3 permissions, let's move to GitLab.

You need to provide GitLab with the IAM Role ARN so that your CI/CD pipeline can use it to authenticate with AWS through OIDC.

To do this, open your GitLab repository and, from the left navigation panel, click Settings. Select CI/CD.

Expand the Variables section and click Add variable.

Heading over to gitlab to put in our environmental variable

Step 9: Create the GitLab CI/CD Pipeline

Now that you've configured GitLab CI/CD variables and created your IAM role, it's time to create the pipeline that automates deployment to Amazon S3.

GitLab uses a file called .gitlab-ci.yml to define the jobs and instructions that should run whenever changes are pushed to the repository.

For this tutorial, your pipeline will:

  • Authenticate with AWS using OIDC.
  • Obtain temporary AWS credentials.
  • Upload our website files to the S3 bucket.
  • Synchronise changes whenever we push new updates.

Here's an example repository:

Ijaylab/practice-action-aws 

Click the + icon to create a new file, or go to the Policy editor and your CI/CD code. Name the file .gitlab-ci.yml and add the following configuration:

deploy_to_s3: stage: deploy image: ubuntu:latest id_tokens: GITLAB_OIDC_TOKEN: aud: sts.amazonaws.com before_script: - apt-get update - apt-get install -y curl unzip jq - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" - unzip awscliv2.zip - ./aws/install script: - export AWS_WEB_IDENTITY_TOKEN_FILE="/tmp/web-identity-token" - echo "$GITLAB_OIDC_TOKEN" > "$AWS_WEB_IDENTITY_TOKEN_FILE" - export AWS_ROLE_SESSION_NAME="GitLab-OIDC-Session" - export AWS_DEFAULT_REGION="us-east-1" - echo "$GITLAB_OIDC_TOKEN" | cut -d '.' -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | jq '{sub, aud}' - aws sts get-caller-identity - aws s3 sync . s3://gitlab-s3-deployment-demo-bucket --delete --exclude ".git/*" --exclude ".gitlab-ci.yml" rules: - if: '$CI_COMMIT_BRANCH == "main"' 

So what's going on in this configuration?

  • deploy_to_s3: Names the job.
  • stage: Defines the deployment stage.
  • image: Provides the Ubuntu environment.
  • id_tokens: Requests an OIDC token.
  • before_script: Installs AWS CLI and other tools.
  • script: Runs the deployment commands.
  • AWS_WEB_IDENTITY_TOKEN_FILE: Stores the OIDC token.
  • AWS_DEFAULT_REGION: Sets the AWS region.
  • aws sts get-caller-identity: Checks AWS authentication.
  • aws s3 sync: Uploads files to S3.
  • --delete: Removes outdated files.
  • --exclude: Skips files we don't need.
  • rules: Runs the job only on the main branch.

Important: Replace YOUR_AWS_REGION with the actual region where you created your S3 bucket, for example eu-west-1 if that is your bucket's region.

This will trigger the GitLab pipeline.

Step 10: Verify the Deployment on Amazon S3

Now that your GitLab pipeline has successfully authenticated with AWS, the next step is to confirm that your website files have been uploaded to your S3 bucket.

To do that, open the AWS Management Console and go to Amazon S3, then Buckets. Open your bucket: gitlab-s3-deployment-demo-bucket.

Then click the Objects tab, and check that your website files are there:

  • index.html
  • error.html
  • style.css

These are the files that we created and uploaded to GitLab earlier.

Then go to the Properties tab of your S3 bucket. Scroll down to Static website hosting, and copy the Bucket website endpoint. Then paste it into your browser. Your website should load.

testing the pipeline

Step 11: Re-Testing Deployment

To confirm that automation works, open index.html in your repository and make a small change to the index page.

Then go back to the AWS console and refresh your S3 website hosting endpoint. You should see your updated website without manually uploading the file to S3.

That's the automation we wanted to achieve. GitLab handles the deployment whenever you push changes to your repository.

Making a change in our index.html file to see if it automatically works

Step 12: Test the Error Page

Since you created an error.html file, let's confirm that it works on your S3 website.

Open your S3 bucket in the AWS Console and go to Properties. Then scroll down to Static website hosting and confirm that the Error document is set to error.html.

Next, copy your bucket website endpoint and open the endpoint in your browser and add a page that doesn't exist.

still testing now the error page

You should see the custom error page you created.

Once you've confirmed that both your homepage and error page work, you can proceed to cleanup.

Step 11: Clean Up Your AWS Resources

Since this is a learning project, you may want to remove the resources when you're done to avoid unnecessary AWS charges.

To do that, empty your S3 bucket and delete it if you no longer need it (AWS will charge). You can also delete the IAM role if you no longer need it (optional, as AWS doesn't charge). Finally, delete the IAM identity provider if no other project uses it (optional, as AWS doesn't charge).

Conclusion

That’s it! You now have GitLab deploying your website files to Amazon S3 for you.

You no longer have to log in to AWS and upload your files by hand every time you make a change. Now you just push your changes to GitLab, and the pipeline does the upload. And because we used OIDC, you didn't have to save any AWS keys anywhere.

I hope this tutorial helped you set up your own pipeline. If you ran into an error, check the job log. That is how I found my own mistakes while writing this.

Further Reading:

If you found this article helpful, feel free to share it.

I also build tools for people in tech. One of my recent projects is SubmitReady, which helps people prepare their AWS Community Builders applications. Whether you are a developer, technical writer, developer advocate, community manager, or work in another area of tech, you can use it to review your application and identify where you can make improvements.

If you prefer learning through videos, I also explain cloud and DevOps topics in simple terms on my YouTube channel.

You can also follow me on Twitter, LinkedIn and GitHub.

Thank you for reading!