Skip to content

Instantly share code, notes, and snippets.

@sapslaj
Created February 15, 2023 04:15
Show Gist options
  • Save sapslaj/0d00b6729bc727ac489ded27d126f0c1 to your computer and use it in GitHub Desktop.
Save sapslaj/0d00b6729bc727ac489ded27d126f0c1 to your computer and use it in GitHub Desktop.

Write a Terraform module that creates an AWS Route53 record that does active-active failover to two backend servers with healthchecks.

Sure, I can help you with that! Here's an example Terraform module that creates an AWS Route53 record that does active-active failover to two backend servers with healthchecks.

First, create a variables.tf file to define the input variables for the module:

variable "zone_id" {
  type        = string
  description = "The ID of the Route53 hosted zone"
}

variable "record_name" {
  type        = string
  description = "The name of the DNS record"
}

variable "record_type" {
  type        = string
  description = "The type of the DNS record (e.g. A, CNAME)"
}

variable "backend_ips" {
  type        = list(string)
  description = "The list of IP addresses for the backend servers"
}

Next, create a main.tf file to define the AWS resources:

resource "aws_route53_health_check" "backend" {
  fqdn            = "example.com"
  port            = 80
  type            = "HTTP"
  request_path    = "/"
  failure_threshold = 3
  tags = {
    Name = "Backend"
  }
}

resource "aws_route53_record" "failover" {
  zone_id = var.zone_id
  name    = var.record_name
  type    = var.record_type
  alias {
    name    = aws_route53_record.failover.name_servers[0]
    zone_id = aws_route53_record.failover.name_servers_zone_id
    evaluate_target_health = true
  }
  set_identifier = "primary"
  weight         = 1
}

resource "aws_route53_record" "failover_secondary" {
  zone_id = var.zone_id
  name    = var.record_name
  type    = var.record_type
  alias {
    name    = aws_route53_record.failover_secondary.name_servers[0]
    zone_id = aws_route53_record.failover_secondary.name_servers_zone_id
    evaluate_target_health = true
  }
  set_identifier = "secondary"
  weight         = 1
}

resource "aws_route53_health_check" "failover_secondary" {
  fqdn            = var.record_name
  port            = 80
  type            = "HTTP"
  request_path    = "/"
  failure_threshold = 3
  tags = {
    Name = "Failover Secondary"
  }
}

resource "aws_route53_record" "failover_healthcheck" {
  zone_id = var.zone_id
  name    = var.record_name
  type    = var.record_type
  set_identifier = "primary"
  health_check_id = aws_route53_health_check.backend.id
  weight         = 1
}

resource "aws_route53_record" "failover_healthcheck_secondary" {
  zone_id = var.zone_id
  name    = var.record_name
  type    = var.record_type
  set_identifier = "secondary"
  health_check_id = aws_route53_health_check.failover_secondary.id
  weight         = 1
}

resource "aws_route53_resolver_rule" "failover" {
  name = var.record_name
  resolver_endpoint_id = aws_route53_resolver_endpoint.resolver.id
  rule_type = "FORWARD"
  domain_name = var.record_name
  target_ips = var.backend_ips
  target_health {
    enabled = true
  }
}

resource "aws_route53_resolver_endpoint" "resolver" {
  direction = "INBOUND"
  ip_address = "10.0.0.2"
  name = "resolver-endpoint"
  security_group_ids = [aws_security_group.resolver.id]
  subnet_ids = [aws_subnet.res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment