Back
📦Automationgeneric
Terraform Infrastructure Template
Generate production-ready Terraform configurations for AWS. Includes VPC, ECS, RDS, S3, and monitoring with best practices baked in.
by InfraPro·50 days ago·
terraformAWSinfrastructureDevOpsIaC
# Terraform AWS Infrastructure Template
# Generates a production-ready VPC with ECS Fargate, RDS, and supporting resources
## Usage
Replace all values in locals {} with your project specifics, then run:
```bash
terraform init && terraform plan && terraform apply
```
```hcl
terraform {
required_version = ">= 1.5"
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
backend "s3" {
bucket = "your-terraform-state-bucket"
key = "infrastructure/terraform.tfstate"
region = "eu-west-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
locals {
project = "myproject"
environment = "production"
region = "eu-west-1"
domain = "myproject.com"
tags = {
Project = local.project
Environment = local.environment
ManagedBy = "terraform"
}
}
# VPC with public + private subnets across 3 AZs
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "${local.project}-${local.environment}"
cidr = "10.0.0.0/16"
azs = ["${local.region}a", "${local.region}b", "${local.region}c"]
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
single_nat_gateway = false # HA: one per AZ
tags = local.tags
}
# ECS Fargate Cluster
resource "aws_ecs_cluster" "main" {
name = "${local.project}-${local.environment}"
setting {
name = "containerInsights"
value = "enabled"
}
tags = local.tags
}
# RDS PostgreSQL
module "db" {
source = "terraform-aws-modules/rds/aws"
version = "~> 6.0"
identifier = "${local.project}-${local.environment}"
engine = "postgres"
engine_version = "16"
instance_class = "db.t4g.medium"
allocated_storage = 100
storage_encrypted = true
vpc_security_group_ids = [module.security_group.security_group_id]
db_subnet_group_name = module.vpc.database_subnet_group
backup_retention_period = 30
deletion_protection = true
tags = local.tags
}
```
Customize: instance sizes, regions, services, monitoring thresholds.