Skip to content

Instantly share code, notes, and snippets.

@coingraham
Last active April 28, 2017 13:57
Show Gist options
  • Select an option

  • Save coingraham/b5ff2aa04aab4e26d62293febf484b48 to your computer and use it in GitHub Desktop.

Select an option

Save coingraham/b5ff2aa04aab4e26d62293febf484b48 to your computer and use it in GitHub Desktop.
Userdata Retrieval based on Hostname
#!/bin/bash
# Given the name of an instance, it will lookup the instance ID and download the UserData.
NAME=$1
PROFILE=$2
REGION=$3
if [ -z $NAME ] ; then
echo "Usage: $0 <instance_name>"
exit 1
fi
if [ -f "$NAME.userdata" ] ; then
echo -n "Userdata file exists. Do you want to proceed (Y/N) "
read ans
if [ $ans != "Y" ] && [ $ans != "y" ] ; then
echo "Aborting..."
exit 1
fi
fi
# describe instance returns result like the following (fnord is machine name)
# fnord i-092bd1c63d1847df2 running
INSTANCE=`aws ec2 describe-instances --profile $PROFILE --region $REGION --filter "Name=tag-key,Values=Name" "Name=tag-value,Values=$NAME" \
--query 'Reservations[*].Instances[*].InstanceId' --output text `
# echo "Found InstanceId $INSTANCE for $NAME"
if [ -z $INSTANCE ] ; then
echo "Unable to find an instance named $NAME"
exit 1
fi
aws ec2 describe-instance-attribute --profile $PROFILE --region $REGION --instance-id $INSTANCE --attribute userData --query '{data:UserData}' \
--output text | awk '{print $NF}' | base64 --decode > $NAME.userdata
echo "SHA Sum of Userdata follows"
cat $NAME.userdata | shasum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment