Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #223 from mr-miles/mr-miles-lifecycle-patch
Browse files Browse the repository at this point in the history
Enable setting up lifecycle hooks on the ASG
  • Loading branch information
brikis98 authored May 28, 2021
2 parents d8900c1 + bca6470 commit ea16058
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
9 changes: 5 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

# ---------------------------------------------------------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions modules/consul-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions modules/consul-cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea16058

Please sign in to comment.