question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for escaping of variable syntax in `serverless.yml`

See original GitHub issue

Allow for escaping of reserved variable characters (i.e. ${…)?

From this forum post http://forum.serverless.com/t/escaping-variable-syntax/1045?u=brianneisler

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 placed 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.

We should allow for this type of character escape.

Perhaps a simple approach is \${}

Proposed solution

(Added in later turn by maintainers)

Ideally \${} and \\\${} should escape, but \\${} and \\\\${} not. That can neatly be achieved by resolving templates via state machine (as one here: https://github.com/medikoo/es6-template-strings/blob/6a7ec2ec4fdf95176f48584e550a81c494c03b1c/compile.js), which should also be intelligent in handling nested variable resolution properly.

  1. Implement a state machine, through which from every string we can retrieve eventual configured variables
  2. Apply a variablesSynteaxregex on resolved variable, to ensure it matches our format, and if it passes resolve further variable as it happens currently

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:28
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

20reactions
cornfeedhobocommented, May 11, 2018

Maybe escaping should follow the same pattern as terraform; provide a double dollar sign ($${}) option to escape variables that the user expects CloudFormation to parse, and not Serverless?

You can escape interpolation with double dollar signs: $${foo} will be rendered as a literal ${foo}.

Reference: https://www.terraform.io/docs/configuration/interpolation.html

19reactions
ernieaycommented, Mar 8, 2018

another workaround would be to escape the ${ part by merging separate parts using the Join function:

Key:
Fn::Join: ['', ['$', '{timestamp()}']]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Escaping Variable Syntax - Serverless Framework
I tried escaping it with a backslash, but it seems that escaping is then itself escaped as my backslashes are propagated further to...
Read more >
How to escape $ sign in serverless yaml config?
I want to escape $ sign when using stage variables for api gateway. Following error appears when I try to deploy. Invalid variable...
Read more >
Environment Variables - Travis CI Docs
Define environment variables in your .travis.yml in the env key, quoting special ... So make sure to escape any Bash special characters accordingly....
Read more >
Template File Reference - The Serverless Way
This means we support your template files in either json or yaml/yml. ... It allows you to iterate over values, define variables or...
Read more >
Substituting variable values | Cloud Build Documentation
Substitutions are helpful for variables whose value isn't known until build time, or to re-use an ... gcloud builds submit --config=cloudbuild.yaml \
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found