Terraform for Multi-Cloud IaC: HCL, State & Modular Architectures

Developed by HashiCorp, Terraform is the undisputed industry standard tool for building, changing, and versioning cloud infrastructure safely and efficiently. Unlike vendor-locked tools (such as CloudFormation for AWS or ARM/Bicep for Azure), Terraform is cloud-agnostic, allowing engineers to manage AWS, Azure, Google Cloud, Cloudflare, GitHub, and Kubernetes clusters using a unified declarative syntax called HashiCorp Configuration Language (HCL).

The Core Terraform Workflow (Init, Plan, Apply, Destroy)

  1. terraform init: Initializes the working directory containing Terraform configuration files. Downloads necessary cloud provider plugins (e.g., hashicorp/aws) and configures the remote backend.
  2. terraform plan: Reads the current state file, refreshes live cloud infrastructure via APIs, calculates the difference (diff), and generates an execution plan outlining exactly what will be created (+), modified (~), or destroyed (-).
  3. terraform apply: Prompts for human confirmation, then executes the planned API calls against the cloud provider in the correct dependency order. Updates the state file upon completion.
  4. terraform destroy: Safely tears down and deletes all resources managed by the configuration file (ideal for temporary testing environments).

Production Remote State Management (S3 + DynamoDB Locking)

In professional engineering teams, storing the terraform.tfstate file on a developer's local laptop is a critical anti-pattern. If two developers run terraform apply simultaneously, the state file corrupts. Production Terraform requires a Remote Backend with State Locking:

# backend.tf - Production Remote State with S3 and DynamoDB Locking
terraform {
  required_version = ">= 1.5.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }

  backend "s3" {
    bucket         = "veewom-terraform-state-prod"
    key            = "vpc/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "terraform-state-locks" # Prevents concurrent applies!
  }
}

Example: Provisioning an AWS VPC with Terraform HCL

# main.tf - Declarative VPC & Subnet
resource "aws_vpc" "main" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support   = true

  tags = {
    Name        = "Veewom-Production-VPC"
    Environment = "production"
    ManagedBy   = "Terraform"
  }
}

resource "aws_subnet" "public_1" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = true

  tags = {
    Name = "Veewom-Public-Subnet-1"
  }
}

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is the purpose of the Terraform state file?

The terraform.tfstate file maps the declarative resource definitions in your HCL code to the real-world resource IDs created in the cloud provider's API. It also tracks metadata and resource dependencies so Terraform knows the exact sequence to provision or destroy resources.

Q2. Can Terraform manage resources across multiple cloud providers in a single project?

Yes. Terraform allows you to define multiple provider blocks (e.g., provider 'aws' and provider 'google') in the same project. You can provision an AWS S3 bucket and pass its URL directly to a Google Cloud Run service in a single unified execution!

Explore Related Cloud Computing Tutorials & Next Steps

Broaden your enterprise cloud architecture and DevOps expertise across our curriculum:

Infrastructure as Code (IaC) Principles

Review state management, provider blocks, variables, outputs, and modular infrastructure architecture.

CloudFormation & Azure Bicep IaC

Evaluate Terraform versus native AWS CloudFormation and Azure Bicep for enterprise infrastructure.

10 Hands-On Cloud Practice Labs

Hands-on project: Provision a complete 3-tier VPC network with EC2 and RDS using Terraform.

Cloud Master Cheat Sheet & CLI

Essential Terraform CLI reference: init, plan, apply, destroy, state list, and import commands.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!