-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
368 lines (292 loc) · 8.74 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
data "aws_ec2_managed_prefix_list" "s3" {
name = "com.amazonaws.us-east-1.s3"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
enable_network_address_usage_metrics = false
instance_tenancy = "default"
tags = {
"Name" = "main"
}
}
resource "aws_vpc_endpoint" "s3" {
vpc_id = aws_vpc.main.id
service_name = "com.amazonaws.us-east-1.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = [aws_route_table.main_rtb_private1.id, aws_route_table.main_rtb_private2.id]
tags = {
"Name" = "s3"
}
}
# ----------- IGW ---------------
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
"Name" = "igw"
}
}
# --------- SUBNETS -------------
resource "aws_subnet" "public1" {
availability_zone = "us-east-1a"
cidr_block = "10.0.0.0/20"
private_dns_hostname_type_on_launch = "ip-name"
tags = {
"Name" = "public1"
}
vpc_id = aws_vpc.main.id
}
resource "aws_subnet" "public2" {
availability_zone = "us-east-1b"
cidr_block = "10.0.16.0/20"
private_dns_hostname_type_on_launch = "ip-name"
tags = {
"Name" = "public2"
}
vpc_id = aws_vpc.main.id
}
resource "aws_subnet" "private1" {
availability_zone = "us-east-1a"
cidr_block = "10.0.128.0/20"
private_dns_hostname_type_on_launch = "ip-name"
tags = {
"Name" = "private1"
}
vpc_id = aws_vpc.main.id
}
resource "aws_subnet" "private2" {
availability_zone = "us-east-1b"
cidr_block = "10.0.144.0/20"
private_dns_hostname_type_on_launch = "ip-name"
tags = {
"Name" = "private2"
}
vpc_id = aws_vpc.main.id
}
# ---------- NAT EIP -----------
resource "aws_eip" "nat_eip" {
domain = "vpc"
network_border_group = "us-east-1"
public_ipv4_pool = "amazon"
tags = {
"Name" = "nat-eip"
}
}
# ----------- NAT ---------------
resource "aws_nat_gateway" "nat_gw" {
allocation_id = aws_eip.nat_eip.id
subnet_id = aws_subnet.public1.id
tags = {
Name = "nat_gw"
}
depends_on = [aws_internet_gateway.igw]
}
# -------- ROUTES -------
resource "aws_route_table" "main_rtb_public" {
tags = {
"Name" = "main_rtb_public"
}
vpc_id = aws_vpc.main.id
}
resource "aws_route" "main_rtb_public_igw" {
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
route_table_id = aws_route_table.main_rtb_public.id
}
resource "aws_route_table" "main_rtb_private1" {
tags = {
"Name" = "main_rtb_private1"
}
vpc_id = aws_vpc.main.id
}
resource "aws_route" "main_rtb_private1_nat" {
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gw.id
route_table_id = aws_route_table.main_rtb_private1.id
}
resource "aws_route_table" "main_rtb_private2" {
tags = {
"Name" = "main_rtb_private2"
}
vpc_id = aws_vpc.main.id
}
resource "aws_route" "main_rtb_private2_nat" {
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gw.id
route_table_id = aws_route_table.main_rtb_private2.id
}
# ------ security groups ------------
resource "aws_security_group" "ingress_sg" {
name = "allow-http"
description = "Allow HTTP inbound traffic"
vpc_id = aws_vpc.main.id
tags = {
Name = "allow-http"
}
}
# Ingress rule to allow incoming HTTP traffic
resource "aws_security_group_rule" "allow_http_ingress" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.ingress_sg.id
}
# Ingress rule to allow incoming HTTPS traffic
resource "aws_security_group_rule" "allow_https_ingress" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.ingress_sg.id
}
# Egress rule to allow all outbound traffic
resource "aws_security_group_rule" "allow_all_egress" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.ingress_sg.id
}
# ----- EC2 Instances -----
# Fetch the latest Ubuntu 22.04 AMI
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
# EC2 Instance in Private Subnet 1
resource "aws_instance" "host1" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
subnet_id = aws_subnet.private1.id
key_name = "nuvei_key" # replace with your key pair name
tags = {
Name = "host1"
}
vpc_security_group_ids = [aws_security_group.ingress_sg.id]
}
# EC2 Instance in Private Subnet 2
resource "aws_instance" "host2" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
subnet_id = aws_subnet.private2.id
key_name = "nuvei_key" # replace with your key pair name
tags = {
Name = "host2"
}
vpc_security_group_ids = [aws_security_group.ingress_sg.id]
}
# ----- Route 53 Hosted Zone -----
resource "aws_route53_zone" "nuvei_zone" {
name = "nuvei-test.com" # placeholder domain
}
# ----- CNAME Record for ELB -----
resource "aws_route53_record" "elb_cname" {
zone_id = aws_route53_zone.nuvei_zone.zone_id
name = "elb.nuvei-test.com" # assuming this naming convention but can be any name really
type = "CNAME"
ttl = "300"
records = [aws_lb.nuvei_lb.dns_name]
}
# ----- ACM Certificate ----------
resource "aws_acm_certificate" "nuvei_cert" {
domain_name = "nuvei-test.com"
subject_alternative_names = ["elb.nuvei-test.com"]
validation_method = "DNS"
}
resource "aws_route53_record" "validation" {
for_each = {
for dvo in aws_acm_certificate.nuvei_cert.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
zone_id = aws_route53_zone.nuvei_zone.zone_id
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = each.value.zone_id
}
resource "aws_acm_certificate_validation" "validation" {
certificate_arn = aws_acm_certificate.nuvei_cert.arn
validation_record_fqdns = [for record in aws_route53_record.validation : record.fqdn]
}
# ----- Elastic Load Balancer (ELB) -----
# Create a load balancer
resource "aws_lb" "nuvei_lb" {
name = "nuvei-lb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.ingress_sg.id]
subnets = [aws_subnet.public1.id, aws_subnet.public2.id]
enable_deletion_protection = false
tags = {
Name = "nuvei_lb"
}
}
# ----- Target Group -----
# Create target group
resource "aws_lb_target_group" "nuvei_lb_tg" {
name = "nuvei-lb-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
health_check {
healthy_threshold = 3
unhealthy_threshold = 3
timeout = 5
interval = 30
path = "/"
protocol = "HTTP"
matcher = "200"
}
}
# Register instances with target group
resource "aws_lb_target_group_attachment" "host1" {
target_group_arn = aws_lb_target_group.nuvei_lb_tg.arn
target_id = aws_instance.host1.id
port = 80
}
resource "aws_lb_target_group_attachment" "host2" {
target_group_arn = aws_lb_target_group.nuvei_lb_tg.arn
target_id = aws_instance.host2.id
port = 80
}
# ----- Listeners -----
# Listener for HTTP (port 80)
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.nuvei_lb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.nuvei_lb_tg.arn
}
}
# Listener for HTTPS (port 443) - Requires a valid SSL certificate
resource "aws_lb_listener" "https" {
load_balancer_arn = aws_lb.nuvei_lb.arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = aws_acm_certificate_validation.validation.certificate_arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.nuvei_lb_tg.arn
}
}