Automating Infrastructure Provisioning with Terraform and Managing Configuration Drift


In today's rapidly evolving IT environments, automation is key to maintaining scalable, consistent, and reliable infrastructure. Terraform, an open-source infrastructure-as-code (IaC) tool, has gained tremendous popularity for its ability to provision, manage, and maintain infrastructure across multiple cloud providers. Terraform's declarative language allows teams to define infrastructure in code, which can be version-controlled and managed in a consistent manner.


What is Infrastructure Provisioning with Terraform?

Infrastructure provisioning refers to the process of creating and configuring IT resources such as virtual machines, databases, networks, and storage systems. Terraform simplifies this by allowing users to define infrastructure using configuration files, which are then executed to provision resources in a specific environment.

How Terraform Works

Terraform works by using a configuration file written in HCL (HashiCorp Configuration Language). When you run terraform apply, Terraform communicates with the target provider (AWS, Google Cloud, Azure, etc.) to create or modify resources based on the configurations.

Terraform follows these steps:

  1. Write Infrastructure Code: Define resources and their configurations in .tf files.
  2. Initialize Terraform: Run terraform init to initialize the working directory and download provider plugins.
  3. Plan the Changes: Execute terraform plan to see what changes will occur.
  4. Apply Changes: Run terraform apply to provision the infrastructure.

Automating Infrastructure with Terraform: Example Code

Let's dive into an example of how Terraform can automate infrastructure provisioning. In this example, we will provision an AWS EC2 instance.

Step 1: Install Terraform

First, make sure that Terraform is installed on your local machine. You can download Terraform from here.

Step 2: Create a Configuration File

Create a file called main.tf and define the configuration for the EC2 instance.

# Define the AWS provider
provider "aws" {
  region = "us-west-2"
}

# Define the EC2 instance resource
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0" # Use the latest Amazon Linux AMI
  instance_type = "t2.micro"
  
  tags = {
    Name = "Terraform Example Instance"
  }
}

Step 3: Initialize, Plan, and Apply

Run the following commands in your terminal to initialize Terraform, see the planned changes, and apply the configuration:

# Initialize Terraform and download the required providers
terraform init

# Show the execution plan
terraform plan

# Apply the plan and provision the EC2 instance
terraform apply

Step 4: Verify Provisioned Resources

Once the Terraform apply command is complete, you can verify that the EC2 instance has been created by logging into your AWS Console and navigating to EC2 > Instances.


What is Configuration Drift?

Configuration Drift occurs when the actual state of your infrastructure diverges from the desired state defined in your code. This can happen due to manual changes, updates outside of Terraform (like direct changes in the cloud console), or untracked modifications over time. Drift can lead to inconsistencies and errors, making it harder to maintain a stable environment.

Common Causes of Configuration Drift

  1. Manual Changes: When a team member manually modifies the infrastructure (e.g., changes to security groups, VPC settings) outside of Terraform.
  2. External Changes: Changes made directly through a cloud provider’s console (e.g., AWS Console, Azure Portal).
  3. Partial Deployments: When Terraform is not run to completion, some resources may be left in an inconsistent state.
  4. Version Updates: Terraform updates may cause discrepancies if the new version introduces breaking changes.

Detecting and Managing Configuration Drift with Terraform

The key to managing configuration drift is continuous monitoring and regularly reapplying the Terraform configurations. Here's how you can prevent and handle configuration drift.

1. Using Terraform State Files

Terraform maintains a state file (typically terraform.tfstate) which keeps track of the resources it manages. The state file helps Terraform compare the actual state of resources with the desired state specified in the configuration files. If there is drift, Terraform can reconcile the difference and apply the necessary changes.

2. Manual Drift Detection

You can detect drift manually by running terraform plan. This command compares the current infrastructure state with the state defined in your .tf files. If there are any discrepancies, Terraform will report them.

# Run terraform plan to check for any configuration drift
terraform plan

If drift is detected, Terraform will prompt you with a plan to bring the infrastructure back in line with the configuration files.

3. Automating Drift Detection with Terraform Cloud or Terraform Enterprise

For a more automated approach, consider using Terraform Cloud or Terraform Enterprise, which includes features like:

  • Remote State Management: Helps track infrastructure state across teams and environments.
  • Drift Detection: Monitors and reports changes that have occurred outside of Terraform, helping you spot drift.
  • Policy as Code: Enforces policies that can prevent or correct drifts in infrastructure.

Preventing Configuration Drift

1. Apply Terraform Regularly

Automating the application of Terraform configurations via a CI/CD pipeline ensures that your infrastructure remains consistent. By running terraform plan and terraform apply in your pipeline, you can regularly check for and correct any drift.

Example: CI/CD Integration with Terraform and GitHub Actions

You can integrate Terraform with a CI/CD pipeline like GitHub Actions to automate drift detection and resolution.

name: Terraform CI/CD

on:
  push:
    branches:
      - main

jobs:
  terraform:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Terraform
        uses: hashicorp/setup-terraform@v1

      - name: Terraform Init
        run: terraform init

      - name: Terraform Plan
        run: terraform plan

      - name: Terraform Apply
        run: terraform apply -auto-approve

This GitHub Actions workflow automatically runs terraform plan and terraform apply whenever code is pushed to the main branch, ensuring that infrastructure is always in sync with the desired state.

2. Use terraform refresh

The terraform refresh command updates the state file with the current infrastructure status. Running terraform refresh helps you stay up-to-date with any changes that might have occurred outside Terraform's management.

# Refresh the Terraform state file with the current infrastructure state
terraform refresh