Skip to content

Commit 7bd9084

Browse files
authored
BENCH-302: Terraform Load Balancer with Domain Name (#86)
* added load balancer and eip * Load balancer working * Added route53 record linked to elastic ip * Connected TLS certificate * Moved sensitive variables to untracked file * removed health check for now so that it works again * fixed deploy.yml * using 1 public+private subnet to cut costs * actually fixed deploy * Connected TLS certificate * removed health check for now so that it works again * fixed deploy.yml * using 1 public+private subnet to cut costs
1 parent 4a13023 commit 7bd9084

File tree

6 files changed

+100
-5
lines changed

6 files changed

+100
-5
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ jobs:
6161
labels: ${{ steps.meta.outputs.labels }}
6262

6363
- name: Download task definition
64-
run: |
65-
aws ecs describe-task-definition --task-definition answerking-dotnet-api-task --query taskDefinition > ${{ env.ECS_TASK_DEFINITION }}
64+
run: aws ecs describe-task-definition --task-definition answerking-dotnet-api-task --query taskDefinition > ${{ env.ECS_TASK_DEFINITION }}
6665

6766
- name: Fill in the new image ID in the Amazon ECS task definition
6867
id: task-def

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,4 @@ $RECYCLE.BIN/
462462
.terraform.lock.hcl
463463
terraform.tfstate
464464
terraform.tfstate.backup
465+
terraform/env_variables.tf

terraform/launch-config.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ resource "aws_autoscaling_group" "failure_analysis_ecs_asg" {
3232
vpc_zone_identifier = [module.vpc_subnet.public_subnet_ids[0]]
3333
launch_configuration = aws_launch_configuration.ecs_launch_config.name
3434

35-
desired_capacity = 2
35+
desired_capacity = 1
3636
min_size = 1
3737
max_size = 5
3838
health_check_grace_period = 300

terraform/lb.tf

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Elastic IP
2+
3+
resource "aws_eip" "lb_eip" {
4+
vpc = true
5+
6+
tags = {
7+
Name = "${var.project_name}-eip"
8+
Owner = var.owner
9+
}
10+
}
11+
12+
resource "aws_route53_record" "dns_dotnet" {
13+
zone_id = "${var.dns_hosted_zone_id}"
14+
name = "${var.dns_record_name}"
15+
type = "A"
16+
ttl = 300
17+
records = [aws_eip.lb_eip.public_ip]
18+
}
19+
20+
# Load Balancer
21+
22+
resource "aws_lb" "load_balancer" {
23+
name = "${var.project_name}-lb"
24+
internal = false
25+
load_balancer_type = "network"
26+
ip_address_type = "ipv4"
27+
28+
subnet_mapping {
29+
subnet_id = "${module.vpc_subnet.public_subnet_ids[0]}"
30+
allocation_id = "${aws_eip.lb_eip.id}"
31+
}
32+
33+
tags = {
34+
Name = "${var.project_name}-lb"
35+
}
36+
}
37+
38+
resource "aws_lb_target_group" "target_group" {
39+
name = "${var.project_name}-lb-tg-${substr(uuid(), 0, 3)}"
40+
port = 80
41+
protocol = "TCP"
42+
target_type = "ip"
43+
vpc_id = module.vpc_subnet.vpc_id
44+
45+
tags = {
46+
Name = "${var.project_name}-lb-tg"
47+
}
48+
49+
lifecycle {
50+
create_before_destroy = true
51+
ignore_changes = [name]
52+
}
53+
}
54+
55+
resource "aws_lb_listener" "listener" {
56+
load_balancer_arn = aws_lb.load_balancer.id
57+
port = "80"
58+
protocol = "TCP"
59+
60+
default_action {
61+
type = "forward"
62+
target_group_arn = aws_lb_target_group.target_group.id
63+
}
64+
}
65+
66+
resource "aws_lb_listener" "listener_443" {
67+
load_balancer_arn = aws_lb.load_balancer.id
68+
port = "443"
69+
protocol = "TLS"
70+
certificate_arn = var.tls_certificate_arn
71+
alpn_policy = "HTTP2Preferred"
72+
73+
default_action {
74+
type = "forward"
75+
target_group_arn = aws_lb_target_group.target_group.id
76+
}
77+
}

terraform/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,10 @@ resource "aws_ecs_service" "aws_ecs_service" {
186186
assign_public_ip = true
187187
security_groups = [aws_security_group.ecs_sg.id]
188188
}
189+
190+
load_balancer {
191+
target_group_arn = aws_lb_target_group.target_group.arn
192+
container_name = "${var.project_name}-container"
193+
container_port = 80
194+
}
189195
}

terraform/variables.tf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ variable "owner" {
1616
default = "answerking-dotnet-team"
1717
}
1818

19+
variable "dns_record_name" {
20+
type = string
21+
description = "DNS Record Name"
22+
default = "dotnet.answerking.co.uk"
23+
}
24+
25+
variable "dns_base_domain_name" {
26+
type = string
27+
description = "DNS Base Domain Name"
28+
default = "answerking.co.uk"
29+
}
30+
1931
variable "image_url" {
2032
type = string
2133
description = "AnswerKing C# API image"
@@ -37,13 +49,13 @@ variable "vpc_cidr" {
3749
variable "num_public_subnets" {
3850
type = number
3951
description = "Number of public subnets"
40-
default = 2
52+
default = 1
4153
}
4254

4355
variable "num_private_subnets" {
4456
type = number
4557
description = "Number of private subnets"
46-
default = 2
58+
default = 1
4759
}
4860

4961
variable "aws_cloudwatch_retention_in_days" {

0 commit comments

Comments
 (0)