Escaping Variable Syntax

Hi, I was wondering whether it’s possible to escape reserved characters (i.e. ${...)?

So suppose I would like to use my/topic/something/${timestamp()} as an S3 object key for writing to an S3 bucket. ${timestamp()} is a valid CloudFormation syntax for referencing IoT SQL functions. However, since this syntax is places in Serverless file, the framework will look at it as if I’m referencing Serverless timestamp() variable and will obviously return an error below:

Invalid variable reference syntax for variable timestamp(). You can only reference env vars, options, & files. You can check our docs for more info.

I tried escaping it with a backslash, but it seems that escaping is then itself escaped as my backslashes are propagated further to the CloudFormation.

Is there any way to correctly escape my example above? That would be a preferred over using a custom variableSyntax.

1 Like

Hey, you able to resolve this issue?

I’m also stuck on the same.

Just opened an issue for this in the serverless project. https://github.com/serverless/serverless/issues/3565

Please upvote!

It seems to be undocumented but you can change the variable syntax by setting variableSyntax in your serverless.yml.

See https://github.com/serverless/serverless/issues/1679

variableSyntax: '\\${{([\\s\\S]+?)}}' # this will match ${{}}

I am aware of variableSyntax option, however that’s less than acceptable and acts as a hacky workaround. Using a custom syntax makes the code out of line compared to any other serverless code/guidelines/examples.

1 Like

you can also just merge the string using the Join function to pseudo-escape it:

Key:
Fn::Join: ['', ['$','{timestamp()}']]
1 Like