Terraform: Define tu Infraestructura en Codigo
Terraform de HashiCorp es la herramienta de Infrastructure as Code (IaC) mas utilizada. Permite crear, modificar y versionar infraestructura cloud de forma segura y reproducible. En lugar de hacer click en la consola de AWS, describes tu infraestructura en archivos HCL.
# main.tf - VPC + EKS cluster
terraform {
required_providers {
aws = { source = 'hashicorp/aws', version = '~> 5.0' }
}
backend 's3' {
bucket = 'mi-empresa-terraform-state'
key = 'production/terraform.tfstate'
region = 'us-east-1'
}
}
provider 'aws' {
region = var.aws_region
}
module 'vpc' {
source = 'terraform-aws-modules/vpc/aws'
version = '5.0.0'
name = 'production-vpc'
cidr = '10.0.0.0/16'
azs = ['us-east-1a', 'us-east-1b', 'us-east-1c']
private_subnets = ['10.0.1.0/24', '10.0.2.0/24', '10.0.3.0/24']
public_subnets = ['10.0.101.0/24', '10.0.102.0/24', '10.0.103.0/24']
enable_nat_gateway = true
}
module 'eks' {
source = 'terraform-aws-modules/eks/aws'
version = '19.0.0'
cluster_name = 'production'
cluster_version = '1.28'
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
}| Comando | Descripcion |
|---|---|
| terraform init | Inicializar, descargar providers |
| terraform plan | Ver cambios antes de aplicar |
| terraform apply | Aplicar cambios a la infraestructura |
| terraform destroy | Eliminar toda la infraestructura |
| terraform state list | Listar recursos gestionados |
🚀 Dato Clave
SIEMPRE usa terraform plan antes de apply. SIEMPRE guarda el state en un backend remoto (S3, GCS). NUNCA compartas el state file — contiene secrets en texto plano.
Terraform es esencial para cualquier DevOps profesional. Practica creando y destruyendo infraestructura.