๐Ÿš€ Terraform - GitLab Integration: Automate Infrastructure Deployment-36 Resource of AWS Deploy in a single shot ๐Ÿš€

ยท

4 min read

๐Ÿš€ 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

  1. Log in to GitLab and go to New Project

  2. Select Public visibility

  3. 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>
ย