Skip to content

Instantly share code, notes, and snippets.

@polds
Created February 7, 2014 00:37
Show Gist options
  • Save polds/8855410 to your computer and use it in GitHub Desktop.
Save polds/8855410 to your computer and use it in GitHub Desktop.
Automatically set our hostname on server startup based on the tag in Amazon.
#!/bin/bash
#
# *************************************************
# chkconfig: 2345 70 99
# description: tasks to perform on server startup
# *************************************************
#
# Installation:
# 1. save as /etc/rc.d/init.d/server
# 2. chmod a+x /etc/rc.d/init.d/server
# 3. /sbin/chkconfig --level 2345 server on
PATH=/bin:/usr/sbin:/usr/bin
# Set our Amazon keys
export AWS_ACCESS_KEY=ACCESS_KEY
export AWS_SECRET_KEY=SECRET_KEY
# Determine our region
REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`
# Set our region
export EC2_URL="https://ec2.$REGION.amazonaws.com"
case $1 in
start)
# Set our hostname to our name tag
host=`/opt/aws/bin/ec2-describe-tags \
--filter "resource-type=instance" \
--filter "resource-id=$(/opt/aws/bin/ec2-metadata -i | cut -d ' ' -f2)" \
--filter "key=Name" | cut -f5`
hostname $host
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment