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 (7)

Collapse
 
nabeelmohamed profile image
Mohamed Nabeel

Clear cut explanations Brother. but i think the github actions should use correct aws account, for that we can add secrets in the repository settings and add a step in workflow as

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
.

also i think we should not apply all the updates as final human checks is must. also bro if we want to take it to the next level we should a future updates for particular files like

 - name: Terraform Plan
        run: terraform plan -target=${{ inputs.resource_type }}.${{ inputs.resource_name }} -out=tfplan
Enter fullscreen mode Exit fullscreen mode

. Finally we can add flexbility like what we want to do with the targeted resource with destroy or plan or apply.

Collapse
 
asi_security profile image
Isaeus "Asi" Guiang

thank your for your feedback brother!

You're absolutely right! I did overlook configuring AWS credentials with repository secrets for security. I agree that a final manual review step is important to avoid unintended changes, thanks for that I'll keep that in mind!!

using terraform plan -target for specific resources is a great for flexibility. would love to connect with you on socials to discuss this further!

Appreciate the insights brother!

Collapse
 
jorgecontreras profile image
Jorge Contreras

Great post, Asi! Automating infrastructure provisioning with IaC tools like terraform makes the process smooth. What would be your take on production environments? Do you think adding a manual action for a human to review would be a good practice? With the amazing power of terraform and the potential of inadvertently destroying resources, I would definitely add such mechanisms.

Collapse
 
asi_security profile image
Isaeus "Asi" Guiang

Thanks for the kind words!

adding a manual review step for production environments is a must. tho streamlines provisioning, the stakes are much higher in production.

safeguard like requiring human permission before applying changes, helps prevent unintended resource destruction and ensures compliance with operational standards.

Balancing automation with caution is always a good practice!

also always apply the principle of least privilege and role-based access controls!

Collapse
 
vighnesh_sl_977ca069be726 profile image
Vighnesh SL

wonderfull

Collapse
 
tythos profile image
Brian Kirkpatrick

Terraform can be addictive. There's a drunk feeling of power--I DECLARE I WANT THIS. GO! SPIN UP WHAT I DESIRE! FIGURE IT OUT, AND MAKE IT SO!

Collapse
 
asi_security profile image
Isaeus "Asi" Guiang • Edited

It's the same feeling the first time I experienced using AWS IAM, I FEEL LIKE A GOD!