aws cli returns an extra 'None' when fetching the first element using --query parameter and with --output text

Viewed 1235

I am getting an extra None in aws-cli (version 1.11.160) with --query parameter and --output text when fetching the first element of the query output.

See the examples below.

$ aws kms list-aliases --query "Aliases[?contains(AliasName,'alias/foo')].TargetKeyId|[0]" --output text a3a1f9d8-a4de-4d0e-803e-137d633df24a None $ aws kms list-aliases --query "Aliases[?contains(AliasName,'alias/foo-bar')].TargetKeyId|[0]" --output text None None

As far as I know this was working till yesterday but from today onwards this extra None comes in and killing our ansible tasks.

Anyone experienced anything similar?

Thanks

3 Answers

I started having this issue in the past few days too. In my case I was querying exports from a cfn stack.

My solution was (since I'll only ever get one result from the query) to change | [0].Value to .Value, which works with --output text.

Some examples:

$ aws cloudformation list-exports --query 'Exports[?Name==`kms-key-arn`] | []'
[
    {
        "ExportingStackId": "arn:aws:cloudformation:ap-southeast-2:111122223333:stack/stack-name/83ea7f30-ba0b-11e8-8b7d-50fae957fc4a",
        "Name": "kms-key-arn",
        "Value": "arn:aws:kms:ap-southeast-2:111122223333:key/a13a4bad-672e-45a3-99c2-c646a9470ffa"
    }
]


$ aws cloudformation list-exports --query 'Exports[?Name==`kms-key-arn`] | [].Value'
[
    "arn:aws:kms:ap-southeast-2:111122223333:key/a13a4bad-672e-45a3-99c2-c646a9470ffa"
]


$ aws cloudformation list-exports --query 'Exports[?Name==`kms-key-arn`] | [].Value' --output text
arn:aws:kms:ap-southeast-2:111122223333:key/a13a4bad-672e-45a3-99c2-c646a9470ffa


aws cloudformation list-exports --query 'Exports[?Name==`kms-key-arn`] | [0].Value' --output text
arn:aws:kms:ap-southeast-2:111122223333:key/a13a4bad-672e-45a3-99c2-c646a9470ffa
None

I'm no closer to finding out why it's happening, but it disproves @LHWizard's theory, or at least indicates there are conditions where that explanation isn't sufficient.

The best explanation is that not every match for your query statement has a TargetKeyId. On my account, there are several Aliases that only have AliasArn and AliasName key/value pairs. The None comes from a null value for TargetKeyId, in other words.

I came across the same issue when listing step functions. I consider it to be a bug. I don't like solutions that ignore the first or last element, expecting it will always be None at that position - at some stage the issue will get fixed and your workaround has introduced a nasty bug.

So, in my case, I did this as a safe workaround (adapt to your needs):

#!/usr/bin/env bash

arn="<step function arn goes here>"

arns=()
for arn in $(aws stepfunctions list-executions --state-machine-arn "$arn" --max-items 50 --query 'executions[].executionArn' --output text); do
    [[ $arn == 'None' ]] || arns+=("$arn")
done

# process execution arns
for arn in "${arns[@]}"; do
    echo "$arn" # or whatever
done
Related