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?
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 :In your line, what you really feed to bash is :
Considering bash use of quotes, you get the string
inside your
/home/ubuntu/myfile.json
To solve your problem, I would recommend using:
which is escaped as
(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.
If you are trying to generate a file on the EC2 instance being started by CloudFormation, I would recommend the following approach :
In the "Resources" section (adapt your policy accordingly):
In the EC2 Instance resource section :
Adding double quotes in the output should be as simple as escaping them such as
But this is not required for AWS CLI and SDK configuration files
In order to trigger the
AWS::Cloudformation::Init
section of your template, you must explicitly callcfn-init
I am usually doing this from the user data section :