Skip to content

Instantly share code, notes, and snippets.

@adriangoransson
Last active June 8, 2016 12:04
Show Gist options
  • Save adriangoransson/c8d5a6f1efad66a134a3a831fcb6d434 to your computer and use it in GitHub Desktop.
Save adriangoransson/c8d5a6f1efad66a134a3a831fcb6d434 to your computer and use it in GitHub Desktop.
aws getinstanceid
#!/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