Automate Amazon S3 Deployments with GitLab CI/CD and OIDC
Want to deploy to Amazon S3 without uploading files by hand after every change? This freeCodeCamp tutorial shows how to do it with GitLab CI/CD. The connection to AWS uses OpenID Connect (OIDC), so you don't need to store long-lived AWS access keys in your GitLab CI/CD variables. Every time you push changes to your GitLab repository, a pipeline automatically uploads your files to an S3 bucket.
Why Automate Deployments to S3
Imagine your site is hosted on Amazon S3. Every time something changes, you have to log in to the AWS Console, open your bucket, and upload the new files one by one. That's fine for a small project that rarely changes. But if your site is updated often, the process gets tedious and eats up time.
With a pipeline, GitLab handles the uploading for you. According to the tutorial, there are four main advantages:
- Faster deployment: once the pipeline is set up, you just push your changes and the deployment starts on its own.
- Fewer mistakes: the pipeline follows the same steps every time, so you're less likely to miss a file or upload the wrong version.
- Less manual work: you don't have to repeat the upload process through the AWS Console.
- Version control: your site files live in GitLab, so you can track changes and look at old commits.
CI/CD stands for Continuous Integration and Continuous Delivery or Continuous Deployment. In short, you write a set of instructions that run automatically whenever you push to your repository. In this tutorial, the pipeline follows the steps in your .gitlab-ci.yml file and uploads your latest files to the S3 bucket. The author also notes that GitHub Actions can achieve the same result if you prefer GitHub.
Why Use OIDC Instead of Access Keys
OIDC is a way for one service to prove its identity to another. Here, GitLab proves its identity to AWS. Without OIDC, you would have to save your AWS access keys in GitLab. Those keys don't expire, so anyone who gets hold of them can use your AWS account.
With OIDC, no keys are stored. When the pipeline runs, GitLab proves its identity and AWS hands back temporary credentials that stop working once the job is done. The advantages:
- You don't need to store permanent AWS access keys in GitLab.
- AWS provides credentials that expire after a set period.
- The risk of exposing long-term AWS credentials is reduced.
The downside comes at the start. You need some extra configuration in AWS IAM and GitLab, including the identity provider, IAM role, and permissions, before the connection works. Once it's set up, though, you can reuse this authentication setup for your deployment pipeline.
The tutorial moves step by step. You start by preparing your website files and uploading them to GitLab, then create an S3 bucket, open public access, and enable static website hosting. After that, you create the OIDC identity provider and IAM role in AWS, attach S3 permissions, set the CI/CD variables, and write the pipeline. It ends with verifying the deployment, re-testing, testing the error page, and cleaning up your AWS resources. The prerequisites are simple: a basic understanding of cloud computing and AWS, an AWS account, a GitLab account, and a code editor like VS Code, Atom, or Sublime.
If you manage a static site on S3, whether as a developer or a business owner, this approach removes the manual uploading and cuts the risk of leaking access keys. And if you're still learning AWS, the tutorial is a good exercise for understanding CI/CD and modern authentication at the same time.
Source: freecodecamp.org