Search code examples
bashamazon-web-servicesaws-clixargsjmespath

xargs not splitting on whitespace from aws cli output


This commands returns all the AWS regions separated by whitespace:

aws ec2 describe-regions --query 'Regions[*].RegionName' --output text

eu-north-1  ap-south-1  eu-west-3   eu-west-2   eu-west-1   ap-northeast-2  ap-northeast-1  sa-east-1   ca-central-1    ap-southeast-1  ap-southeast-2  eu-central-1    us-east-1   us-east-2   us-west-1   us-west-2

I'm trying to pipe this to xargs but it's seeing it as a single string:

aws ec2 describe-regions --query 'Regions[*].RegionName' --output text | gxargs -I {} aws cloudformation list-stacks --region {}

Invalid endpoint: https://cloudformation.eu-north-1 ap-south-1  eu-west-3   eu-west-2   eu-west-1   ap-northeast-2  ap-northeast-1  sa-east-1   ca-central-1    ap-southeast-1  ap-southeast-2  eu-central-1    us-east-1   us-east-2   us-west-1   us-west-2.amazonaws.com
gxargs: aws: exited with status 255; aborting

gxargs is just gnu xargs (I'm on Mac).

Also, tried this to use jmespath to create a string from an array with a specific delimiter (which I could use with xargs):

aws ec2 describe-regions --query 'Regions[*].join(",",@.RegionName)'
In function join(), invalid type for value: None, expected one of: ['string'], received: "null"

EDIT: just following up, this is what I wound up with. It insists on throwing an error when it doesn't find a stack- probably the same for other aws cli commands

aws ec2 describe-regions --query 'Regions[*].RegionName' --output text | gxargs -n 1 sh -c 'aws cloudformation describe-stacks --stack-name findme --region $0 || true'

Solution

  • Here's man xargs for -I:

    -I replace-str
    Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.

    You can use xargs -n 1 aws cloudformation list-stacks --region instead