Terraform on Linux: Infrastructure as Code from Scratch on AWS
Terraform is a HashiCorp Infrastructure as Code (IaC) tool that allows you to create, modify, and destroy cloud infrastructure using versionable configuration files. Instead of clicking in the AWS console, you describe what you want and Terraform creates it.
Why Use Terraform?
- Versionable: infrastructure in Git, with a complete change history
- Reproducible: recreates the identical environment in any account/region in seconds
- Multi-cloud: the same workflow for AWS, GCP, Azure, DigitalOcean, and +300 providers
- Plan before applying: you see exactly what will change before confirming
- Avoids drift: the desired state in code is the source of truth
Installation on Linux
# Ubuntu/Debian — via repositório oficial HashiCorp
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "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
sudo apt update && sudo apt install terraform -y
terraform version
Terraform Project Structure
projeto/
├── main.tf # recursos principais
├── variables.tf # declaração de variáveis
├── outputs.tf # valores expostos após o apply
├── terraform.tfvars # valores das variáveis (não versionar com secrets!)
└── backend.tf # onde salvar o estado (S3, etc.)
First Resource: EC2 on AWS
# main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.aws_region
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2
instance_type = var.instance_type
tags = {
Name = "meu-servidor-web"
Environment = "producao"
ManagedBy = "terraform"
}
}
# variables.tf
variable "aws_region" {
description = "Região AWS"
type = string
default = "us-east-1"
}
variable "instance_type" {
description = "Tipo da instância EC2"
type = string
default = "t3.micro"
}
# outputs.tf
output "instance_id" {
value = aws_instance.web.id
}
output "instance_public_ip" {
value = aws_instance.web.public_ip
}
The Basic Workflow
# 1. Inicializa: baixa providers e configura o backend
terraform init
# 2. Valida a sintaxe dos arquivos .tf
terraform validate
# 3. Formata o código (boa prática antes de commitar)
terraform fmt
# 4. Mostra o plano de mudanças (read-only, não altera nada)
terraform plan
# 5. Aplica as mudanças (pede confirmação)
terraform apply
# 6. Aplica sem pedir confirmação (usar em CI/CD)
terraform apply -auto-approve
# 7. Destroi toda a infraestrutura
terraform destroy
Terraform State
Terraform maintains a terraform.tfstate file that records the current state of the infrastructure. In teams, this state should be stored remotely to avoid conflicts:
# backend.tf
terraform {
backend "s3" {
bucket = "meu-terraform-state"
key = "producao/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-lock" # evita apply simultâneo
encrypt = true
}
}
Modules: Reusing Infrastructure
Modules are reusable blocks of infrastructure. The community has already published hundreds on registry.terraform.io:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "minha-vpc"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
single_nat_gateway = true
}
# Referencia o output do módulo
output "vpc_id" {
value = module.vpc.vpc_id
}
Useful Day-to-Day Commands
# Ver o estado atual sem conectar à nuvem
terraform show
# Listar recursos no state
terraform state list
# Importar recurso existente para o state
terraform import aws_instance.web i-1234567890abcdef0
# Recarregar modules após alteração
terraform get -update
# Visualizar grafo de dependências
terraform graph | dot -Tsvg > grafo.svg
Terraform in the Job Market
Terraform is the most requested IaC tool in DevOps, SRE, and Cloud Engineer job postings. HashiCorp offers the Terraform Associate, an accessible certification for those with 6 months of practice — and it appears with increasing frequency in senior job posts.
The LPIC-1 + Terraform + AWS combination is today the most demanded profile for mid/senior-level infrastructure positions.
That was just one piece. The course teaches the whole path — from your first command to exam day.