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

15,191

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.

Share:
15,191
Lui
Author by

Lui

Updated on June 06, 2022

Comments

  • Lui
    Lui almost 2 years

    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?