Solved

AWS CLI issues with Tags

Posted on 2016-11-23
  • Linux
  • Shell Scripting
  • jQuery
  • AWS
  • Scripting Languages
  • +1
3
86 Views
Last Modified: 2016-11-28
On AWS CLI, I have a script to get the instance details, There are many ways we can generate the instance information I preferred to chose "JQ", Here is the script

I am not able to figure out the issue, Can someone help....

JSON_STATUS=`aws ec2 describe-instances --filter Name=tag:Name,Values=EHI-*--output json`
INSTANCE_COUNT=`echo $JSON_STATUS | jq '.Reservations | length' 2>&1`
echo "Running instances: $INSTANCE_COUNT"
for (( INSTANCE_IDX=0; INSTANCE_IDX<$INSTANCE_COUNT; INSTANCE_IDX++ ))
do
        INSTANCE_STATUS=`echo $JSON_STATUS | jq ".Reservations[$INSTANCE_IDX].Instances[0].State.Name"`
        INSTANCE_NAME=`echo $JSON_STATUS | jq ".Reservations[$INSTANCE_IDX].Instances[0].InstanceId"`
        INSTANCE_NAME1=`echo $JSON_STATUS | jq  ".Reservations[$INSTANCE_IDX].Instances[0].Tags[] | select ( .Key | contains("Name") ) | .Value"`
        INSTANCE_IP=`echo $JSON_STATUS | jq ".Reservations[$INSTANCE_IDX].Instances[0].NetworkInterfaces[0].PrivateIpAddress"`
        INSTANCE_DATE_UP=`echo $JSON_STATUS | jq ".Reservations[$INSTANCE_IDX].Instances[0].LaunchTime"`
        echo "INSTANCE $INSTANCE_IDX [$INSTANCE_NAME]: $INSTANCE_NAME1  $INSTANCE_STATUS @ $INSTANCE_IP "
done

Open in new window


Here is the error -->
-----------------------------------------------------------------------------------------------------------------------
Running instances: 8
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[0].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value
jq: 1 compile error
INSTANCE 0 ["i-caf2b153"]:   "stopped" @ "10.64.36.200"
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[1].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value
jq: 1 compile error
INSTANCE 1 ["i-fb98fa03"]:   "running" @ "10.64.34.58"
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[2].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value
jq: 1 compile error
INSTANCE 2 ["i-31b8m3c9"]:   "running" @ "10.64.35.161"
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[3].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value
jq: 1 compile error
INSTANCE 3 ["i-1f48n8e7"]:   "running" @ "10.64.34.223"
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[4].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value
jq: 1 compile error
INSTANCE 4 ["i-be3ofea8"]:   "running" @ "10.64.36.127"
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[5].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value
jq: 1 compile error
INSTANCE 5 ["i-018e19f9"]:   "running" @ "10.64.38.10"
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[6].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value
jq: 1 compile error
INSTANCE 6 ["i-c8fc9030"]:   "running" @ "10.64.34.230"
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[7].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value
jq: 1 compile error
INSTANCE 7 ["i-edf11q75"]:   "running" @ "10.64.36.253"
------------------------------------------------------------------------------------------------------------------------
0
Comment
Question by:Rama Badra
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41902385
You need to escape your double quotes around Name : contains(\"Name\")

JSON_STATUS=`aws ec2 describe-instances --filter Name=tag:Name,Values=EHI-* --output json`
INSTANCE_COUNT=`echo $JSON_STATUS | jq '.Reservations | length' 2>&1`
echo "Running instances: $INSTANCE_COUNT"
for (( INSTANCE_IDX=0; INSTANCE_IDX<$INSTANCE_COUNT; INSTANCE_IDX++ ))
do
        INSTANCE_STATUS=`echo $JSON_STATUS | jq ".Reservations[$INSTANCE_IDX].Instances[0].State.Name"`
        INSTANCE_NAME=`echo $JSON_STATUS | jq ".Reservations[$INSTANCE_IDX].Instances[0].InstanceId"`
        INSTANCE_NAME1=`echo $JSON_STATUS | jq  ".Reservations[$INSTANCE_IDX].Instances[0].Tags[] | select ( .Key | contains(\"Name\") ) | .Value"`
        INSTANCE_IP=`echo $JSON_STATUS | jq ".Reservations[$INSTANCE_IDX].Instances[0].NetworkInterfaces[0].PrivateIpAddress"`
        INSTANCE_DATE_UP=`echo $JSON_STATUS | jq ".Reservations[$INSTANCE_IDX].Instances[0].LaunchTime"`
        echo "INSTANCE $INSTANCE_IDX [$INSTANCE_NAME]: $INSTANCE_NAME1  $INSTANCE_STATUS @ $INSTANCE_IP "
done

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 41902392
Work with single quotes for the jq parameter and double quotes around Name :
MBP-de-leakim:Downloads leakim$ echo $JSON_STATUS | /Users/leakim/bin/jq '.Reservations[0].Instances[0].Tags[] | select ( .Key | contains("Name") ) | .Value'
"TEST-1"

Open in new window

Doesn't work escaping the single quotes...
MBP-de-leakim:Downloads leakim$ echo $JSON_STATUS | /Users/leakim/bin/jq '.Reservations[0].Instances[0].Tags[] | select ( .Key | contains(\'Name\') ) | .Value'
-bash: syntax error near unexpected token `)'

Open in new window

Doesn't work not putting quotes :
MBP-de-leakim:Downloads leakim$ echo $JSON_STATUS | /Users/leakim/bin/jq '.Reservations[0].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value'
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[0].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value                                                                
jq: 1 compile error

Open in new window

Don't work using double quotes for jq command and not escaping the double around the Name :
MBP-de-leakim:Downloads leakim$ echo $JSON_STATUS | /Users/leakim/bin/jq ".Reservations[0].Instances[0].Tags[] | select ( .Key | contains("Name") ) | .Value"
jq: error: Name/0 is not defined at <top-level>, line 1:
.Reservations[0].Instances[0].Tags[] | select ( .Key | contains(Name) ) | .Value                                                                
jq: 1 compile error

Open in new window

Work with double quotes escaped :
MBP-de-leakim:Downloads leakim$ echo $JSON_STATUS | /Users/leakim/bin/jq ".Reservations[0].Instances[0].Tags[] | select ( .Key | contains(\"Name\") ) | .Value"
"TEST-1"

Open in new window

0
 

Author Closing Comment

by:Rama Badra
ID: 41904386
Thanks for the info, The issue is now resolved
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Happy holidays! Your Ops team can pack their bags. IT management and IT management tools are dead. Or at least that’s according to a new blog from Tech Target on AWS’s new Managed Services (MS) offering.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question