How to use infrastructure as code with Terraform

· Category: Cloud Computing

Short answer

Terraform uses declarative configuration files to create, update, and version infrastructure across cloud providers.

Steps

  1. Install Terraform and configure provider credentials.
  2. Write a .tf file:
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t3.micro"
}
  1. Initialize:
terraform init
  1. Plan and apply:
terraform plan
terraform apply

Tips

  • Use remote state backends (S3, Azure Blob) for team collaboration.
  • Modules promote reuse and standardization.
  • Run terraform fmt and validate in CI pipelines.

Common issues

  • State conflicts: use state locking with DynamoDB or Terraform Cloud.
  • Drift: resources changed manually cause unexpected plans.