# terraform-infrastructure > Infrastructure as Code with Terraform - Author: joseluis - Repository: joseluissaorin/clopus - Version: 20251231102703 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/joseluissaorin/clopus - Web: https://mule.run/skillshub/@@joseluissaorin/clopus~terraform-infrastructure:20251231102703 --- --- name: terraform-infrastructure description: Infrastructure as Code with Terraform version: 1.0.0 category: devops technologies: [terraform, aws, gcp, azure, iac] triggers: - terraform - infrastructure as code - iac - cloud infrastructure --- # Terraform Infrastructure Infrastructure as Code for cloud resources. ## Project Structure ``` infrastructure/ ├── main.tf ├── variables.tf ├── outputs.tf ├── providers.tf ├── terraform.tfvars ├── modules/ │ ├── vpc/ │ ├── eks/ │ └── rds/ └── environments/ ├── dev/ ├── staging/ └── prod/ ``` ## Provider Configuration ```hcl # providers.tf terraform { required_version = ">= 1.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } backend "s3" { bucket = "my-terraform-state" key = "prod/terraform.tfstate" region = "us-east-1" encrypt = true dynamodb_table = "terraform-locks" } } provider "aws" { region = var.aws_region default_tags { tags = { Environment = var.environment ManagedBy = "Terraform" Project = var.project_name } } } ``` ## VPC Module ```hcl # modules/vpc/main.tf resource "aws_vpc" "main" { cidr_block = var.vpc_cidr enable_dns_hostnames = true enable_dns_support = true tags = { Name = "${var.name}-vpc" } } resource "aws_subnet" "public" { count = length(var.availability_zones) vpc_id = aws_vpc.main.id cidr_block = cidrsubnet(var.vpc_cidr, 4, count.index) availability_zone = var.availability_zones[count.index] map_public_ip_on_launch = true tags = { Name = "${var.name}-public-${count.index + 1}" Type = "public" } } resource "aws_subnet" "private" { count = length(var.availability_zones) vpc_id = aws_vpc.main.id cidr_block = cidrsubnet(var.vpc_cidr, 4, count.index + length(var.availability_zones)) availability_zone = var.availability_zones[count.index] tags = { Name = "${var.name}-private-${count.index + 1}" Type = "private" } } resource "aws_internet_gateway" "main" { vpc_id = aws_vpc.main.id tags = { Name = "${var.name}-igw" } } resource "aws_nat_gateway" "main" { allocation_id = aws_eip.nat.id subnet_id = aws_subnet.public[0].id tags = { Name = "${var.name}-nat" } } ``` ## EKS Cluster ```hcl # modules/eks/main.tf module "eks" { source = "terraform-aws-modules/eks/aws" version = "~> 19.0" cluster_name = var.cluster_name cluster_version = "1.28" vpc_id = var.vpc_id subnet_ids = var.private_subnet_ids cluster_endpoint_public_access = true eks_managed_node_groups = { default = { min_size = 2 max_size = 10 desired_size = 3 instance_types = ["t3.medium"] labels = { Environment = var.environment } } } tags = var.tags } ``` ## RDS Database ```hcl # modules/rds/main.tf resource "aws_db_instance" "main" { identifier = var.db_name engine = "postgres" engine_version = "15.4" instance_class = var.instance_class allocated_storage = 20 max_allocated_storage = 100 storage_encrypted = true db_name = var.db_name username = var.db_username password = var.db_password vpc_security_group_ids = [aws_security_group.rds.id] db_subnet_group_name = aws_db_subnet_group.main.name backup_retention_period = 7 backup_window = "03:00-04:00" maintenance_window = "Mon:04:00-Mon:05:00" deletion_protection = var.environment == "prod" skip_final_snapshot = var.environment != "prod" tags = var.tags } resource "aws_security_group" "rds" { name_prefix = "${var.db_name}-rds-" vpc_id = var.vpc_id ingress { from_port = 5432 to_port = 5432 protocol = "tcp" security_groups = var.allowed_security_groups } } ``` ## Variables & Outputs ```hcl # variables.tf variable "environment" { description = "Environment name" type = string } variable "aws_region" { description = "AWS region" type = string default = "us-east-1" } variable "vpc_cidr" { description = "VPC CIDR block" type = string default = "10.0.0.0/16" } # outputs.tf output "vpc_id" { description = "VPC ID" value = module.vpc.vpc_id } output "eks_cluster_endpoint" { description = "EKS cluster endpoint" value = module.eks.cluster_endpoint } output "rds_endpoint" { description = "RDS endpoint" value = module.rds.endpoint sensitive = true } ``` ## Common Commands ```bash # Initialize terraform init # Plan changes terraform plan -out=tfplan # Apply changes terraform apply tfplan # Destroy (careful!) terraform destroy # Format code terraform fmt -recursive # Validate configuration terraform validate # Import existing resource terraform import aws_instance.example i-1234567890abcdef0 # State management terraform state list terraform state show aws_instance.example ``` ## Best Practices 1. Use remote state with locking 2. Organize with modules 3. Use workspaces or separate directories per environment 4. Never commit secrets to version control 5. Use data sources for existing resources 6. Implement proper tagging 7. Use terraform fmt and validate in CI 8. Review plans before applying