Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ref as the first argument in Fn::Sub intrinsic function

I experience quite strange issues when compiling the template, where I reference a string parameter in Fn::Sub, while the docs do explicitly say that one can use Ref function inside of Fn::Sub. Here is a piece of template:

"Resources": {
    "LaunchConfiguration": {
      "Type" : "AWS::AutoScaling::LaunchConfiguration",
      "Properties" : {
        "UserData": { "Fn::Base64": { "Fn::Sub": { "Ref": "UserDataParam" } } },

And here is an error I get:

Template error: One or more Fn::Sub intrinsic functions don't specify expected arguments. Specify a string as first argument, and an optional second argument to specify a mapping of values to replace in the string

When I use full notation: { "Fn::Sub": [ { "Ref": "UserDataParam" }, {} ] }, I get exactly the same error. Has anybody had the same issue? And is it possible to avoid it while still using the parameter?

like image 513
Dmitry Deryabin Avatar asked Jun 14 '17 14:06

Dmitry Deryabin


1 Answers

You are misreading the docs. While the docs do say you can use the Ref function, it is only in the second (optional) parameter (the key-value map) where you can do that.

The example given in the docs is:

{"Fn::Sub": ["www.${Domain}", {"Domain": {"Ref": "RootDomainName"}}]}

If, however, you need to substitute into the first parameter, you must use the dollar notation.

To achieve what you appear to want, you should rewrite your code as:

{"Fn::Sub": "${UserDataParam}"}

Or in context:

"UserData": {"Fn::Base64": {"Fn::Sub": "${UserDataParam}"}}
like image 147
Alex Harvey Avatar answered Oct 13 '22 14:10

Alex Harvey