Skip to content

Commit

Permalink
Use weighted routing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Ainsworth committed Oct 26, 2023
1 parent 5a7c20d commit 442baf8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
18 changes: 9 additions & 9 deletions terraform/environment/refactor.tf
Original file line number Diff line number Diff line change
Expand Up @@ -864,47 +864,47 @@ moved {

moved {
from = aws_cloudwatch_metric_alarm.actor_health_check_alarm
to = module.eu_west_1.module.actor_use_my_lpa[0].aws_cloudwatch_metric_alarm.this[0]
to = module.eu_west_1.module.actor_use_my_lpa.aws_cloudwatch_metric_alarm.this[0]
}

moved {
from = aws_cloudwatch_metric_alarm.viewer_health_check_alarm
to = module.eu_west_1.module.viewer_use_my_lpa[0].aws_cloudwatch_metric_alarm.this[0]
to = module.eu_west_1.module.viewer_use_my_lpa.aws_cloudwatch_metric_alarm.this[0]
}

moved {
from = aws_route53_health_check.actor_health_check
to = module.eu_west_1.module.actor_use_my_lpa[0].aws_route53_health_check.this[0]
to = module.eu_west_1.module.actor_use_my_lpa.aws_route53_health_check.this[0]
}

moved {
from = aws_route53_health_check.viewer_health_check
to = module.eu_west_1.module.viewer_use_my_lpa[0].aws_route53_health_check.this[0]
to = module.eu_west_1.module.viewer_use_my_lpa.aws_route53_health_check.this[0]
}

moved {
from = aws_route53_record.actor_use_my_lpa
to = module.eu_west_1.module.actor_use_my_lpa[0].aws_route53_record.this
to = module.eu_west_1.module.actor_use_my_lpa.aws_route53_record.this
}

moved {
from = aws_route53_record.admin_use_my_lpa
to = module.eu_west_1.module.admin_use_my_lpa[0].aws_route53_record.this
to = module.eu_west_1.module.admin_use_my_lpa.aws_route53_record.this
}

moved {
from = aws_route53_record.public_facing_use_lasting_power_of_attorney
to = module.eu_west_1.module.public_facing_use_lasting_power_of_attorney[0].aws_route53_record.this
to = module.eu_west_1.module.public_facing_use_lasting_power_of_attorney.aws_route53_record.this
}

moved {
from = aws_route53_record.public_facing_view_lasting_power_of_attorney
to = module.eu_west_1.module.public_facing_view_lasting_power_of_attorney[0].aws_route53_record.this
to = module.eu_west_1.module.public_facing_view_lasting_power_of_attorney.aws_route53_record.this
}

moved {
from = aws_route53_record.viewer_use_my_lpa
to = module.eu_west_1.module.viewer_use_my_lpa[0].aws_route53_record.this
to = module.eu_west_1.module.viewer_use_my_lpa.aws_route53_record.this
}

moved {
Expand Down
16 changes: 11 additions & 5 deletions terraform/environment/region/dns.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ resource "aws_service_discovery_private_dns_namespace" "internal_ecs" {
}

module "public_facing_view_lasting_power_of_attorney" {
count = local.is_active_region ? 1 : 0
source = "./modules/dns"

dns_namespace_env = var.dns_namespace_env
is_active_region = local.is_active_region
current_region = data.aws_region.current.name
zone_id = data.aws_route53_zone.live_service_view_lasting_power_of_attorney.zone_id
loadbalancer = aws_lb.viewer
dns_name = data.aws_route53_zone.live_service_view_lasting_power_of_attorney.name
Expand All @@ -37,10 +38,11 @@ module "public_facing_view_lasting_power_of_attorney" {
}

module "viewer_use_my_lpa" {
count = local.is_active_region ? 1 : 0
source = "./modules/dns"

dns_namespace_env = var.dns_namespace_env
is_active_region = local.is_active_region
current_region = data.aws_region.current.name
zone_id = data.aws_route53_zone.opg_service_justice_gov_uk.zone_id
loadbalancer = aws_lb.viewer
dns_name = "view.lastingpowerofattorney"
Expand All @@ -56,10 +58,11 @@ module "viewer_use_my_lpa" {
}

module "public_facing_use_lasting_power_of_attorney" {
count = local.is_active_region ? 1 : 0
source = "./modules/dns"

dns_namespace_env = var.dns_namespace_env
is_active_region = local.is_active_region
current_region = data.aws_region.current.name
zone_id = data.aws_route53_zone.live_service_use_lasting_power_of_attorney.zone_id
dns_name = data.aws_route53_zone.live_service_use_lasting_power_of_attorney.name
loadbalancer = aws_lb.actor
Expand All @@ -72,10 +75,11 @@ module "public_facing_use_lasting_power_of_attorney" {
}

module "actor_use_my_lpa" {
count = local.is_active_region ? 1 : 0
source = "./modules/dns"

dns_namespace_env = var.dns_namespace_env
is_active_region = local.is_active_region
current_region = data.aws_region.current.name
zone_id = data.aws_route53_zone.opg_service_justice_gov_uk.zone_id
loadbalancer = aws_lb.actor
dns_name = "use.lastingpowerofattorney"
Expand All @@ -91,12 +95,14 @@ module "actor_use_my_lpa" {
}

module "admin_use_my_lpa" {
count = local.is_active_region ? 1 : 0
source = "./modules/dns"

dns_namespace_env = var.dns_namespace_env
is_active_region = local.is_active_region
current_region = data.aws_region.current.name
zone_id = data.aws_route53_zone.opg_service_justice_gov_uk.zone_id
loadbalancer = aws_lb.admin
service_name = "admin"
dns_name = "admin.lastingpowerofattorney"
environment_name = var.environment_name

Expand Down
10 changes: 5 additions & 5 deletions terraform/environment/region/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ locals {
}

route53_fqdns = {
public_facing_view = local.is_active_region ? module.public_facing_view_lasting_power_of_attorney[0].fqdn : ""
public_facing_use = local.is_active_region ? module.public_facing_use_lasting_power_of_attorney[0].fqdn : ""
admin = local.is_active_region ? module.admin_use_my_lpa[0].fqdn : ""
actor = local.is_active_region ? module.actor_use_my_lpa[0].fqdn : ""
viewer = local.is_active_region ? module.viewer_use_my_lpa[0].fqdn : ""
public_facing_view = local.is_active_region ? module.public_facing_view_lasting_power_of_attorney.fqdn : ""
public_facing_use = local.is_active_region ? module.public_facing_use_lasting_power_of_attorney.fqdn : ""
admin = local.is_active_region ? module.admin_use_my_lpa.fqdn : ""
actor = local.is_active_region ? module.actor_use_my_lpa.fqdn : ""
viewer = local.is_active_region ? module.viewer_use_my_lpa.fqdn : ""
}
}
10 changes: 8 additions & 2 deletions terraform/environment/region/modules/dns/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
locals {
create_alarm = var.create_alarm && var.create_health_check
create_alarm = var.create_alarm && var.create_health_check && var.is_active_region
route_weight = var.is_active_region ? 100 : 0
}

resource "aws_route53_record" "this" {
Expand All @@ -13,11 +14,16 @@ resource "aws_route53_record" "this" {
zone_id = var.loadbalancer.zone_id
}

weighted_routing_policy {
weight = local.route_weight
}

lifecycle {
create_before_destroy = true
}

provider = aws.management
set_identifier = "${var.current_region}-${var.environment_name}-${var.service_name}"
provider = aws.management
}

resource "aws_route53_health_check" "this" {
Expand Down
12 changes: 12 additions & 0 deletions terraform/environment/region/modules/dns/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ variable "service_name" {
type = string
default = ""
}

variable "is_active_region" {
description = "Should traffic be routed to this region. Used to create a weighted record set"
type = bool
default = false
}

variable "current_region" {
description = "The current region"
type = string
default = "eu-west-1"
}

0 comments on commit 442baf8

Please sign in to comment.