CI/CD Pipeline for Terraform Deployment

Task Goal

Automate Terraform infrastructure deployment using a robust CI/CD pipeline with Jenkins, GitHub Actions, or GitLab CI. Ensure all infrastructure changes are version-controlled, reviewed via pull requests (PRs), and deployed automatically.

Architecture Overview

1. Tools and Technologies

Task Workflow

1. Infrastructure as Code (IaC) Setup

2. Version Control & PR Workflow

3. CI/CD Pipeline Automation

Pipeline Triggers

Jenkins Pipeline (Alternative: GitHub Actions/GitLab CI)

Implementation Steps

Step 1: Set Up Terraform Code Repository

  1. Initialize a GitHub/GitLab repo for Terraform configurations.
  2. Define Terraform modules:
  3. Store Terraform backend configuration in S3/Terraform Cloud.

Step 2: Configure CI/CD Pipeline

For Jenkins

  1. Install Jenkins plugins: Pipeline, GitHub Branch Source, Terraform.
  2. Create a >Jenkinsfile:
    pipeline {
        agent any
        environment {
            AWS_ACCESS_KEY_ID = credentials('aws-access-key')
            AWS_SECRET_ACCESS_KEY = credentials('aws-secret-key')
        }
        stages {
            stage('Checkout Code') {
                steps {
                    git 'https://github.com/org/repo.git'
                }
            }
            stage('Terraform Init') {
                steps {
                    sh 'terraform init'
                }
            }
            stage('Terraform Validate') {
                steps {
                    sh 'terraform validate'
                }
            }
            stage('Terraform Plan') {
                steps {
                    sh 'terraform plan'
                }
            }
            stage('Terraform Apply') {
                when {
                    branch 'main'
                }
                steps {
                    sh 'terraform apply -auto-approve'
                }
            }
        }
    }
    

For GitHub Actions

Create .github/workflows/terraform.yml:

name: Terraform Deployment
on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main

jobs:
  terraform:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v2

      - name: Terraform Init
        run: terraform init

      - name: Terraform Validate
        run: terraform validate

      - name: Terraform Plan
        run: terraform plan

      - name: Terraform Apply
        if: github.ref == 'refs/heads/main'
        run: terraform apply -auto-approve

For GitLab CI

Add .gitlab-ci.yml:

stages:
  - validate
  - plan
  - apply

variables:
  TF_IN_AUTOMATION: "true"

validate:
  stage: validate
  script:
    - terraform init
    - terraform validate
  only:
    - merge_requests

plan:
  stage: plan
  script:
    - terraform plan
  only:
    - merge_requests

apply:
  stage: apply
  script:
    - terraform apply -auto-approve
  only:
    - main

Step 3: Secure Terraform State & Credentials

Step 4: Monitoring & Logging

Step 5: Testing and Deployment

  1. Push changes to a feature branch → Create PR.
  2. Pipeline runs Terraform validation & plan.
  3. Review PR, approve, and merge.
  4. CI/CD triggers Terraform apply in main.

Expected Outcomes

Next Steps