Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudformation output double quotes in a file using Fn::Join

After much research and frustration, I'm not quite getting the output I'm hoping for.

The desired output into a file would be for example

"accessKeyId":"UIIUHO]SOMEKEY[SHPIUIUHIU"

But what I'm getting is

accessKeyId:UIIUHO]SOMEKEY[SHPIUIUHIU

Below is the line in an AWS Cloudformation template

{"Fn::Join": ["", ["echo \" accessKeyId:", {"Ref": "AccessKeyId"}, "\" >>  /home/ubuntu/myfile.json"] ] }, 

I've tried adding \" with in the echo statement but no quotes are output. Can someone show how to produce the desired output above?

like image 666
Lui Avatar asked Oct 16 '25 00:10

Lui


1 Answers

It's a problem of correctly escaping the quotes in fact.

Reason is : \" inside a CloudFormation string is escaped as " (double-quote).

For example, "hello \"me\"" gives you :

hello "me"

In your line, what you really feed to bash is :

echo " accessKeyId:XXXXX" >> /home/ubuntu/myfile.json

Considering bash use of quotes, you get the string

accessKeyId:XXXXX

inside your /home/ubuntu/myfile.json

To solve your problem, I would recommend using:

{"Fn::Join": ["", ["echo '\"accessKeyId\":\"", {"Ref": "AccessKeyId"}, "\"' >>  /home/ubuntu/myfile.json"] ] },

which is escaped as

echo '"accessKeyId":"XXXXX"' >>  /home/ubuntu/myfile.json

(hard to read : the whole string used by echo is inside single-quotes).

I'm not able to try it now, but it should do the trick.

like image 71
huelbois Avatar answered Oct 18 '25 15:10

huelbois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!