From 4d392cc17ce845776e8f96a1f5cd0a6ef6e8a90c Mon Sep 17 00:00:00 2001 From: Stefan Hattrell <29941279+digorgonzola@users.noreply.github.com> Date: Fri, 9 May 2025 10:52:34 +1000 Subject: [PATCH] fix: set lifecycle hook name from local and pass to userdata --- main.tf | 14 ++++++++------ templates/cloud-init.tpl | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/main.tf b/main.tf index 31f4cc8..c15bc2f 100644 --- a/main.tf +++ b/main.tf @@ -1,10 +1,12 @@ locals { - name = "squid" + name = "squid" + lifecycle_hook_name = "${local.name}-hook" userdata = templatefile("${path.module}/templates/cloud-init.tpl", { - architecture = local.architecture - aws_region = data.aws_region.current.name - eip_allocation_id = var.enable_eip ? aws_eip.squid[0].id : "" - s3_bucket = module.config_bucket.s3_bucket_id + architecture = local.architecture + aws_region = data.aws_region.current.name + eip_allocation_id = var.enable_eip ? aws_eip.squid[0].id : "" + lifecycle_hook_name = local.lifecycle_hook_name + s3_bucket = module.config_bucket.s3_bucket_id }) } @@ -232,7 +234,7 @@ resource "aws_autoscaling_group" "squid" { vpc_zone_identifier = var.public_subnet_ids initial_lifecycle_hook { - name = "squid-asg-hook" + name = local.lifecycle_hook_name lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" heartbeat_timeout = 300 default_result = "ABANDON" diff --git a/templates/cloud-init.tpl b/templates/cloud-init.tpl index 1119620..4f491f5 100644 --- a/templates/cloud-init.tpl +++ b/templates/cloud-init.tpl @@ -35,6 +35,7 @@ write_files: --allocation-id "${eip_allocation_id}" \ --network-interface-id "$eni_id" \ --private-ip-address "$private_ip" \ + --allow-reassociation \ --region "$region" fi @@ -144,7 +145,7 @@ write_files: # Complete the lifecycle action aws autoscaling complete-lifecycle-action \ - --lifecycle-hook-name squid-asg-hook \ + --lifecycle-hook-name "${lifecycle_hook_name}" \ --auto-scaling-group-name "$asg_name" \ --lifecycle-action-result CONTINUE \ --instance-id "$instanceid" \