Skip to content

Instantly share code, notes, and snippets.

@narcisbcn
Created October 7, 2019 08:47
Show Gist options
  • Save narcisbcn/7c6b4c6f8182a63715d48e9df6054963 to your computer and use it in GitHub Desktop.
Save narcisbcn/7c6b4c6f8182a63715d48e9df6054963 to your computer and use it in GitHub Desktop.
#!/bin/bash
AWS_CREDENTIALS_DIR=~/.aws/
AWS_CREDENTIALS_FILE=credentials
#function profiles() {
# if [ -z "$1" ]
# then
# NUM_PROFILES=$(cat ${AWS_CREDENTIALS_DIR}${AWS_CREDENTIALS_FILE}|grep "^\["|sed "s/\[//"|sed "s/]$//"|wc -l)
# if [ ${NUM_PROFILES} -lt "1" ]
# then
# echo "ERROR: Your credentials file does not contain any profile."
# fi
# fi
#}
#profiles
#AWS_PROFILE=$(cat ${AWS_CREDENTIALS_DIR}${AWS_CREDENTIALS_FILE}|grep "^\["|sed "s/\[//"|sed "s/]$//")
function do_query() {
QUERY=$1
if [ -d ${AWS_CREDENTIALS_DIR} ] && [ -f ${AWS_CREDENTIALS_DIR}/${AWS_CREDENTIALS_FILE} ] ; then
aws ec2 describe-instances --filters "Name=tag:Name,Values=*${QUERY}*" --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[],PrivateIpAddress,PublicIpAddress]' --output text --region us-east-1 | sed '$!N;s/\n/ /'
else
echo "It looks like you don't have a ~/aws/credentials file"
exit 1
fi
}
do_query $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment