Skip to content

Instantly share code, notes, and snippets.

@rwiggins
Created May 30, 2018 00:01
Show Gist options
  • Save rwiggins/53dcb7be6f248e37283db8283fd933b4 to your computer and use it in GitHub Desktop.
Save rwiggins/53dcb7be6f248e37283db8283fd933b4 to your computer and use it in GitHub Desktop.
Set AWS EC2 instance hostname using cloud-config's bootcmd as userdata. Useful for autoscaling group launch configurations. Uses a prefix + instance ID, useful for Consul hosts for example.
#cloud-config
bootcmd:
- HOSTNAME=my-prefix-"$INSTANCE_ID"
- echo "$HOSTNAME" > /etc/hostname && hostname -F /etc/hostname
- grep -q "127.0.0.1 $HOSTNAME" /etc/hosts || echo "127.0.0.1 $HOSTNAME" >> /etc/hosts
- grep -q "::1 $HOSTNAME" /etc/hosts || echo "::1 $HOSTNAME" >> /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment