How to the set the hostname in an AWS ec2 instance to a unique value in an autoscale group

amazon-web-servicescloud

I wanted to find out what is the best practice of setting the hostname of an Ec2 instance that is in an autoscale group.

I am looking for a method that is

  • generally acceptable in the sysadmin/devops world.
  • Does not bring surprise faces when a new ops takes it over.
  • allows me to set a unique hostname/machine name so that in monitoring, I have a unique asset to monitor

The current approach I was going for was to
– pass the desired hostname as part of user-data in a cloud init format in AWS
– use cloud init to suffix the hostname with the current instance ID via the boot cmd

bootcmd:
    - "HOSTNAME_PREFIX='{{ app_id }}'"
    - "REGION_NAME=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id`"
    - "INSTANCE_ID=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id`"
    - "echo $HOSTNAME_PREFIX'-'$INSTANCE_ID > /etc/hostname; hostname -F /etc/hostname"

This is especially important when in auto scale groups when instances can be created on the fly. Eventhough we should be treating the servers as cattle, I think a unique hostname at least helps better identify the servers.
Is there a better approach to this?

Best Answer

This is more or less the approach that I take. It makes a lot of sense to include the instance ID in the hostname, as this makes it very easy to identify and associate running servers with instances in the web console or other API commands.

Is there a better approach to this?

What's your definition of "better"?

If it works for you, just go with it. Instance IDs are guaranteed to be unique within an account, and you already have a solution using those IDs, so it sounds like you're all set.