Solved

AWS CLI issues with Tags

Posted on 2016-11-23
  • Linux
  • Shell Scripting
  • jQuery
  • AWS
  • Scripting Languages
  • +1
3
32 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
AWS Glacier is Amazons cheapest storage option and is their answer to a ‘Cold’ storage service.  Customers primarily use this service for archival purposes and storage of infrastructure backups.  Its unlimited storage potential and low storage cost …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now