Solved

AWS CLI issues with Tags

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wifi not working on Raspberry Pi 3? 2 53
AWS Design\Cisco Meraki 4 22
linux SFTP 8 41
why $(this).attr('id'); function not returning value ? 9 30
The purpose of this article is to demonstrate how we can use conditional statements using Python.
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
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)

785 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