-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvpc.tf
282 lines (254 loc) · 7.54 KB
/
vpc.tf
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# VPC, pvt, pub subnets, NAT, VPCE and its sg
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.8.1"
# general
name = var.vpc_name
cidr = var.vpc_cidr
azs = var.azs
enable_dns_hostnames = true
create_egress_only_igw = false
create_igw = true
enable_nat_gateway = true
single_nat_gateway = true
enable_vpn_gateway = false
map_public_ip_on_launch = false
# private subnets
private_subnets = var.vpc_private_subnets_cidrs
private_subnet_names = var.vpc_private_subnets_names
# public subnets
public_subnets = var.vpc_public_subnets_cidrs
public_subnet_names = var.vpc_public_subnets_names
# default nacl
manage_default_network_acl = true
default_network_acl_name = "${var.vpc_name}-default-nacl"
default_network_acl_ingress = [
{
rule_no = 100
action = "deny"
from_port = 0
to_port = 0
protocol = "-1"
cidr_block = "0.0.0.0/0"
},
{
rule_no = 101
action = "deny"
from_port = 0
to_port = 0
protocol = "-1"
ipv6_cidr_block = "::/0"
},
]
default_network_acl_egress = [
{
rule_no = 100
action = "deny"
from_port = 0
to_port = 0
protocol = "-1"
cidr_block = "0.0.0.0/0"
},
{
rule_no = 101
action = "deny"
from_port = 0
to_port = 0
protocol = "-1"
ipv6_cidr_block = "::/0"
},
]
# private nacls
private_dedicated_network_acl = true
private_inbound_acl_rules = [
{
rule_number = 100
rule_action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
cidr_block = "0.0.0.0/0"
},
{
rule_number = 101
rule_action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
ipv6_cidr_block = "::/0"
}
]
private_outbound_acl_rules = [
{
rule_number = 100
rule_action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
cidr_block = "0.0.0.0/0"
},
{
rule_number = 101
rule_action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
ipv6_cidr_block = "::/0"
}
]
# public nacls
public_dedicated_network_acl = true
public_inbound_acl_rules = [
{
rule_number = 100
rule_action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
cidr_block = "0.0.0.0/0"
},
{
rule_number = 101
rule_action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
ipv6_cidr_block = "::/0"
}
]
public_outbound_acl_rules = [
{
rule_number = 100
rule_action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
cidr_block = "0.0.0.0/0"
},
{
rule_number = 101
rule_action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
ipv6_cidr_block = "::/0"
}
]
# default security group
manage_default_security_group = true
default_security_group_name = "${var.vpc_name}-default-sg"
default_security_group_egress = []
default_security_group_ingress = []
# default route table
manage_default_route_table = true
default_route_table_name = "${var.vpc_name}-default-rt"
# dhcp options
enable_dhcp_options = true
# flow log
enable_flow_log = true
create_flow_log_cloudwatch_log_group = true
create_flow_log_cloudwatch_iam_role = true
flow_log_destination_type = "cloud-watch-logs"
flow_log_cloudwatch_log_group_retention_in_days = 365
# Tags
tags = local.tags
}
# SG for vpce
resource "aws_security_group" "llm_vpc_vpce" {
name = "llm_vpc_vpce"
description = "Security group for llm vpc vpce"
vpc_id = module.vpc.vpc_id
tags = local.tags
}
resource "aws_vpc_security_group_ingress_rule" "llm_vpc_vpce_ingress_1" {
security_group_id = aws_security_group.llm_vpc_vpce.id
description = "Traffic over TLS from VPC"
from_port = 443
to_port = 443
ip_protocol = "tcp"
cidr_ipv4 = module.vpc.vpc_cidr_block
}
# VPC Endpoint (aws native)
module "llm_vpc_aws_vpce" {
source = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints"
version = "5.8.1"
vpc_id = module.vpc.vpc_id
security_group_ids = [aws_security_group.llm_vpc_vpce.id]
subnet_ids = module.vpc.private_subnets
endpoints = {
s3 = {
service_name = "com.amazonaws.${var.region}.s3"
service_type = "Gateway"
route_table_ids = concat(module.vpc.private_route_table_ids, module.vpc.public_route_table_ids)
tags = { Name = "vpce-llm-s3" }
},
# ssm = {
# service_name = "com.amazonaws.${var.region}.ssm"
# private_dns_enabled = true
# tags = { Name = "vpce-llm-ssm" }
# },
# ec2messages = {
# service_name = "com.amazonaws.${var.region}.ec2messages"
# private_dns_enabled = true
# tags = { Name = "vpce-llm-ec2messages" }
# },
# ssmmessages = {
# service_name = "com.amazonaws.${var.region}.ssmmessages"
# private_dns_enabled = true
# tags = { Name = "vpce-llm-ssmmessages" }
# }
}
tags = local.tags
}
# Default SG for private subnet workloads
resource "aws_security_group" "default_pvt_subnet_sg" {
name = "default-pvt-ec2-sg"
description = "Security group for pvt subnet ec2"
vpc_id = module.vpc.vpc_id
}
resource "aws_vpc_security_group_egress_rule" "default_pvt_subnet_egress_1" {
security_group_id = aws_security_group.default_pvt_subnet_sg.id
description = "Allow all tcp outbound"
from_port = 0
to_port = 65535
ip_protocol = "tcp"
cidr_ipv4 = "0.0.0.0/0"
}
resource "aws_vpc_security_group_ingress_rule" "default_pvt_subnet_ingress_1" {
security_group_id = aws_security_group.default_pvt_subnet_sg.id
description = "Traffic over TLS from VPC"
from_port = 443
to_port = 443
ip_protocol = "tcp"
cidr_ipv4 = module.vpc.vpc_cidr_block
}
resource "aws_vpc_security_group_ingress_rule" "default_pvt_subnet_ingress_2" {
security_group_id = aws_security_group.default_pvt_subnet_sg.id
description = "Traffic over HTTP from VPC"
from_port = 80
to_port = 80
ip_protocol = "tcp"
cidr_ipv4 = module.vpc.vpc_cidr_block
}
# Default SG for public subnet workloads
resource "aws_security_group" "default_pub_subnet_sg" {
name = "default-pub-ec2-sg"
description = "Security group for pub subnet ec2"
vpc_id = module.vpc.vpc_id
}
resource "aws_vpc_security_group_egress_rule" "default_pub_subnet_egress_1" {
security_group_id = aws_security_group.default_pub_subnet_sg.id
description = "Allow all tcp outbound"
from_port = 0
to_port = 65535
ip_protocol = "tcp"
cidr_ipv4 = "0.0.0.0/0"
}
resource "aws_vpc_security_group_ingress_rule" "default_pub_subnet_ingress_1" {
security_group_id = aws_security_group.default_pub_subnet_sg.id
description = "All tcp traffic over TLS"
from_port = 443
to_port = 443
ip_protocol = "tcp"
cidr_ipv4 = "0.0.0.0/0"
}