Skip to content

Commit e296c2e

Browse files
authored
feat: add route53 healthchecks (#67)
* feat: add route53 healthchecks * fix: update version to support optional object types * fix: fix tflint and bats * fix: adjust test version * fix: update typo * fix: update zone id * fix: update test hostname
1 parent eeb0555 commit e296c2e

File tree

9 files changed

+106
-12
lines changed

9 files changed

+106
-12
lines changed

examples/complete/fixtures.us-east-2.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ name = "test-hostname"
66

77
type = "CNAME"
88

9-
zone_id = "Z3SO0TKDDQ0RGG"
9+
zone_id = "Z0880904EUMUUAAGCA17"
1010

1111
records = ["test-hostname"]

examples/complete/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ module "hostname" {
88
zone_id = var.zone_id
99
type = var.type
1010
records = var.records
11+
ttl = var.ttl
12+
13+
healthcheck_enabled = var.healthcheck_enabled
1114

1215
context = module.this.context
1316
}

examples/complete/variables.tf

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
variable "region" {
2-
type = string
2+
type = string
3+
description = "AWS region"
34
}
45

56
variable "zone_id" {
6-
type = string
7+
type = string
8+
description = "Route53 DNS Zone ID"
79
}
810

911
variable "records" {
10-
type = list(string)
12+
description = "DNS records to create"
13+
type = list(string)
1114
}
1215

1316
variable "type" {
@@ -21,3 +24,10 @@ variable "ttl" {
2124
default = 300
2225
description = "The TTL of the record to add to the DNS zone to complete certificate validation"
2326
}
27+
28+
variable "healthcheck_enabled" {
29+
type = bool
30+
description = "Whether to create a Route53 health check"
31+
default = false
32+
}
33+

examples/complete/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 0.13.0"
2+
required_version = ">= 1.3.9"
33

44
required_providers {
55
aws = {

main.tf

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ locals {
33
}
44

55
data "aws_route53_zone" "default" {
6-
count = module.this.enabled ? 1 : 0
6+
count = local.enabled ? 1 : 0
77

88
name = var.zone_name
99
private_zone = var.private_zone
@@ -13,10 +13,38 @@ data "aws_route53_zone" "default" {
1313
}
1414

1515
resource "aws_route53_record" "default" {
16-
count = module.this.enabled ? 1 : 0
16+
count = local.enabled ? 1 : 0
1717
name = var.dns_name == "" ? module.this.id : var.dns_name
1818
zone_id = join("", data.aws_route53_zone.default[*].zone_id)
1919
type = var.type
2020
ttl = var.ttl
2121
records = var.records
22-
}
22+
}
23+
24+
resource "aws_route53_health_check" "default" {
25+
count = local.enabled && var.healthcheck_enabled ? 1 : 0
26+
27+
fqdn = coalesce(var.healthcheck_settings.domain, aws_route53_record.default[0].fqdn)
28+
ip_address = var.healthcheck_settings.ip_address
29+
type = var.healthcheck_settings.type
30+
port = var.healthcheck_settings.port
31+
regions = var.healthcheck_settings.regions
32+
33+
reference_name = var.healthcheck_settings.reference_name
34+
resource_path = var.healthcheck_settings.resource_path
35+
search_string = var.healthcheck_settings.search_string
36+
measure_latency = var.healthcheck_settings.measure_latency
37+
38+
routing_control_arn = var.type == "RECOVERY_CONTROL" ? var.healthcheck_settings.routing_control_arn : null
39+
enable_sni = var.type == "HTTPS" ? true : false
40+
41+
# Must be either 10 or 30
42+
request_interval = var.healthcheck_settings.request_interval
43+
# Must be between 0 and 256
44+
child_health_threshold = var.healthcheck_settings.child_health_threshold
45+
child_healthchecks = var.healthcheck_settings.child_healthchecks
46+
invert_healthcheck = var.healthcheck_settings.invert_healthcheck
47+
failure_threshold = var.healthcheck_settings.failure_threshold
48+
# Valid values are Healthy , Unhealthy and LastKnownStatus
49+
insufficient_data_health_status = var.healthcheck_settings.insufficient_data_health_status
50+
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
output "hostname" {
2-
value = join("", aws_route53_record.default.*.fqdn)
2+
value = join("", aws_route53_record.default[*].fqdn)
33
description = "DNS hostname"
44
}

test/src/examples_complete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestExamplesComplete(t *testing.T) {
3838

3939
// Run `terraform output` to get the value of an output variable
4040
hostname := terraform.Output(t, terraformOptions, "hostname")
41-
expectedHostname := "test-hostname-" + randId + ".testing.cloudposse.co"
41+
expectedHostname := "test-hostname-" + randId + ".modules.cptest.test-automation.app"
4242
// Verify we're getting back the outputs we expect
4343
assert.Equal(t, expectedHostname, hostname)
4444
}

variables.tf

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,57 @@ variable "dns_name" {
4949
type = string
5050
description = "The name of the DNS record"
5151
default = ""
52-
}
52+
}
53+
54+
variable "healthcheck_enabled" {
55+
type = bool
56+
description = "Whether to create a Route53 health check"
57+
default = false
58+
}
59+
60+
variable "healthcheck_settings" {
61+
type = object({
62+
domain = optional(string)
63+
ip_address = optional(string)
64+
regions = optional(list(string))
65+
type = optional(string, "HTTPS")
66+
request_interval = optional(string, "30")
67+
port = optional(number, 443)
68+
reference_name = optional(string)
69+
resource_path = optional(string)
70+
failure_threshold = optional(number)
71+
search_string = optional(string)
72+
measure_latency = optional(bool)
73+
invert_healthcheck = optional(bool)
74+
child_healthchecks = optional(list(string))
75+
routing_control_arn = optional(string)
76+
child_health_threshold = optional(number)
77+
cloudwatch_alarm_name = optional(string)
78+
cloudwatch_alarm_region = optional(string)
79+
insufficient_data_health_status = optional(string)
80+
})
81+
description = <<EOT
82+
Route 53 health check configuration settings
83+
84+
domain: The fully qualified domain name of the endpoint to be checked
85+
ip_address: The IP address of the endpoint to be checked
86+
regions: AWS regions to run the health checks from
87+
type: The protocol to use for the health check such as HTTP HTTPS TCP etc
88+
port: Port on the endpoint to be checked
89+
reference_name: Used in caller reference and helpful for identifying individual health check sets
90+
resource_path: The URL path Route 53 requests during the health check
91+
failure_threshold: Number of consecutive health checks that an endpoint must pass or fail
92+
search_string: String searched in response body for match checks
93+
measure_latency: Whether to measure and report latency from multiple regions
94+
invert_healthcheck: If true a healthy check is considered unhealthy and vice versa
95+
child_healthchecks: List of health check IDs for associated child checks
96+
routing_control_arn: ARN of the Application Recovery Controller routing control
97+
request_interval: Interval between health check requests in seconds
98+
child_health_threshold: Minimum number of child checks that must be healthy
99+
cloudwatch_alarm_name: Name of the CloudWatch alarm to evaluate
100+
cloudwatch_alarm_region: Region where the CloudWatch alarm is configured
101+
insufficient_data_health_status: Status to assign when CloudWatch has insufficient data
102+
EOT
103+
default = {}
104+
nullable = false
105+
}

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 0.13.0"
2+
required_version = ">= 1.3.9"
33

44
required_providers {
55
aws = {

0 commit comments

Comments
 (0)