2

I wrote a script to create sqs resource on local stack. I wanted to pass a value that I get from one cli command to the next but inside an inline json. Following is the section of the script in question.

arn=$(aws --endpoint-url=http://localhost:4576 sqs get-queue-attributes \
--queue-url http://localhost:4576/my_dead_letter_queue_url \
--query 'Attributes.QueueArn' \
--output text)

aws --endpoint-url=http://localhost:4576 sqs create-queue \
--queue-name my_queue \
--attributes \
'{"RedrivePolicy":"{\"deadLetterTargetArn\":\"$arn\", \"maxReceiveCount\":\"5\"}"}'

So I'm trying to pass that "arn" variable but the cli is taking that as a string and trying to find a sqs with url "$arn" and fails. I also tried removing the quote. In that case, the error is malformed string.

Instead of the arn variable, if I use the arn value as string there, it works.

Can someone please show me how to pass that variable inside that inline json if it is possible?

Thank you for reading :)

Shahed

Shahed
  • 131
  • 1
  • 2
  • 6
  • This is similar to another question, answered here: https://stackoverflow.com/a/36778045/3121039 Try those suggestions on using a "here document". (You can also just echo the string to see that `$arn` does not expand when enclosed in single-quotes.) – Eric Bolinger Apr 11 '20 at 17:14
  • I will give that a try, thank you. – Shahed Apr 11 '20 at 17:37

2 Answers2

4

I was able to do the following with successful results, grant it it doesn't process the json (for that I'm just replacing tokens via sed), but I updated my example and tested it at least in bash with what I was doing:

#!/bin/bash
export awscmd="aws --region us-east-1 iam"
function setArn() {
  ${awscmd} list-policies --query 'Policies[?PolicyName==`'${1}'`].{ARN:Arn}' --output text
}

arn=$(setArn "some-policy-name")
echo '{"RedrivePolicy":"{"deadLetterTargetArn":"'$arn'", "maxReceiveCount":"5"}"}'

$ ./somearntest.sh
{"RedrivePolicy":"{"deadLetterTargetArn":"arn:aws:iam::############:policy/some-policy-name", "maxReceiveCount":"5"}"}

Notice the use of single tics to concatenate the output result outside of the string. This is in bash 4 and I removed the escaped \"s as I think that was added in error; ymmv.

Pavman
  • 125
  • 7
  • Thank you for the example. I will give it a try, I get a feeling this will work for me. Thanks again – Shahed Sep 11 '20 at 09:30
2

The problem here is you are trying to expand a bash variable inside single quotes. Using single quotes like this is usually to pass a bunch of strings and unqoutable stuff as one argument. If you can't replace them with double quotes you'll have to resort to dirty eval hacks, which I do not recommend.

Here is an example:

$ arn=foobar
$ echo '{"RedrivePolicy":"{\"deadLetterTargetArn\":\"$arn\", \"maxReceiveCount\":\"5\"}"}'
{"RedrivePolicy":"{\"deadLetterTargetArn\":\"$arn\", \"maxReceiveCount\":\"5\"}"}
$ eval echo '{"RedrivePolicy":"{\"deadLetterTargetArn\":\"$arn\", \"maxReceiveCount\":\"5\"}"}'
{RedrivePolicy:{"deadLetterTargetArn":"foobar", "maxReceiveCount":"5"}}

For more information I suggest to check How eval works and Expansion of variables inside single quotes

morbeo
  • 87
  • 4
  • 1
    Thank you. I will do a bit of research and see what I find out about this. Thanks again for pointing it out. – Shahed Apr 14 '20 at 16:08