Last active
April 1, 2019 18:46
-
-
Save jayers99/2208e49f67eb1568ada2659ca2ecd0a0 to your computer and use it in GitHub Desktop.
resolve some sort of name for an aws interface id
This file contains hidden or 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 | |
# todo - add a cache so it will not have to query repeat eni ids | |
instanceId="" | |
instanceName="" | |
interfaceInfo="" | |
exitcode="" | |
declare -A cache=() | |
interfaceId=$1 | |
if [ ${cache[${interfaceId}]} ]; then | |
instanceName="${cache[${interfaceId}]}" | |
else | |
interfaceInfo=$(aws ec2 describe-network-interfaces --network-interface-id $interfaceId 2> /dev/null) | |
exitcode=$? | |
if [[ "$exitcode" == "0" ]]; then | |
if [[ "$interfaceInfo" != "" ]]; then | |
instanceId=$(echo $interfaceInfo | jq -r '.NetworkInterfaces[0].Attachment.InstanceId') | |
if [[ "$instanceId" == "null" ]]; then | |
instanceName=$(echo $interfaceInfo | jq -r '.NetworkInterfaces[0].Description') | |
else | |
instanceName=$(aws ec2 describe-instances --instance-ids $instanceId | jq -r '.Reservations[0].Instances[0] | (.Tags[] | select(.Key=="Name") | .Value)' 2> /dev/null) | |
if [[ "$?" != "0" || "$instanceName" == "" ]]; then | |
instanceName=$instanceId | |
fi | |
fi | |
else | |
instanceName="nullJson" | |
fi | |
elif [[ "$exitcode" == "255" ]]; then | |
instanceName="InterfaceNotFound" | |
else | |
instanceName="UnknownError" | |
fi | |
cache+=(["$interfaceId"]="$instanceName") | |
fi | |
echo $interfaceId $instanceName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment