如何在无服务器 yaml 配置中转义 $ 登录?

How to escape $ sign in serverless yaml config?

我想在对 api 网关使用 stage variables 时转义 $ 符号。

当我尝试部署时出现以下错误。

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

我尝试了以下选项,但没有用

1) 不带引号使用 网址:https://${stageVariables.capabilitySecurityUrl}

2) 使用引号 乌里:"https://${stageVariables.capabilitySecurityUrl}"

3) 从文件访问变量

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

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

有什么帮助吗?

您可以尝试使用 Cloudformation Join function:

Fn::Join: ['', ['https://$','{{stageVariables.capabilitySecurityUrl}}']]

正如作者所说,自定义 variableSyntax 对我有用:

  # 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

Serverless

现在原生支持转义变量

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

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