Last active
June 8, 2016 12:04
-
-
Save adriangoransson/c8d5a6f1efad66a134a3a831fcb6d434 to your computer and use it in GitHub Desktop.
aws getinstanceid
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
#!/bin/bash | |
set -euo pipefail | |
if [[ -z "$1" ]]; then | |
echo "Need something to search for!" | |
exit 1 | |
fi | |
getdata() { | |
local name="$1" | |
local results="" | |
results=$(aws ec2 describe-instances \ | |
--filter "Name=tag:Name,Values=${name}") | |
echo "$results" | |
} | |
parsejson() { | |
local input="$1" | |
local jqargs="" | |
jqargs=$(cat <<'EOF' | |
.Reservations[].Instances[] | |
| (.Tags | from_entries) as $tags | |
| { | |
id: .InstanceId, | |
name: $tags.Name, | |
ip: { | |
private: .PrivateIpAddress, | |
public: .PublicIpAddress, | |
subnet: .SubnetId | |
}, | |
machine: { | |
state: .State.Name, | |
type: .InstanceType | |
}, | |
tags: $tags | |
} | |
EOF | |
) | |
echo "$input" | jq "$jqargs" | |
} | |
parsejson "$(getdata "$1")" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment