Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AWS CLI issues with Tags

Posted on 2016-11-23
3
Medium Priority
?
156 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 83

Accepted Solution

by:
leakim971 earned 2000 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 83

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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
As managed cloud service providers, we often get asked to intervene when cloud deployments go awry. Attracted by apparent ease-of-use, flexibility and low computing costs, companies quickly adopt leading public cloud platforms such as Amazon Web Ser…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

824 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