🏗️
Infrastructure as Code

Terraform

Infrastructure as Code tool by HashiCorp

🐧 Linux🍎 Mac🪟 Windows📋 Quick reference →
Reviewed: Tested on: Terraform 1.8 · AWS provider 5.x · Ubuntu 22.04

If you're on Terraform 1.7 or older

  • S3 native locking (use_lockfile) differs from DynamoDB — don't mix backends mid-migration
  • Provider version constraints: run terraform init -upgrade after bump
  • terraform test (1.6+) replaces some external test harness patterns

✓ Team golden path — Infrastructure as Code

Industry default; large module ecosystem

What is this?

Terraform creates cloud servers and networks from config files instead of clicking in a web console.

📁

Config files for Terraform

Where to create or edit the main configuration — paths below match the setup steps.

  • main.tf

    Location: Terraform module directory

    Providers, resources, and module calls

  • variables.tf

    Location: Same module

    Input variables and defaults

  • terraform.tfvars

    Location: Same module (often gitignored)

    Environment-specific variable values

📥

Step 01

Install Terraform

(01)Install Terraform

Linux
1# HashiCorp official repo (Ubuntu/Debian)
2wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
3echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
4sudo apt update && sudo apt install terraform
5
6# Or direct download
7curl -fsSL https://releases.hashicorp.com/terraform/1.10.0/terraform_1.10.0_linux_amd64.zip -o terraform.zip
8unzip terraform.zip && sudo mv terraform /usr/local/bin/

(02)Enable Tab Completion

Linux
1terraform -install-autocomplete
2# Add to ~/.bashrc or ~/.zshrc:
3# complete -C /usr/bin/terraform terraform

Step 02

Verify Installation

(01)Check Version

Linux
1terraform version

Step 03

Manage Terraform

(01)Initialize & Plan

Linux
1cd your-project/
2terraform init
3terraform validate
4terraform plan -out=tfplan

(02)Apply & Destroy

Linux
1terraform apply tfplan
2terraform apply -auto-approve
3terraform destroy

(03)State Management

Linux
1terraform state list
2terraform state show aws_instance.example
3terraform import aws_instance.example i-1234567890abcdef0
4terraform fmt -recursive
🔧

Step 04

Common Problems

#1Error acquiring the state lock

Another terraform apply is running or a previous run crashed.

Linux
1# Wait for other job to finish, or force unlock (use carefully):
2terraform force-unlock <LOCK_ID>
3# LOCK_ID from error message
4# If using remote backend, check DynamoDB/S3 lock table

#2Provider authentication failed

Linux
1# AWS:
2aws sts get-caller-identity
3export AWS_PROFILE=your-profile
4# Re-run: terraform plan

📋Config templates

3 YAML templates for Terraform. Copy and deploy after setup.

3 ready-to-copy templates. Expand one, copy the YAML, then run the deploy commands.

main.tf

yaml
1terraform {
2 required_providers {
3 aws = {
4 source = "hashicorp/aws"
5 version = "~> 5.0"
6 }
7 }
8}
9
10provider "aws" {
11 region = var.aws_region
12}
13
14resource "aws_security_group" "ubuntu_sg" {
15 name = "ubuntu-sg"
16 description = "Allow SSH and HTTP"
17
18 ingress {
19 from_port = 22
20 to_port = 22
21 protocol = "tcp"
22 cidr_blocks = ["0.0.0.0/0"]
23 }
24
25 ingress {
26 from_port = 80
27 to_port = 80
28 protocol = "tcp"
29 cidr_blocks = ["0.0.0.0/0"]
30 }
31
32 egress {
33 from_port = 0
34 to_port = 0
35 protocol = "-1"
36 cidr_blocks = ["0.0.0.0/0"]
37 }
38}
39
40resource "aws_instance" "ubuntu" {
41 ami = var.ami
42 instance_type = var.instance_type
43 key_name = var.key_name
44 vpc_security_group_ids = [aws_security_group.ubuntu_sg.id]
45
46 tags = {
47 Name = "Ubuntu-EC2"
48 }
49}

variables.tf

yaml
1variable "aws_region" {
2 default = "us-east-1"
3}
4
5variable "ami" {
6 description = "Ubuntu AMI ID for your region"
7 default = "ami-0c7217cdde317cfec" # Ubuntu 22.04 us-east-1 — update for your region
8}
9
10variable "instance_type" {
11 default = "t2.medium"
12}
13
14variable "key_name" {
15 description = "Existing EC2 key pair name"
16}

outputs.tf

yaml
1output "instance_public_ip" {
2 value = aws_instance.ubuntu.public_ip
3}
4
5output "instance_id" {
6 value = aws_instance.ubuntu.id
7}
📄

Step 01

Provision EC2

(01)Initialize and apply

Linux
1terraform init
2terraform plan
3terraform apply -auto-approve
4terraform output instance_public_ip
5ssh -i ~/.ssh/mykey.pem ubuntu@$(terraform output -raw instance_public_ip)

(02)Destroy when done

Linux
1terraform destroy -auto-approve