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?