From 55b82e1b1444b05e57ad312b05316ae402d2413c Mon Sep 17 00:00:00 2001 From: Marc Billow Date: Mon, 12 Jul 2021 16:38:38 -0500 Subject: [PATCH 1/2] Allow users to opt-out of changing instance name tags --- README.md | 3 +++ lambda/autoscale/autoscale.py | 3 ++- main.tf | 3 ++- variables.tf | 5 +++++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 55d8f0a..dc0d0ed 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,10 @@ Once you have your ASG set up, you can just invoke this module and point to it: module "clever_name_autoscale_dns" { source = "meltwater/asg-dns-handler/aws" version = "x.y.z" + # use_public_ip = true + # update_instance_name_tag = false + autoscale_handler_unique_identifier = "clever_name" autoscale_route53zone_arn = "ABCDEFGHIJ123" vpc_name = "my_vpc" diff --git a/lambda/autoscale/autoscale.py b/lambda/autoscale/autoscale.py index 83da131..aede4f8 100644 --- a/lambda/autoscale/autoscale.py +++ b/lambda/autoscale/autoscale.py @@ -125,7 +125,8 @@ def process_message(message): if operation == "UPSERT": ip = fetch_ip_from_ec2(instance_id) - update_name_tag(instance_id, hostname) + if os.getenv("update_instance_name_tag", "true") == "true": + update_name_tag(instance_id, hostname) else: ip = fetch_ip_from_route53(hostname, zone_id) diff --git a/main.tf b/main.tf index c638d6a..ad53999 100644 --- a/main.tf +++ b/main.tf @@ -115,7 +115,8 @@ resource "aws_lambda_function" "autoscale_handling" { description = "Handles DNS for autoscaling groups by receiving autoscaling notifications and setting/deleting records from route53" environment { variables = { - "use_public_ip" = var.use_public_ip + "use_public_ip" = var.use_public_ip, + "update_instance_name_tag" = var.update_instance_name_tag } } } diff --git a/variables.tf b/variables.tf index d622455..20dbdbc 100644 --- a/variables.tf +++ b/variables.tf @@ -11,6 +11,11 @@ variable "use_public_ip" { default = false } +variable "update_instance_name_tag" { + description = "Update the name tag of the instance" + default = true +} + variable "autoscale_route53zone_arn" { description = "The ARN of route53 zone associated with autoscaling group" } From e7e055399365fcd2cb508adae9672d722bac7ecf Mon Sep 17 00:00:00 2001 From: Marc Billow Date: Tue, 10 Aug 2021 14:00:48 -0500 Subject: [PATCH 2/2] Switch to checking a tag on the ASG to determine behavior --- README.md | 20 +++++++++++++++++--- lambda/autoscale/autoscale.py | 21 ++++++++++++++------- main.tf | 3 +-- variables.tf | 5 ----- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index dc0d0ed..6dda707 100644 --- a/README.md +++ b/README.md @@ -36,16 +36,24 @@ Once you have your ASG set up, you can just invoke this module and point to it: module "clever_name_autoscale_dns" { source = "meltwater/asg-dns-handler/aws" version = "x.y.z" - # use_public_ip = true - # update_instance_name_tag = false - autoscale_handler_unique_identifier = "clever_name" autoscale_route53zone_arn = "ABCDEFGHIJ123" vpc_name = "my_vpc" } ``` +By default, this module will also update the `Name` tag of your instance to match the short name of your pattern. If +you want to opt-out of this behavior, you can add an additional tag to your auto scaling group: + +```hcl +tag { + key = "asg:update_instance_name" + value = "false" + propagate_at_launch = true +} +``` + ## How does it work? The module sets up the following @@ -111,6 +119,12 @@ resource "aws_autoscaling_group" "my_asg" { value = "${var.hostname_prefix}-#instanceid.${var.vpc_name}.testing@${var.internal_zone_id}" propagate_at_launch = true } + + # tag { + # key = "asg:update_instance_name" + # value = "false" + # propagate_at_launch = true + # } } module "autoscale_dns" { diff --git a/lambda/autoscale/autoscale.py b/lambda/autoscale/autoscale.py index aede4f8..4c2ae39 100644 --- a/lambda/autoscale/autoscale.py +++ b/lambda/autoscale/autoscale.py @@ -12,6 +12,7 @@ route53 = boto3.client('route53') HOSTNAME_TAG_NAME = "asg:hostname_pattern" +UPDATE_INSTANCE_TAG_NAME = "asg:update_instance_name" LIFECYCLE_KEY = "LifecycleHookName" ASG_KEY = "AutoScalingGroupName" @@ -45,21 +46,25 @@ def fetch_ip_from_route53(hostname, zone_id): return ip_address # Fetches relevant tags from ASG -# Returns tuple of hostname_pattern, zone_id +# Returns dict of tags def fetch_tag_metadata(asg_name): logger.info("Fetching tags for ASG: %s", asg_name) - tag_value = autoscaling.describe_tags( + tags = autoscaling.describe_tags( Filters=[ {'Name': 'auto-scaling-group','Values': [asg_name]}, - {'Name': 'key','Values': [HOSTNAME_TAG_NAME]} + {'Name': 'key','Values': [HOSTNAME_TAG_NAME, UPDATE_INSTANCE_TAG_NAME]} ], MaxRecords=1 )['Tags'][0]['Value'] - logger.info("Found tags for ASG %s: %s", asg_name, tag_value) + tag_values = { + tag['Key']: tag['Value'] for tag in tags + } - return tag_value.split("@") + logger.info("Found tags for ASG %s: %s", asg_name, tag_values) + + return tag_values # Builds a hostname according to pattern def build_hostname(hostname_pattern, instance_id): @@ -119,13 +124,15 @@ def process_message(message): asg_name = message['AutoScalingGroupName'] instance_id = message['EC2InstanceId'] - hostname_pattern, zone_id = fetch_tag_metadata(asg_name) + asg_tag_metadata = fetch_tag_metadata(asg_name) + + hostname_pattern, zone_id = asg_tag_metadata[HOSTNAME_TAG_NAME].split("@") hostname = build_hostname(hostname_pattern, instance_id) if operation == "UPSERT": ip = fetch_ip_from_ec2(instance_id) - if os.getenv("update_instance_name_tag", "true") == "true": + if asg_tag_metadata.get(UPDATE_INSTANCE_TAG_NAME, "true") == "true": update_name_tag(instance_id, hostname) else: ip = fetch_ip_from_route53(hostname, zone_id) diff --git a/main.tf b/main.tf index ad53999..c638d6a 100644 --- a/main.tf +++ b/main.tf @@ -115,8 +115,7 @@ resource "aws_lambda_function" "autoscale_handling" { description = "Handles DNS for autoscaling groups by receiving autoscaling notifications and setting/deleting records from route53" environment { variables = { - "use_public_ip" = var.use_public_ip, - "update_instance_name_tag" = var.update_instance_name_tag + "use_public_ip" = var.use_public_ip } } } diff --git a/variables.tf b/variables.tf index 20dbdbc..d622455 100644 --- a/variables.tf +++ b/variables.tf @@ -11,11 +11,6 @@ variable "use_public_ip" { default = false } -variable "update_instance_name_tag" { - description = "Update the name tag of the instance" - default = true -} - variable "autoscale_route53zone_arn" { description = "The ARN of route53 zone associated with autoscaling group" }