Created
November 7, 2018 10:08
-
-
Save lifeofguenter/c78bc1ea6201fec4d4c3a925476cfdf1 to your computer and use it in GitHub Desktop.
bash_profile functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# will search either by instance-name or instance-id | |
# on instance-id it will return the first match, on instance-name it will return all matches | |
# will iterate through all your ~/.aws/credentials profiles | |
# | |
# Usage: aws-find-ec2 NAME-OR-INSTANCE-ID | |
# Example output: | |
# ~ aws-find-ec2 i-1234567abcdefghijk | |
# - checking company-master... | |
# - checking company-development... | |
# asg-platform / i-1234567abcdefghijk / 172.0.1.200 | |
aws-find-ec2() { | |
local profile | |
local buf | |
local search_key | |
local return_first_match="1" | |
if [[ "${1}" =~ ^i-[0-9a-f]{17,}$ ]]; then | |
search_key="instance-id" | |
else | |
search_key="tag:Name" | |
return_first_match="0" | |
fi | |
grep '\[' ~/.aws/credentials | tr -d '[]' | while read -r profile; do | |
echo "- checking ${profile}..." | |
buf="$(aws \ | |
--profile "${profile}" \ | |
--output json \ | |
ec2 describe-instances \ | |
--filters "Name=${search_key},Values=${1}" "Name=instance-state-name,Values=running" \ | |
--query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value,InstanceId,PrivateIpAddress]')" | |
if [[ "${buf}" == '[]' ]]; then | |
continue | |
else | |
echo "${buf}" | jq -r '.[][] | "\(.[0][0]) / \(.[1]) / \(.[2])"' | |
if [[ "${return_first_match}" == "1" ]]; then | |
break | |
fi | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment