Empty elements returned in CLI query
I want to extract fields under the following circumstances:
- Instance has a tag named Version with a value of 3.0.0
- Instance is in the running state
- Instance is not using an ImageId of ami-0b3f1fa3dfff8f017
Of those instances, I wish to output the following information:
- Value of the Tag named Service
- Value of Tag named ServiceOwner
- InstanceId
- ImageId
The query I have defined is as follows (formatted for readability):
aws ec2 describe-instances
--filter "Name=instance-statename,Values=running,Name=tag:Version,Values=3.0.0"
--query 'Reservations[*].Instances[?ImageId!=`ami-0b3f1fa3dfff8f017`].{Service:Tags[?Key==`Service`].Value,Service:Tags[?Key==`ServiceOwner`].Value,InstanceId:InstanceId,AMI:ImageId}'
Whilst this seems to give me the filtered output I need, I can see empty elements within the JSON output:
[
[
{
"InstanceId": "exampleText",
"AMI": "exampleText",
"ServiceOwner": [
"exampleText"
],
"Service": [
"exampleText"
]
}
],
[],
[
{
"InstanceId": "exampleText",
"AMI": "exampleText",
"ServiceOwner": [
"exampleText"
],
"Service": [
"exampleText"
]
}
]
]
With this in mind I would like to understand:
- Why would I have empty elements in my JSON structure - are they instances that do not meet my filtering criteria?
- How would I improve the output to flatten ServiceOwner and Service into a sigular JSON attribute instead of an array 3.When attempting to push the output into the table format I receive an index error - will the answers to 1 and 2 correct that?
The version of the AWS CLI that I am using is aws-cli/1.15.66 Python/2.7.9 Windows/7 botocore/1.10.65
@JamieKeeling - Thank you for reporting this behavior. I've used the information you provided to runs some tests and have different results.
I would like to understand "Why would I have empty elements in my JSON structure - are they instances that do not meet my filtering criteria?" before moving onto 2. and 3.
Are the elements empty across all of your instances or just some? Please use the the following EC2 command on a couple of Instances returning empty elements (or any of your Instances) and confirm if the element are still empty:
aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxxxxx
I'd like to understand if the same results return without the filter.
I look forward to hearing from you.
Hi @justnance,
I'm not able to use your query against instances returning the empty array element as it seems they're missing entirely:

I do not see empty arrays when using your query with an existing instance, such as i-0403442dd10d828a3 shown.
@JamieKeeling - Thank you for your feedback and screen shot. I was able to reproduce this issue on both Windows 7 and MAC OS. I used the latest version of the CLI and the version reported in your first response. The repro steps also included launching 8 instances in 3 separate launch requests which created 3 Reservation IDs. One launch request for 3 instances, a second launch for 4 instances, and a third launch for a single Instance to which all launch requests used different AMIs. Next, I added Tags with a variety of keys and values including null values.
Regarding, "Why would I have empty elements in my JSON structure - are they instances that do not meet my filtering criteria?"
The empty element in the array is because Reservations[*] is returning all Reservations including the ones that do not have Instances that match the AMI ID. Here is the command I used to reproduce the same results:
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running,Name=tag:Version,Values=3.0.0" --query "Reservations[*].Instances[?ImageId!=`ami-0c5a578433f91e821`].{Service:Tags[?Key==`Service`].Value,Service:Tags[?Key==`ServiceOwner`].Value,InstanceId:InstanceId,AMI:ImageId}"
As such, the --output table also returns following error:
Row should have 0 elements, instead it has 4
By design, this error is expected if the table output does not support rows with a variable number of columns. It is recommended to flattening the list of lists that contain a variable number of items using jmespath query.
Next, I modified the syntax to show more information related to the empty array and successfully output to results to json, text, and table:
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running,Name=tag:Version,Values=3.0.0" --query "Reservations[*].{ReservationId:ReservationId,Instances:Instances[?ImageId!=`ami-0c5a578433f91e821`].{InstanceId:InstanceId,AMI:ImageId,Service:Tags[?Key==`Service`]|[0].Value,ServiceOwner:Tags[?Key==`ServiceOwner`]|[0].Value}}"
The JSON output looks something like this.
[
{
"Instances": [],
"ReservationId": "r-0896a14afab10cae3"
},
{
"Instances": [
{
"InstanceId": "i-055344fc6aae20fdf",
"AMI": "ami-02bf319a7b460e2bd",
"ServiceOwner": "OwnerTest2",
"Service": "ServiceTest2"
}
],
"ReservationId": "r-07879483f760c4eb8"
},
]
Regarding, "How would I improve the output to flatten ServiceOwner and Service into a singular JSON attribute instead of an array?":
The following syntax shows how to flatten the list of lists and shows three different results:
Example: Displays the empty array.
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running,Name=tag:Version,Values=3.0.0" --query "Reservations[*].{ReservationId:ReservationId,Instances:Instances[?ImageId!=`ami-0c5a578433f91e821`].{InstanceId:InstanceId,AMI:ImageId,Service:Tags[?Key==`Service`]|[0].Value,ServiceOwner:Tags[?Key==`ServiceOwner`]|[0].Value}}"
Example: Displays Reservation ID
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running,Name=tag:Version,Values=3.0.0" --query "Reservations[].Instances[?ImageId!=`ami-0c5a578433f91e821`].{InstanceId:InstanceId,AMI:ImageId,Service:Tags[?Key==`Service`]|[0].Value,ServiceOwner:Tags[?Key==`ServiceOwner`]|[0].Value}"
Example: Excluding both the empty array and Reservation ID
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running,Name=tag:Version,Values=3.0.0" --query "Reservations[*].Instances[?ImageId!=`ami-0c5a578433f91e821`].{InstanceId:InstanceId,AMI:ImageId,Service:Tags[?Key==`Service`]|[0].Value,ServiceOwner:Tags[?Key==`ServiceOwner`]|[0].Value}|[]"
Adding the --output table to which the last one returns:
------------------------------------------------------------------------------------------
| DescribeInstances |
+-----------------------+-----------------------+---------------+------------------------+
| AMI | InstanceId | Service | ServiceOwner |
+-----------------------+-----------------------+---------------+------------------------+
| ami-02bf319a7b460e2bd| i-055344fc6aae20fdf | ServiceTest2 | OwnerTest2 |
| ami-0453125a0771a994e| i-0e2eac8a6289398d7 | ServiceTest | OwnerTypo, OwnerTest |
| ami-0453125a0771a994e| i-095a25f8a1e233e04 | | None |
| ami-0453125a0771a994e| i-01407ae48fa5b5e39 | ServiceTest | OwnerTest |
+-----------------------+-----------------------+---------------+------------------------+
Regarding "When attempting to push the output into the table format I receive an index error - will the answers to 1 and 2 correct that?": correct. Please see my previous examples and append --output table to test the results.
It looks like the ec2 describe-instances command could use some more examples in our documentation so I will escalate this issue internally to our writers.
Hope this helps.
Related Issues: #368, #3925, #3760, #3496, #3190, #3182, and #3164
This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.