Expected Behavior
When resources are deployed using autoscaling group with Terminating lifecycle hook setup, Terraform destroy command should invoke/trigger the terminating lifecyclehook and wait for the instance to be terminated before the terminating lifecyclehook is destroyed.
Actual Behavior
When resources are deployed using autoscaling group with Terminating lifecycle hook setup, Terraform destroy command does not trigger terminatnig lifecyclehook, instead noticed that the lifecycle resource is destroyed first before autoscaing group/instances are terminated/destroyed.
Steps to Reproduce the Problem
Below code is used for deploying autoscaling group, with initial lifecycle hook and a separate lifecycle resouce for terminating.
resource "aws_autoscaling_group" "asg_group" {
count = var.autoscaling ? 1 : 0
name = var.asg_name
max_size = var.asg_max_size
min_size = var.asg_min_size
wait_for_capacity_timeout = "0"
health_check_grace_period = var.asg_health_check_grace_period
desired_capacity = var.asg_desired_capacity
launch_configuration = var.operating_system == "rhel" ?
aws_launch_configuration.launch_cf[0].name :
aws_launch_configuration.launch_cf_windows[0].name
vpc_zone_identifier = local.subnet_ids
tags = concat(local.asg_tags,local.custom_tags_format)
timeouts {
delete = var.asg_timeouts
}
initial_lifecycle_hook {
name = "${var.asg_name}-launching"
default_result = "CONTINUE"
heartbeat_timeout = 600
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
}
resource "aws_autoscaling_lifecycle_hook" "terminating" {
count = var.autoscaling ? 1 : 0
name = "${var.asg_name}-terminating"
depends_on = [aws_autoscaling_group.asg_group]
autoscaling_group_name = aws_autoscaling_group.asg_group[0].name
default_result = "CONTINUE"
heartbeat_timeout = 900
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
lifecycle {
create_before_destroy = true
}
}
Solutions Tried:
Tried adding depends_on in aws_autoscaling_lifecycle_hook, but did not help.
depends_on = [aws_autoscaling_group.asg_group]