From 442baf83e29bcd4d3e35d25d8010ace28c5462e4 Mon Sep 17 00:00:00 2001 From: Sam Ainsworth Date: Thu, 19 Oct 2023 15:31:46 +0100 Subject: [PATCH] Use weighted routing --- terraform/environment/refactor.tf | 18 +++++++++--------- terraform/environment/region/dns.tf | 16 +++++++++++----- terraform/environment/region/locals.tf | 10 +++++----- .../environment/region/modules/dns/main.tf | 10 ++++++++-- .../region/modules/dns/variables.tf | 12 ++++++++++++ 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/terraform/environment/refactor.tf b/terraform/environment/refactor.tf index f67fa4f168..23caaf9937 100644 --- a/terraform/environment/refactor.tf +++ b/terraform/environment/refactor.tf @@ -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 { diff --git a/terraform/environment/region/dns.tf b/terraform/environment/region/dns.tf index 553f669cd0..e1e297451f 100644 --- a/terraform/environment/region/dns.tf +++ b/terraform/environment/region/dns.tf @@ -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 @@ -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" @@ -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 @@ -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" @@ -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 diff --git a/terraform/environment/region/locals.tf b/terraform/environment/region/locals.tf index 8f32365226..5417e94576 100644 --- a/terraform/environment/region/locals.tf +++ b/terraform/environment/region/locals.tf @@ -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 : "" } } diff --git a/terraform/environment/region/modules/dns/main.tf b/terraform/environment/region/modules/dns/main.tf index 0a4c248550..8753729307 100644 --- a/terraform/environment/region/modules/dns/main.tf +++ b/terraform/environment/region/modules/dns/main.tf @@ -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" { @@ -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" { diff --git a/terraform/environment/region/modules/dns/variables.tf b/terraform/environment/region/modules/dns/variables.tf index b8546da345..e93789e0b0 100644 --- a/terraform/environment/region/modules/dns/variables.tf +++ b/terraform/environment/region/modules/dns/variables.tf @@ -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" +}