Skip to content

Instantly share code, notes, and snippets.

@aguy
Last active December 29, 2016 12:39
Show Gist options
  • Save aguy/dd9646eba1e4aebec5f1 to your computer and use it in GitHub Desktop.
Save aguy/dd9646eba1e4aebec5f1 to your computer and use it in GitHub Desktop.
ec2 rename from tag
#!/bin/bash
REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | awk -F\" '{print $4}')
FQDN=$(aws ec2 describe-tags --region $REGION --filters "Name=resource-id,Values=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)" "Name=key,Values=Name" --query Tags[].Value --output text)
HOSTNAME=$(echo $FQDN | cut -d. -f1)
IPADDRESS=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
if [ -z "$HOSTNAME" ]; then
echo "You forgot to set Hostname in Tag Name."
exit 0
fi
echo $HOSTNAME > /etc/hostname
hostname $HOSTNAME
cat > /etc/hosts << EOF
127.0.0.1 localhost.localdomain localhost smtp
$IPADDRESS $FQDN $HOSTNAME
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment