What is Infrastructure as Code?
Infrastructure as Code (IaC) is the practice of managing and provisioning infrastructure (servers, networks, databases) through machine-readable definition files, rather than manual processes or interactive configuration tools. The benefits are huge: repeatability, version control, auditability, and speed.
Terraform by HashiCorp is one of the most popular IaC tools. It's cloud-agnostic, meaning you can use the same workflow to manage AWS, Azure, GCP, or even on-prem resources.
Core Concepts
1. Declarative Configuration
You describe the desired state of your infrastructure in .tf files. Terraform figures out what to create, update, or delete to reach that state.
2. Providers
Providers are plugins that let Terraform interact with cloud APIs. For example, the AWS provider knows how to create EC2 instances, S3 buckets, etc.
provider "aws" {
region = "us-west-2"
}
3. Resources
Resources are the actual infrastructure components you manage. Each resource block has a type (e.g., aws_instance) and a local name.
resource "aws_instance" "my_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
4. State
Terraform keeps a state file (terraform.tfstate) that maps your config to real-world resources. It's critical for tracking what's deployed. Never edit it manually.
Your First Terraform Project
Let's walk through a minimal example: deploying an AWS EC2 instance.
Step 1: Install Terraform
Download from terraform.io/downloads and add to your PATH.
Step 2: Create a project directory
mkdir learn-terraform
cd learn-terraform
Step 3: Write main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "TerraformExample"
}
}
Step 4: Initialize
terraform init
This downloads the AWS provider plugin.
Step 5: Plan
terraform plan
Shows what will be created. Review it carefully.
Step 6: Apply
terraform apply
Type yes to confirm. Terraform creates the EC2 instance.
Step 7: Destroy (when done)
terraform destroy
Cleans up all resources defined in your config.
Variables and Outputs
Hardcoding values is bad practice. Use variables to make configs reusable.
variable "instance_type" {
default = "t2.micro"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
}
Outputs let you expose useful information after apply:
output "instance_ip" {
value = aws_instance.example.public_ip
}
Best Practices
-
Use remote state: Store
terraform.tfstatein a shared backend like S3 (with DynamoDB for locking). Never commit state to Git. - Organize modules: Break large configs into reusable modules.
-
Version control everything: All
.tffiles belong in Git. -
Use
terraform fmt: Auto-format your code. -
Validate with
terraform validate: Catch syntax errors early.
Conclusion
Terraform makes infrastructure management systematic and reproducible. Start small, learn the workflow (init, plan, apply, destroy), and gradually adopt more advanced features like modules, workspaces, and provisioning. The official Terraform documentation is excellent when you're ready to go deeper.
Top comments (0)