IntroductionÂ
Continuous Integration and Continuous Deployment (CI/CD) are critical practices in contemporary software program development, ensuring fast and reliable shipping of software. When it comes to infrastructure as code (IaC), those practices can drastically streamline the deployment system. GitLab CI/CD offers a comprehensive solution for automating infrastructure deployment. In this manual, we’re going to use Terraform to provision resources on AWS and integrate this technique right into a GitLab CI/CD pipeline.Â
PrerequisitesÂ
GitLab Account: Ensure you have a GitLab account, and a project set up.Â
AWS Account: You’ll want get access to an AWS account for deploying infrastructure.Â
Terraform: Basic knowledge of Terraform and its configuration files.Â
Git: Familiarity with Git for version control.Â
Setting Up the GitLab RepositoryÂ
First, create a new repository on GitLab. This repository will store your Terraform configuration files and the GitLab CI/CD pipeline configuration.Â
Copy the Clone with SSHÂ

# Clone the new repository locallyÂ
git clone https://gitlab.com/your-username/your-repo.git

cd your-repoÂ

# Create the necessary directory structureÂ
mkdir -p infrastructure/terraform

# Creat the necessary file for gitlab CI/CD:Â
Vi -p .gitlab-ci.yml

Writing the CI/CD PipelineÂ
The .Gitlab-ci.Yml file is essential for defining the CI/CD pipeline. Below is an example configuration for deploying infrastructure the usage of Terraform.Â
yamlÂ
Code:Â
 stages:Â
 - initÂ
 - planÂ
 - applyÂ
variables:Â
 TF_ROOT: "infrastructure/terraform" # Directory where your Terraform code is storedÂ
 TF_VAR_aws_access_key: $AWS_ACCESS_KEY_IDÂ
 TF_VAR_aws_secret_key: $AWS_SECRET_ACCESS_KEYÂ
before_script:Â
 - apt-get update && apt-get install -y curl unzip python3-pip jqÂ
 - pip3 install awscliÂ
 - curl -LO https://releases.hashicorp.com/terraform/1.9.3/terraform_1.9.3_linux_amd64.zipÂ
 - unzip terraform_1.9.3_linux_amd64.zipÂ
 - mv terraform /usr/local/bin/Â
 - terraform --versionÂ
default:Â
 image:Â
   name: ubuntu:20.04Â
   entrypoint: [""]Â
init:Â
 stage: initÂ
 script:Â
   - echo "Listing repository contents:"Â
   - ls -laÂ
   - echo "Listing contents of $TF_ROOT:"Â
   - cd $TF_ROOTÂ
   - ls -la # List files in the Terraform directoryÂ
   - terraform initÂ
plan:Â
 stage: planÂ
 script:Â
   - cd $TF_ROOTÂ
   - terraform initÂ
   - terraform plan -out=tfplanÂ
apply:Â
 stage: applyÂ
 script:Â
   - cd $TF_ROOTÂ
   - terraform initÂ
   - terraform apply -auto-approve tfplanÂ
 when: manualÂ
Configuring Infrastructure as CodeÂ
Within the infrastructure/terraform directory, create your Terraform configuration files. For example, to create an default vpc and S3 bucket :Â
Create Terraform configuration files:Â
Inside the infrastructure/terraform directory, create a file named main.tf with theÂ
HCLÂ
Code:Â
#As a sample:Â
provider "aws" {Â
 region = "us-east-1"Â
}Â
resource "aws_vpc" "main" {Â
 cidr_block = "10.0.0.0/16"Â
 instance_tenancy = "default"Â
}Â
resource "aws_s3_bucket" "SUDO1" {Â
 bucket = "my-tf-test-bucket97"Â
 tags = {Â
   Name       = "My bucket"Â
   Environment = "Dev"Â
 }Â
}
Integrating with AWSÂ
To integrate AWS with GitLab CI/CD, you’ll need to configure AWS credentials in your GitLab project. Go to your project settings and navigate to CI/CD > Variables. Add the following variables:Â
AWS_ACCESS_KEY_IDÂ
AWS_SECRET_ACCESS_KEY

These variables will be used by Terraform to authenticate with AWS.Â
Running the PipelineÂ
Once the whole thing is installation, commit your changes and push them to the GitLab repository. This action will cause the CI/CD pipeline.Â
Command:Â
git add .Â
git commit -m "Add Terraform configuration and CI/CD pipeline"Â
git push origin main


After pushing the repository, go to GitLab to monitor the pipeline stages.Â
Stages1 RunningÂ

Stage2 PassÂ

After moving to the third stage in the pipeline, we must run it manually because we set up the CI/CD configuration to be manual. If we change it to automatic, it will run automatically and pass the third stage.Â

Monitor the pipeline execution inside the GitLab CI/CD interface. If any troubles arise, the logs will help you troubleshoot and fix them.Â
Best PracticesÂ
Security: Use GitLab CI/CD variables to securely store sensitive information such as AWS credentials.Â
Modularity: Organize your Terraform code into modules to improve reusability and maintainability.Â
Testing: Implement automated tests to validate your infrastructure code before deployment.Â
ConclusionÂ
By leveraging GitLab CI/CD and Terraform, you could automate the deployment and control of your infrastructure, main to greater efficient and reliable operations. This approach no longer saves time but also reduces the hazard of human mistakes. Experiment with exclusive configurations and amplify the pipeline to fulfill your specific wishes. Happy automating!Â