Search code examples
amazon-web-servicesyamlaws-api-gatewayserverless-framework

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 reference syntax for variable stageVariables.capabilitySecurityUrl. You can only reference env vars, options, & files. You can check our docs for more info.

I have tried following options, but not working

1) Using Without Quotes Uri: https://${stageVariables.capabilitySecurityUrl}

2) Using with quotes Uri: "https://${stageVariables.capabilitySecurityUrl}"

3) Access variable from file

./stageVariables.json
{
   "capabilitySecurityUrl":"https://${stageVariables.capabilitySecurityUrl}"
}

./serverless.yml
${file(./stageVariables.json):capabilitySecurityUrl}

Any help?


Solution

  • As author said, customizing variableSyntax worked for me:

      # notice the double quotes for yaml to ignore the escape characters!
      # Use this for allowing CloudFormation Pseudo-Parameters in your serverless.yml
      # e.g. ${stageVariables.my_var}. All other Serverless variables work as usual.
      variableSyntax: "\\${((?!stageVariables)[ ~:a-zA-Z0-9._@'\",\\-\\/\\(\\)]+?)}"
    

    https://www.serverless.com/framework/docs/providers/aws/guide/variables/#using-custom-variable-syntax