What is Terraform?
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire datacenter.
# Configure the AWS Provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2"
}
provider "aws" {
region = "us-west-2"
}
# Create a VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main-vpc"
}
}
# Create an Internet Gateway
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main-igw"
}
}
# Create a subnet
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = true
tags = {
Name = "public-subnet"
}
}
# Create an EC2 instance
resource "aws_instance" "web" {
ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
subnet_id = aws_subnet.public.id
tags = {
Name = "web-server"
}
}
# Output the public IP
output "instance_public_ip" {
value = aws_instance.web.public_ip
}
Key Features
- Infrastructure as Code: Define infrastructure using declarative configuration files
- Execution Plans: Preview changes before applying them
- Resource Graph: Build a graph of all resources and parallelize creation/modification
- Change Automation: Apply complex changesets with minimal human interaction
- Multi-Cloud: Support for multiple cloud providers and services
- State Management: Keep track of resource metadata and improve performance
- Modules: Reusable configurations for common infrastructure patterns
Career Impact
Average salary for Terraform engineers
Growth in IaC job postings
Of DevOps teams use Terraform
Learning Path
- Learn cloud computing basics
- Understand Infrastructure as Code concepts
- Practice with Terraform syntax (HCL)
- Master state management
- Learn modules and best practices
- Explore advanced features and providers