Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AWS CLI issues with Tags

Posted on 2016-11-23
3
Medium Priority
?
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

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 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

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 look for a specific file type in a local or remote server directory using PHP.

660 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