Search code examples
amazon-web-servicesaws-clijmespathaws-shell

AWS CLI - result using jmespath query


I've a problem so you will safe my life :-)

when I run the following command from aws-shell

cloudformation describe-stacks --query Stacks[*].[StackName,StackId,CreationTime,LastUpdatedTime,Parameters[?ParameterKey==\`PARAM_NAME1\`].ParameterValue,Parameters[?ParameterKey==\`PARAM_NAME2\`].ParameterValue] --output text

the result is splitted on 3 rows:

automation-X arn:aws:cloudformation:X X None
PARAM_VALUE1
PARAM_VALUE2

but my goal is to have only one row (like this)

automation-X arn:aws:cloudformation:X X None PARAM_VALUE1 PARAM_VALUE2

that is, having PARAM_VALUE1 and PARAM_VALUE2 on the first and only line.

can anyone help me?

I appreciate it, thank you in advance

I forgot to indicate that the result of the command are many lines (more than 1000) and each line is composed of 6 parameters


Solution

  • The expression

    Parameters[?ParameterKey==`PARAM_VALUE1`].ParameterValue
    

    returns a projection that itself returns an array of ParameterValues. Even though that array only contains one item, the aws --output text mode still interprets it as a new line. To fix this, you need to convert the projection into a single value using a pipe | to stop the projection, then choose the first item in the array:

    aws cloudformation describe-stacks --query 'Stacks[*].[StackName,StackId,CreationTime,LastUpdatedTime,Parameters[?ParameterKey==`PARAM_NAME1`].ParameterValue|[0],Parameters[?ParameterKey==`PARAM_NAME2`].ParameterValue|[0]]' --output text
    

    You'll see the |[0] added to each parameter in the query.