6

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?

Zanon
  • 29,231
  • 20
  • 113
  • 126
Jagdish Idhate
  • 7,513
  • 9
  • 35
  • 51
  • 1
    i realize this is old, but did you ever figure this out? having the same issue – CoryDorning Jul 27 '20 at 16:58
  • If I remember correctly, I changed syntax of variable. https://www.serverless.com/framework/docs/providers/aws/guide/variables#using-custom-variable-syntax Let us know if this works. – Jagdish Idhate Jul 28 '20 at 04:55

3 Answers3

2

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

Xavier Garnier
  • 139
  • 2
  • 2
  • 13
1

You can try using the Cloudformation Join function:

Fn::Join: ['', ['https://$','{{stageVariables.capabilitySecurityUrl}}']]
CaRnAgE
  • 53
  • 5
1

Escaping variables is now natively supported by Serverless

https://github.com/serverless/serverless/issues/3565

Closing, as escaping is supported by a new resolver, e.g \${self:} will be taken literally

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99