Automating AWS Infrastructure with Terraform:
 A Step-by-Step Guide to CI/CD Pipelines in GitLab

Automating AWS Infrastructure with Terraform: A Step-by-Step Guide to CI/CD Pipelines in GitLab

Context

In the rapidly evolving landscape of software development, two concepts have stood out for their transformative impact: Infrastructure as Code (IaC) and Continuous Integration/Continuous Deployment (CI/CD).

IaC tools like Terraform have revolutionized how we manage and configure our software environments, increasing efficiency and reducing the risk of human error. On the other hand, CI/CD practices have streamlined the process of deploying changes and updates, ensuring that your software is always running at its peak potential.

GitLab, a renowned platform for comprehensive DevSecOps tools, is at the forefront of marrying these two concepts seamlessly. Its powerful, flexible, and user-friendly CI/CD pipeline sets it apart from the rest.

In this blog post, we will showcase one of GitLab's most impressive features: the ability to rapidly deploy infrastructure through its CI/CD pipelines. By combining the prowess of Terraform and GitLab, you can effortlessly create resources like an AWS S3 bucket within minutes.

Whether you are an experienced DevOps engineer or just starting your journey into IaC and CI/CD, you're in the right place. We will delve into a step-by-step guide on deploying an S3 bucket using Terraform on GitLab’s platform.

Before we get started, let's take a look at the prerequisites you'll need to have in place.

Pre-Requisites

  • AWS Account: To begin with, it is essential to have an AWS account since we will be deploying resources on the AWS cloud. If you haven't created an account yet, you can easily do so by following the link provided here.

  • Code Repository: For this tutorial, it is assumed that you already have a repository containing your Terraform scripts. We will be utilizing the "This Code Repository" specifically for creating the S3 bucket.

  • GitLab Account: Lastly, don't forget to create a GitLab account. GitLab provides a freemium option, which is perfectly adequate for getting started. You can easily sign up for a GitLab account here. Additionally, you have the option to sign up using your Google or GitHub account.

Let’s Get Started…

Goal

In this tutorial, we'll guide you through the process of deploying an AWS S3 bucket using Terraform scripts. The deployment will be streamlined and automated through a CI/CD pipeline on GitLab. By following this tutorial, you'll gain the ability to effortlessly deploy an S3 bucket to your AWS account with just a single push to your code repository.

Throughout the upcoming sections, we will lead you through the steps to set up your GitLab project, establish your CI/CD pipeline, and validate the successful deployment of the S3 bucket.

Start…

Creating a Project

“The first step in GitLab is to create a project. A project is essentially a space where your code resides. It's designed to help you organize your projects under different teams or departments. To create a workspace, click on the 'New Project' icon at the left-most.”

Choosing Project Type

“Once you click on the 'New Project' icon, you will be presented with various project templates to choose from. Opt for ‘Import project’' since we are importing from github.”

Importing Your Project

“After selecting the ‘Import project' icon, you will be prompted to select importing from.We are choosing our Github repository for code. Import the appropriate repository with all the terraform code. ”

From Github

  • “To import from your GitHub account, select the repository you wish to import. In this instance, we are importing from this specified repository. Additionally, you have the option to assign a name to your project directly from this interface.”

  • “ Click ‘Import 1 repository’.”

Project

“ Once you have successfully imported the repository, return to the list of all projects. You will now notice that a new project has been established. Click on the project name to access and enter into it.”

Defining the Terraform Pipeline in GitLab

  • Create a .gitlab-ci.yml(make sure the file name is .gitlab-ci.yml) file in the root of your repository.

  • Define the stages for the pipeline, such as init, plan, and apply.

  • Configure the jobs for each stage to run the Terraform commands using the environment variables for AWS credentials.

  • Commit and push the .gitlab-ci.yml file to trigger the pipeline.

[REQUIRED] Add Secrets for your yml file

  • Let's explore the process of adding secrets to your GitLab, enabling seamless pipeline delivery.

  • Creating a key:

    • Login to your AWS account.

    • Search for IAM in the top search bar and click on it.

    • Click on the ‘manage access key’ or click here.

    • Scroll down to ‘Access Key’, click on ‘Create access key’ and on the next page click ‘Create access key’ again, either copy this key ‘Access key’ and ‘Secret access key’ or click download.

  • GitLab Variable:

    • Back on project that you are working on

    • From sidebar, scroll down and click ‘Settings’ and then click ‘CI/CD’

      • Scroll down to ‘Variables’ and click ‘expand’.

      • Here you can add your ‘Access Key’ and ‘Secret access key’ by clicking ‘Add variable’

      • Now your yml file is complete.

Running the Pipeline

Open your Gitlab project and navigate to the Build section form sidebar.

  • Next, click on ‘Pipelines’.

  • Click on ‘Run pipeline’, it will run from code written in the .gitlab-ci.yml file in the repository.

You will be prompt to this page:

Next, click ‘Run pipeline’.

Successful Run

Tree ->

  • validate

    • init

    • validate

  • build

    • plan
  • deploy

    • apply

S3 Bucket created on AWS

Please log in to your AWS account and proceed to check for the bucket.

Looking for the .gitlab-ci.yml? Visit github and you’ll find the full working YAML.

Note: In our .gitlab-ci.yml file, we've incorporated a meticulously preconfigured GitLab template that effortlessly handles Terraform operations. This code is remarkably user-friendly, enabling even novices to build pipelines with ease. With just a few minutes of setup, you can seamlessly set up production-ready workflows. However, it's worth noting that two areas need attention for improvement:

1. The handling of destruction was not addressed, and we should ensure it is accounted for.

2. The GitLab template was utilized in our implementation to streamline the process further.

In summary, we have experienced the effortless integration of GitLab's CI/CD pipeline with Terraform, automating the deployment of AWS S3 buckets. This powerful synergy not only streamlines workflows but also minimizes errors and expedites software delivery.

Embrace this opportunity and embark on a journey of profound transformation in your development process by harnessing the immense potential of GitLab's CI/CD alongside Terraform. Don't delay any further; begin your transformative endeavors today and unlock a new realm of efficiency and innovation for your projects.

For more of these pre-built terraform recipes, visit Infrasity."

Did you find this article valuable?

Support Infrasity Blog by becoming a sponsor. Any amount is appreciated!