DEV Community

Cover image for Automating Infrastructure Deployment for CI/CD Pipelines Using Terraform
Isaeus "Asi" Guiang
Isaeus "Asi" Guiang

Posted on

Automating Infrastructure Deployment for CI/CD Pipelines Using Terraform

Image description

Modern software development heavily relies on continuous integration and continuous delivery (CI/CD) pipelines to streamline deployments. Infrastructure as Code (IaC) tools like Terraform play a critical role in automating the provisioning and management of infrastructure resources for these pipelines. In this blog, we will explore how Terraform simplifies infrastructure deployment for CI/CD pipelines, guide you through setting up Terraform, and demonstrate a simple implementation example.


Why Automate Infrastructure Deployment in CI/CD?

Image description

Automating infrastructure provisioning as part of CI/CD pipelines ensures:

  • Consistency: Resources are created and managed in a repeatable manner.
  • Efficiency: Reduces manual effort and speeds up pipeline execution.
  • Scalability: Easily scales infrastructure as application demands grow.
  • Version Control: Enables tracking and rollback of infrastructure changes.
  • Cost Optimization: Automates resource cleanup post-deployment or testing.

Setting Up Terraform

Image description

To use Terraform in your CI/CD workflows, you must first set up the tool on your local machine or pipeline environment. Follow these steps:

Step 1: Install Terraform

  1. Download Terraform:

    Visit the official Terraform website and download the appropriate binary for your operating system.

  2. Install Terraform:

    • Linux/macOS: Extract the binary and move it to a directory in your PATH. For example:
     unzip terraform_<version>_linux_amd64.zip
     sudo mv terraform /usr/local/bin/
    
  • Windows: Extract the binary and add the directory to your system's PATH.
  1. Verify Installation: Run the following command to confirm Terraform is installed:
   terraform --version
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure a Cloud Provider

Terraform needs credentials to interact with cloud providers. For example, if using AWS:

  1. Install AWS CLI:

    Follow the AWS CLI installation guide.

  2. Set Up AWS Credentials:

    Run the following command and provide your AWS access key and secret key:

   aws configure
Enter fullscreen mode Exit fullscreen mode
  1. Verify Access: Confirm that the credentials are working by running:
   aws s3 ls
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize a Terraform Project

  1. Create a Project Directory: Organize your Terraform configurations in a dedicated directory:
   mkdir terraform-project
   cd terraform-project
Enter fullscreen mode Exit fullscreen mode
  1. Write a Configuration File:

    Create a main.tf file to define the resources (an example is provided later).

  2. Initialize Terraform:

    Run the following command to download provider plugins and prepare the working directory:

   terraform init
Enter fullscreen mode Exit fullscreen mode

Sample Use Case: Deploying Infrastructure for a Web Application

Imagine a simple scenario where a CI/CD pipeline needs to provision:

  • A virtual machine for running the application.
  • A load balancer to manage traffic.
  • A database instance.

We’ll use Terraform to automate this.


Step 1: Prepare Terraform Configuration Files

main.tf

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "app_server" {
  ami           = "ami-0c02fb55956c7d316" # Example AMI ID
  instance_type = "t2.micro"
  tags = {
    Name = "AppServer"
  }
}

resource "aws_elb" "app_lb" {
  name               = "app-lb"
  availability_zones = ["us-east-1a", "us-east-1b"]

  listener {
    instance_port     = 80
    instance_protocol = "HTTP"
    lb_port           = 80
    lb_protocol       = "HTTP"
  }

  health_check {
    target              = "HTTP:80/"
    interval            = 30
    timeout             = 5
    healthy_threshold   = 2
    unhealthy_threshold = 2
  }

  instances = [aws_instance.app_server.id]
}

resource "aws_db_instance" "app_db" {
  identifier            = "app-db"
  engine                = "mysql"
  instance_class        = "db.t2.micro"
  allocated_storage     = 20
  name                  = "appdb"
  username              = "admin"
  password              = "password"
  publicly_accessible   = true
  skip_final_snapshot   = true
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Integrate Terraform with a CI/CD Pipeline

Most CI/CD tools (e.g., GitHub Actions, Jenkins, GitLab CI/CD) support Terraform. Here’s how to integrate Terraform with GitHub Actions.

GitHub Actions Workflow File: .github/workflows/terraform.yml

name: Terraform Deployment

on:
  push:
    branches:
      - main

jobs:
  terraform:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

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

      - name: Terraform Init
        run: terraform init

      - name: Terraform Plan
        run: terraform plan

      - name: Terraform Apply
        run: terraform apply -auto-approve
Enter fullscreen mode Exit fullscreen mode

Step 3: Execute the Pipeline

  1. Push your Terraform configurations and GitHub Actions workflow to your repository.
  2. Upon pushing changes to the main branch, the GitHub Actions pipeline will:
    • Initialize Terraform.
    • Generate and display the execution plan.
    • Apply the configurations to provision resources.

Key Considerations for CI/CD Automation

Image description

  • State Management: Use remote backends (e.g., AWS S3) to store Terraform state securely and enable collaboration.
  • Secrets Handling: Manage sensitive data (e.g., database credentials) securely using tools like AWS Secrets Manager or HashiCorp Vault.
  • Rollback Strategy: Automate resource cleanup in case of pipeline failures to minimize costs and maintain resource hygiene.
  • Testing: Incorporate automated testing tools like Terratest to validate infrastructure deployments.

Goodluck!

Image description

Setting up and using Terraform in CI/CD pipelines automates infrastructure provisioning, ensuring faster, consistent, and reliable deployments. By defining infrastructure as code, teams can focus on delivering high-quality applications without manual setup overhead.

Terraform’s versatility and wide cloud provider support make it a powerful tool for modern DevOps workflows. Start small by automating one piece of your infrastructure and scale gradually as you refine your pipeline processes.


Have questions or insights about Terraform in CI/CD? Let’s discuss in the comments!

Top comments (0)