-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
261 lines (233 loc) · 10.1 KB
/
main.tf
File metadata and controls
261 lines (233 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// main.tf
# Written by Marc Straubinger - Overhauled for Security-First Best Practices
# Web Tier Security Group
# PSA Compliance: Req 10 (network access control)
resource "aws_security_group" "web_tier" {
count = var.create_web_tier_sg ? 1 : 0
name = "${local.name_prefix}-sg-web"
description = "Security group for web tier - PSA compliant"
vpc_id = var.vpc_id
tags = merge(local.common_tags, {
"Name" = "${local.name_prefix}-sg-web"
"Tier" = "web"
"PSA-Compliant" = "true"
})
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "web_https_ingress" {
count = var.create_web_tier_sg ? 1 : 0
description = "HTTPS inbound from allowed CIDRs"
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.allowed_web_cidrs
security_group_id = aws_security_group.web_tier[0].id
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "web_http_ingress" {
count = var.create_web_tier_sg && var.allow_http ? 1 : 0
description = "HTTP inbound (redirect to HTTPS recommended)"
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = var.allowed_web_cidrs
security_group_id = aws_security_group.web_tier[0].id
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "web_to_app_egress" {
count = var.create_web_tier_sg ? 1 : 0
description = "Outbound to application tier only"
type = "egress"
from_port = var.app_port
to_port = var.app_port
protocol = "tcp"
source_security_group_id = var.create_app_tier_sg ? aws_security_group.app_tier[0].id : local.existing_app_tier_sg_id
security_group_id = aws_security_group.web_tier[0].id
lifecycle {
precondition {
condition = var.create_app_tier_sg || local.existing_app_tier_sg_id != null
error_message = "app_tier_sg_ids must contain at least one security group ID when create_app_tier_sg is false."
}
}
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "web_https_egress" {
count = var.create_web_tier_sg ? 1 : 0
description = "HTTPS outbound for security updates"
type = "egress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.allowed_https_egress_cidrs
security_group_id = aws_security_group.web_tier[0].id
}
# Application Tier Security Group
# PSA Compliance: Req 10 (network access control)
resource "aws_security_group" "app_tier" {
count = var.create_app_tier_sg ? 1 : 0
name = "${local.name_prefix}-sg-app"
description = "Security group for application tier - PSA compliant"
vpc_id = var.vpc_id
tags = merge(local.common_tags, {
"Name" = "${local.name_prefix}-sg-app"
"Tier" = "application"
"PSA-Compliant" = "true"
})
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "app_from_web_ingress" {
count = var.create_app_tier_sg ? 1 : 0
description = "Inbound from web tier only"
type = "ingress"
from_port = var.app_port
to_port = var.app_port
protocol = "tcp"
source_security_group_id = var.create_web_tier_sg ? aws_security_group.web_tier[0].id : local.existing_web_tier_sg_id
security_group_id = aws_security_group.app_tier[0].id
lifecycle {
precondition {
condition = var.create_web_tier_sg || local.existing_web_tier_sg_id != null
error_message = "web_tier_sg_ids must contain at least one security group ID when create_web_tier_sg is false."
}
}
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "app_to_db_egress" {
count = var.create_app_tier_sg ? 1 : 0
description = "Outbound to database tier only"
type = "egress"
from_port = var.db_port
to_port = var.db_port
protocol = "tcp"
source_security_group_id = var.create_db_tier_sg ? aws_security_group.db_tier[0].id : local.existing_db_tier_sg_id
security_group_id = aws_security_group.app_tier[0].id
lifecycle {
precondition {
condition = var.create_db_tier_sg || local.existing_db_tier_sg_id != null
error_message = "db_tier_sg_ids must contain at least one security group ID when create_db_tier_sg is false."
}
}
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "app_https_egress" {
count = var.create_app_tier_sg ? 1 : 0
description = "HTTPS outbound for external APIs and updates"
type = "egress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.allowed_https_egress_cidrs
security_group_id = aws_security_group.app_tier[0].id
}
# Database Tier Security Group
# PSA Compliance: Req 10 (network access control)
resource "aws_security_group" "db_tier" {
count = var.create_db_tier_sg ? 1 : 0
name = "${local.name_prefix}-sg-db"
description = "Security group for database tier - PSA compliant"
vpc_id = var.vpc_id
tags = merge(local.common_tags, {
"Name" = "${local.name_prefix}-sg-db"
"Tier" = "database"
"PSA-Compliant" = "true"
})
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "db_from_app_ingress" {
count = var.create_db_tier_sg ? 1 : 0
description = "Inbound from application tier only"
type = "ingress"
from_port = var.db_port
to_port = var.db_port
protocol = "tcp"
source_security_group_id = var.create_app_tier_sg ? aws_security_group.app_tier[0].id : local.existing_app_tier_sg_id
security_group_id = aws_security_group.db_tier[0].id
lifecycle {
precondition {
condition = var.create_app_tier_sg || local.existing_app_tier_sg_id != null
error_message = "app_tier_sg_ids must contain at least one security group ID when create_app_tier_sg is false."
}
}
}
# Management Security Group
# PSA Compliance: Req 10 (network access control)
resource "aws_security_group" "management" {
count = var.create_management_sg ? 1 : 0
name = "${local.name_prefix}-sg-mgmt"
description = "Security group for management/bastion access - PSA compliant"
vpc_id = var.vpc_id
tags = merge(local.common_tags, {
"Name" = "${local.name_prefix}-sg-mgmt"
"Tier" = "management"
"PSA-Compliant" = "true"
})
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "mgmt_ssh_ingress" {
count = var.create_management_sg ? 1 : 0
description = "SSH from authorized management networks only"
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.allowed_management_cidrs
security_group_id = aws_security_group.management[0].id
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "mgmt_rdp_ingress" {
count = var.create_management_sg && var.allow_rdp ? 1 : 0
description = "RDP from authorized management networks only"
type = "ingress"
from_port = 3389
to_port = 3389
protocol = "tcp"
cidr_blocks = var.allowed_management_cidrs
security_group_id = aws_security_group.management[0].id
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "mgmt_ssh_egress" {
count = var.create_management_sg ? 1 : 0
description = "SSH outbound to internal VPC networks"
type = "egress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.internal_cidrs
security_group_id = aws_security_group.management[0].id
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "mgmt_https_egress" {
count = var.create_management_sg ? 1 : 0
description = "HTTPS outbound for security patches and updates"
type = "egress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.allowed_https_egress_cidrs
security_group_id = aws_security_group.management[0].id
}
# Custom Rules
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "web_custom" {
for_each = var.create_web_tier_sg ? { for idx, rule in var.web_custom_ingress : idx => rule } : {}
security_group_id = aws_security_group.web_tier[0].id
type = "ingress"
from_port = each.value.from_port
to_port = coalesce(each.value.to_port, each.value.from_port)
protocol = each.value.protocol
cidr_blocks = each.value.cidr_blocks
description = each.value.description
}
# PSA Compliance: Req 10 (least privilege network access)
resource "aws_security_group_rule" "app_custom" {
for_each = var.create_app_tier_sg ? { for idx, rule in var.app_custom_ingress : idx => rule } : {}
security_group_id = aws_security_group.app_tier[0].id
type = "ingress"
from_port = each.value.from_port
to_port = coalesce(each.value.to_port, each.value.from_port)
protocol = each.value.protocol
cidr_blocks = each.value.cidr_blocks
description = each.value.description
}