|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + aws = { |
| 4 | + source = "hashicorp/aws" |
| 5 | + version = "~> 5.0" |
| 6 | + } |
| 7 | + } |
| 8 | + required_version = ">= 1.4.0" |
| 9 | +} |
| 10 | + |
| 11 | +provider "aws" { |
| 12 | + region = var.region |
| 13 | +} |
| 14 | + |
| 15 | +# |
| 16 | +# Web Tier Security Group |
| 17 | +# |
| 18 | +resource "aws_security_group" "web_tier" { |
| 19 | + name = "web-tier-sg" |
| 20 | + description = "Security group for web tier - allows public HTTP/HTTPS" |
| 21 | + vpc_id = var.vpc_id |
| 22 | + |
| 23 | + ingress { |
| 24 | + description = "HTTP from public internet" |
| 25 | + from_port = 80 |
| 26 | + to_port = 80 |
| 27 | + protocol = "tcp" |
| 28 | + cidr_blocks = ["0.0.0.0/0"] |
| 29 | + } |
| 30 | + |
| 31 | + ingress { |
| 32 | + description = "HTTPS from public internet" |
| 33 | + from_port = 443 |
| 34 | + to_port = 443 |
| 35 | + protocol = "tcp" |
| 36 | + cidr_blocks = ["0.0.0.0/0"] |
| 37 | + } |
| 38 | + |
| 39 | + ingress { |
| 40 | + description = "SSH from bastion host" |
| 41 | + from_port = 22 |
| 42 | + to_port = 22 |
| 43 | + protocol = "tcp" |
| 44 | + security_groups = [aws_security_group.bastion.id] |
| 45 | + } |
| 46 | + |
| 47 | + egress { |
| 48 | + description = "Allow all outbound" |
| 49 | + from_port = 0 |
| 50 | + to_port = 0 |
| 51 | + protocol = "-1" |
| 52 | + cidr_blocks = ["0.0.0.0/0"] |
| 53 | + } |
| 54 | + |
| 55 | + tags = { |
| 56 | + Name = "web-tier-sg" |
| 57 | + Environment = var.environment |
| 58 | + ManagedBy = "Terraform" |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +# |
| 63 | +# Application Tier Security Group |
| 64 | +# |
| 65 | +resource "aws_security_group" "app_tier" { |
| 66 | + name = "app-tier-sg" |
| 67 | + description = "Security group for application tier - only accessible from web tier" |
| 68 | + vpc_id = var.vpc_id |
| 69 | + |
| 70 | + ingress { |
| 71 | + description = "Application port from web tier" |
| 72 | + from_port = 8080 |
| 73 | + to_port = 8080 |
| 74 | + protocol = "tcp" |
| 75 | + security_groups = [aws_security_group.web_tier.id] |
| 76 | + } |
| 77 | + |
| 78 | + ingress { |
| 79 | + description = "SSH from bastion host" |
| 80 | + from_port = 22 |
| 81 | + to_port = 22 |
| 82 | + protocol = "tcp" |
| 83 | + security_groups = [aws_security_group.bastion.id] |
| 84 | + } |
| 85 | + |
| 86 | + egress { |
| 87 | + description = "Allow all outbound" |
| 88 | + from_port = 0 |
| 89 | + to_port = 0 |
| 90 | + protocol = "-1" |
| 91 | + cidr_blocks = ["0.0.0.0/0"] |
| 92 | + } |
| 93 | + |
| 94 | + tags = { |
| 95 | + Name = "app-tier-sg" |
| 96 | + Environment = var.environment |
| 97 | + ManagedBy = "Terraform" |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +# |
| 102 | +# Database Tier Security Group |
| 103 | +# |
| 104 | +resource "aws_security_group" "database" { |
| 105 | + name = "database-sg" |
| 106 | + description = "Security group for database tier - only accessible from app tier" |
| 107 | + vpc_id = var.vpc_id |
| 108 | + |
| 109 | + ingress { |
| 110 | + description = "PostgreSQL from application tier" |
| 111 | + from_port = 5432 |
| 112 | + to_port = 5432 |
| 113 | + protocol = "tcp" |
| 114 | + security_groups = [aws_security_group.app_tier.id] |
| 115 | + } |
| 116 | + |
| 117 | + egress { |
| 118 | + description = "Allow HTTPS to S3 VPC endpoint for backups" |
| 119 | + from_port = 443 |
| 120 | + to_port = 443 |
| 121 | + protocol = "tcp" |
| 122 | + prefix_list_ids = [var.s3_prefix_list_id] |
| 123 | + } |
| 124 | + |
| 125 | + tags = { |
| 126 | + Name = "database-sg" |
| 127 | + Environment = var.environment |
| 128 | + ManagedBy = "Terraform" |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +# |
| 133 | +# Bastion Host Security Group |
| 134 | +# |
| 135 | +resource "aws_security_group" "bastion" { |
| 136 | + name = "bastion-sg" |
| 137 | + description = "Security group for bastion host - restricted SSH access" |
| 138 | + vpc_id = var.vpc_id |
| 139 | + |
| 140 | + ingress { |
| 141 | + description = "SSH from office network" |
| 142 | + from_port = 22 |
| 143 | + to_port = 22 |
| 144 | + protocol = "tcp" |
| 145 | + cidr_blocks = [var.office_cidr] |
| 146 | + } |
| 147 | + |
| 148 | + egress { |
| 149 | + description = "SSH to private subnets" |
| 150 | + from_port = 22 |
| 151 | + to_port = 22 |
| 152 | + protocol = "tcp" |
| 153 | + cidr_blocks = [var.private_subnet_cidr] |
| 154 | + } |
| 155 | + |
| 156 | + tags = { |
| 157 | + Name = "bastion-sg" |
| 158 | + Environment = var.environment |
| 159 | + ManagedBy = "Terraform" |
| 160 | + } |
| 161 | +} |
0 commit comments