0

I'm trying to parse the output of AWS cli using jq where the jq query contains bash variables and by using the --arg switch like so:

$ aws ec2 describe-instances --region $region | jq --arg environment "$Environment" --arg br "$BUILD_NUMBER" --arg app "$App" -r '.Reservations[].Instances[].Tags[]?.Value | select(startswith("Company-$environment-$br-$app")

I get a new line when I run the command.

This is the output when I use hard coded auto scaling group name:

itai@Itais-MBP ~/Downloads -  $ aws ec2 describe-instances --region $region |   jq --arg environment "$Environment" --arg br "$BUILD_NUMBER" --arg app "$App"   -r '.Reservations[].Instances[].Tags[]?.Value | select(startswith("Company-Dev-60-Api") )'
Company-Dev-60-ApiServersASG-19L10K16RUXTE
Company-Dev-60-ApiServersASG-19L10K16RUXTE
itai@Itais-MBP ~/Downloads -  $

You can see the values of the variables are set:

itai@Itais-MBP ~/Downloads -  $ echo $Environment
Dev
itai@Itais-MBP ~/Downloads -  $ echo $App
Api
itai@Itais-MBP ~/Downloads -  $ echo $BUILD_NUMBER
60

So what am I doing wrong?

1 Answer 1

2

The jq language has its own way of combining variables. In jq, the expression you wrote:

"Company-$environment-$br-$app"

is just a vanilla JSON string.

One option would be to use jq's support for interpolation:

"Company-\($environment)-\($br)-\($app)"

Another option would be to use jq's string-concatenation operator, +.

You could also use join("-"). And so on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.