๐ Terraform - GitLab Integration: Automate Infrastructure Deployment-36 Resource of AWS Deploy in a single shot ๐
Managing infrastructure efficiently is crucial for any DevOps pipeline. Terraform and GitLab CI/CD provide a seamless way to automate deployments and infrastructure management. This guide walks you through integrating Terraform with GitLab CI/CD using YAML configurations, GitLab Runner, and best practices.
GitLab-project link:
https://gitlab.com/sheakdev/terra-gitlab
๐ Why Integrate Terraform with GitLab?
โ
Automate infrastructure deployment
โ
Ensure consistency with Infrastructure as Code (IaC)
โ
Manage cloud resources efficiently
โ
Leverage GitLab CI/CD for automated workflows
๐ Table of Contents
๐ Getting Started
1๏ธโฃ Create a GitLab Project
Log in to GitLab and go to New Project
Select Public visibility
Clone the project locally
git clone <your-repo-url>
cd <your-repo>
2๏ธโฃ Add Terraform Code & Push to GitLab
git init
git add .
git commit -m "Initial Terraform setup"
git remote add origin <your-repo-url>
git push origin master
๐ GitLab CI/CD Configuration
Create a .gitlab-ci.yml file in your repository:
default:
tags:
- Terraform
- AWS
variables:
TERRAFORM_DESTROY: "YES"
TERRAFORM_APPLY: "NO"
TERRAFORM_VERSION: "1.6.1"
stages:
- terraform-version-check
- terraform-init
- terraform-fmt-validate
- terraform-plan
- terraform-apply
- terraform-destroy
# Terraform Version Check Job
job-check-terraform-version:
stage: terraform-version-check
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- pwd && ls -al
- tfenv install 1.6.1 && tfenv use 1.6.1
- terraform version
# Terraform Init Job for Developmenttt
job-run-terraform-init-development:
stage: terraform-init
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- cd development
- terraform init
# Terraform Init Job for Production
job-run-terraform-init-production:
stage: terraform-init
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- cd production
- terraform init
# Terraform Format and Validate Job for Development
job-run-terraform-fmt-validate-development:
stage: terraform-fmt-validate
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- cd development
- terraform init
- terraform fmt
- terraform validate
# Terraform Format and Validate Job for Production
job-run-terraform-fmt-validate-production:
stage: terraform-fmt-validate
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- cd production
- terraform init
- terraform fmt
- terraform validate
# Terraform Plan Job for Development
job-run-terraform-plan-development:
stage: terraform-plan
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- cd development
- terraform init
- terraform plan
# Terraform Plan Job for Production
job-run-terraform-plan-production:
stage: terraform-plan
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- cd production
- terraform init
- terraform plan
# Terraform Apply Job for Development
job-run-terraform-apply-development:
stage: terraform-apply
needs: ["job-run-terraform-plan-development"]
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- cd development
- terraform init
- terraform apply --auto-approve
# Terraform Apply Job for Production
job-run-terraform-apply-production:
stage: terraform-apply
needs: ["job-run-terraform-plan-production"]
rules:
- if: $TERRAFORM_DESTROY == "NO" && $TERRAFORM_APPLY == "YES"
script:
- cd production
- terraform init
- terraform apply --auto-approve
# Terraform Destroy Job for Development
job-run-terraform-destroy-development:
stage: terraform-destroy
rules:
- if: $TERRAFORM_DESTROY == "YES"
script:
- cd development
- terraform init
- terraform destroy --auto-approve
# Terraform Destroy Job for Production
job-run-terraform-destroy-production:
stage: terraform-destroy
rules:
- if: $TERRAFORM_DESTROY == "YES"
script:
- cd production
- terraform init
- terraform destroy --auto-approve
๐ Explanation:
โ
Terraform Init - Initializes Terraform
โ
Validate Stage - Ensures syntax is correct
โ
Plan Stage - Previews changes before applying
โ
Apply Stage - Deploys the infrastructure
๐ง Using tfenv for Terraform Version Management
To manage different Terraform versions, install tfenv:
git clone https://github.com/tfutils/tfenv.git ~/.tfenv
echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
tfenv install latest
tfenv use latest
๐ค Installing GitLab Runner
1๏ธโฃ Install GitLab Runner:
sudo curl -L --output /usr/local/bin/gitlab-runner \
"https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
sudo chmod +x /usr/local/bin/gitlab-runner
2๏ธโฃ Register the Runner:
sudo gitlab-runner register \
--non-interactive \
--url "https://gitlab.com/" \
--registration-token "<your-token>" \
--executor "docker" \
--docker-image "hashicorp/terraform:latest"
โ๏ธ Deploying an Ubuntu Server with Terraform
Example main.tf for deploying an Ubuntu EC2 instance:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "Terraform-Instance"
}
}
Apply the configuration:
terraform init
terraform apply -auto-approve
๐งน Cleaning Up
To destroy resources just change your gitlab-ci.yml file to TERRAFORM_DESTROY: "YES" and TERRAFORM_APPLY: "NO" :
default:
tags:
- Terraform
- AWS
variables:
TERRAFORM_DESTROY: "YES"
TERRAFORM_APPLY: "NO"
TERRAFORM_VERSION: "1.6.1"
๐ Troubleshooting
๐ก Pipeline Fails?
Check the CI/CD logs in GitLab > CI/CD > Pipelines
๐ก GitLab Runner Issues?
Restart the runner:
sudo gitlab-runner restart
๐ก Terraform Version Conflict?
Use tfenv to switch versions:
tfenv install <version>
tfenv use <version>