Skip to content

Commit f8f4e2c

Browse files
authored
Merge pull request #22 from davidsilva/feature/frontend-to-https
Switch frontend to HTTPS
2 parents a8e67a8 + c973e45 commit f8f4e2c

File tree

8 files changed

+93
-6
lines changed

8 files changed

+93
-6
lines changed

backend/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let corsOrigin: string;
1818
if (process.env['NODE_ENV'] === 'local') {
1919
corsOrigin = 'http://localhost:4200';
2020
} else {
21-
corsOrigin = 'http://dev.interviewprep.onyxdevtutorials.com';
21+
corsOrigin = 'https://dev.interviewprep.onyxdevtutorials.com';
2222
}
2323

2424
const corsOptions = {

terraform/environments/development/main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ module "load_balancer" {
152152
vpc_id = module.vpc.vpc_id
153153
frontend_health_check_path = "/health"
154154
backend_health_check_path = "/health"
155+
frontend_domain_name = "dev.interviewprep.onyxdevtutorials.com"
156+
zone_id = aws_route53_zone.onyxdevtutorials_com.zone_id
157+
frontend_cert_arn = module.dns.frontend_cert_arn
155158
}
156159

157160
module "dns" {
@@ -163,6 +166,7 @@ module "dns" {
163166
lb_zone_id = module.load_balancer.lb_zone_id
164167
custom_domain_name = module.api_gateway.custom_domain_name
165168
custom_domain_zone_id = module.api_gateway.custom_domain_zone_id
169+
environment = var.environment
166170
}
167171

168172
module "api_gateway" {
@@ -172,5 +176,5 @@ module "api_gateway" {
172176
lb_dns_name = module.load_balancer.lb_dns_name
173177
region = var.region
174178
certificate_arn = var.certificate_arn
175-
cors_origin = "http://dev.interviewprep.onyxdevtutorials.com"
179+
cors_origin = "https://dev.interviewprep.onyxdevtutorials.com"
176180
}

terraform/environments/development/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,8 @@ output "backend_target_group_arn" {
137137
description = "The ARN of the backend target group"
138138
value = module.load_balancer.backend_target_group_arn
139139
}
140+
141+
output "frontend_cert_arn" {
142+
description = "The ARN of the frontend certificate"
143+
value = module.dns.frontend_cert_arn
144+
}

terraform/modules/dns/main.tf

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,40 @@ resource "aws_route53_record" "backend" {
2020
zone_id = var.custom_domain_zone_id
2121
evaluate_target_health = false
2222
}
23-
}
23+
}
24+
25+
resource "aws_acm_certificate" "frontend_cert" {
26+
domain_name = var.frontend_record_name
27+
validation_method = "DNS"
28+
29+
tags = {
30+
Name = "${var.environment}-frontend-cert"
31+
Environment = var.environment
32+
}
33+
}
34+
35+
resource "aws_route53_record" "frontend_cert_validation" {
36+
for_each = {
37+
for dvo in aws_acm_certificate.frontend_cert.domain_validation_options : dvo.domain_name => {
38+
name = dvo.resource_record_name
39+
type = dvo.resource_record_type
40+
record = dvo.resource_record_value
41+
}
42+
}
43+
44+
zone_id = var.zone_id
45+
name = each.value.name
46+
type = each.value.type
47+
records = [each.value.record]
48+
ttl = 60
49+
50+
lifecycle {
51+
create_before_destroy = true
52+
}
53+
}
54+
55+
resource "aws_acm_certificate_validation" "frontend_cert_validation" {
56+
certificate_arn = aws_acm_certificate.frontend_cert.arn
57+
validation_record_fqdns = [for record in aws_route53_record.frontend_cert_validation : record.fqdn]
58+
}
59+

terraform/modules/dns/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ output "frontend_record_name" {
66
output "backend_record_name" {
77
description = "The DNS record name for the backend service"
88
value = aws_route53_record.backend.name
9+
}
10+
11+
output "frontend_cert_arn" {
12+
description = "The ARN of the frontend certificate"
13+
value = aws_acm_certificate.frontend_cert.arn
914
}

terraform/modules/dns/variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
variable "environment" {
2+
description = "The environment in which the resources are being created"
3+
type = string
4+
}
5+
16
variable "zone_id" {
27
description = "The ID of the Route 53 hosted zone"
38
type = string
@@ -31,4 +36,4 @@ variable "custom_domain_name" {
3136
variable "custom_domain_zone_id" {
3237
description = "The custom domain zone ID for api"
3338
type = string
34-
}
39+
}

terraform/modules/load_balancer/main.tf

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,31 @@ resource "aws_lb_target_group" "backend" {
5959
}
6060
}
6161

62+
resource "aws_lb_listener" "https_frontend" {
63+
load_balancer_arn = aws_lb.this.arn
64+
port = 443
65+
protocol = "HTTPS"
66+
ssl_policy = "ELBSecurityPolicy-2016-08"
67+
certificate_arn = var.frontend_cert_arn
68+
69+
default_action {
70+
type = "forward"
71+
target_group_arn = aws_lb_target_group.frontend.arn
72+
}
73+
}
74+
6275
resource "aws_lb_listener" "http_frontend" {
6376
load_balancer_arn = aws_lb.this.arn
6477
port = 80
6578
protocol = "HTTP"
6679

6780
default_action {
68-
type = "forward"
69-
target_group_arn = aws_lb_target_group.frontend.arn # Refer to the ECS module to see how the target group ARN is passed to the ECS service.
81+
type = "redirect"
82+
redirect {
83+
port = "443"
84+
protocol = "HTTPS"
85+
status_code = "HTTP_301"
86+
}
7087
}
7188
}
7289

terraform/modules/load_balancer/variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,18 @@ variable "backend_health_check_path" {
2929
type = string
3030
default = "/"
3131
}
32+
33+
variable "frontend_domain_name" {
34+
description = "The domain name for the frontend"
35+
type = string
36+
}
37+
38+
variable "zone_id" {
39+
description = "The Route 53 zone ID for the domain"
40+
type = string
41+
}
42+
43+
variable "frontend_cert_arn" {
44+
description = "The ARN of the certificate for the frontend"
45+
type = string
46+
}

0 commit comments

Comments
 (0)