Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to opt-out of changing instance name tags #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ module "clever_name_autoscale_dns" {
}
```

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
Expand Down Expand Up @@ -108,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" {
Expand Down
22 changes: 15 additions & 7 deletions lambda/autoscale/autoscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -119,13 +124,16 @@ 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)

update_name_tag(instance_id, hostname)
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)

Expand Down