diff --git a/main.tf b/main.tf index 51c09bfa..4912bf0c 100644 --- a/main.tf +++ b/main.tf @@ -10,10 +10,11 @@ # REQUIRE A SPECIFIC TERRAFORM VERSION OR HIGHER # ---------------------------------------------------------------------------------------------------------------------- terraform { - # This module is now only being tested with Terraform 0.15.x. However, to make upgrading easier, we are setting - # 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it - # forwards compatible with 0.15.x code. - required_version = ">= 0.12.26" + # This module is now only being tested with Terraform 0.14.x. However, to make upgrading easier, we are setting + # 0.14.0 as the minimum version, as that version added support for validation and the alltrue function + # Removing the validation completely will yield a version compatible with 0.12.26 as that added support for + # required_providers with source URLs + required_version = ">= 0.14.0" } # --------------------------------------------------------------------------------------------------------------------- diff --git a/modules/consul-cluster/main.tf b/modules/consul-cluster/main.tf index 9c188f34..7faf9bb7 100644 --- a/modules/consul-cluster/main.tf +++ b/modules/consul-cluster/main.tf @@ -52,6 +52,20 @@ resource "aws_autoscaling_group" "autoscaling_group" { ] ) + dynamic "initial_lifecycle_hook" { + for_each = var.lifecycle_hooks + + content { + name = initial_lifecycle_hook.key + default_result = lookup(initial_lifecycle_hook.value, "default_result", null) + heartbeat_timeout = lookup(initial_lifecycle_hook.value, "heartbeat_timeout", null) + lifecycle_transition = initial_lifecycle_hook.value.lifecycle_transition + notification_metadata = lookup(initial_lifecycle_hook.value, "notification_metadata", null) + notification_target_arn = lookup(initial_lifecycle_hook.value, "notification_target_arn", null) + role_arn = lookup(initial_lifecycle_hook.value, "role_arn", null) + } + } + lifecycle { # As of AWS Provider 3.x, inline load_balancers and target_group_arns # in an aws_autoscaling_group take precedence over attachment resources. diff --git a/modules/consul-cluster/variables.tf b/modules/consul-cluster/variables.tf index eb7a941b..6830617e 100644 --- a/modules/consul-cluster/variables.tf +++ b/modules/consul-cluster/variables.tf @@ -122,6 +122,32 @@ variable "termination_policies" { default = "Default" } +variable "lifecycle_hooks" { + description = "The lifecycle hooks to create that are triggered by the launch event. This is a map where the keys are the name of the hook and the values are an object with the keys and values defined in the lifecycle_hook block of the aws_autoscaling_group resource. Default is no launch hooks" + type = map(any) + default = {} + + # example = { + # name = { + # default_result = string : CONTINUE | ABANDON + # heartbeat_timeout = int + # lifecycle_transition = string : "autoscaling:EC2_INSTANCE_LAUNCHING" | "autoscaling:EC2_INSTANCE_TERMINATING" + # notification_metadata = string + # notification_target_arn = string + # role_arn = string + # } + + validation { + condition = alltrue([for x in values(var.lifecycle_hooks) : contains(["CONTINUE", "ABANDON"], lookup(x, "default_result", "CONTINUE"))]) + error_message = "Lifecycle_hooks[x].default_result must be set to either \"CONTINUE\" or \"ABANDON\"." + } + + validation { + condition = alltrue([for x in values(var.lifecycle_hooks) : contains(["autoscaling:EC2_INSTANCE_LAUNCHING", "autoscaling:EC2_INSTANCE_TERMINATING"], lookup(x, "lifecycle_transition", "BLANK"))]) + error_message = "Lifecycle_hooks[x].lifecycle_transition must be set to either \"autoscaling:EC2_INSTANCE_LAUNCHING\" or \"autoscaling:EC2_INSTANCE_TERMINATING\"." + } +} + variable "associate_public_ip_address" { description = "If set to true, associate a public IP address with each EC2 Instance in the cluster." type = bool