无服务器:将文件导入自定义+其他变量

Iri*_*lho 5 serverless-framework serverless

我有一个serverless.common.yml, 其属性应由所有服务共享,其中:

service: ixxxx
custom:
  stage: ${opt:stage, self:provider.stage}
  resourcesStages:
    prod: prod
    dev: dev
  resourcesStage: ${self:custom.resourcesStages.${self:custom.stage}, self:custom.resourcesStages.dev}

lambdaPolicyXRay:
  Effect: Allow
  Action:
    - xray:PutTraceSegments
    - xray:PutTelemetryRecords
  Resource: "*"
Run Code Online (Sandbox Code Playgroud)

并且,另一个serverless.yml位于 services 文件夹中,它使用公共文件的属性:

...

custom: ${file(../../serverless.common.yml):custom}
...
environment:
    stage: ${self:custom.stage}
...
Run Code Online (Sandbox Code Playgroud)

这样,我就可以毫无问题地访问自定义变量(从公共文件)。现在,我想继续将此文件导入到自定义文件中,但添加与此服务相关的新变量,所以我尝试了:

custom: 
  common: ${file(../../serverless.common.yml):custom}
  wsgi:
    app: app.app
    packRequirements: false
  pythonRequirements:
    dockerizePip: non-linux
Run Code Online (Sandbox Code Playgroud)

似乎可以访问,例如:

environment:
    stage: ${self:custom.common.stage}
Run Code Online (Sandbox Code Playgroud)

但现在,我收到错误:

 Serverless Warning --------------------------------------

  A valid service attribute to satisfy the declaration 'self:custom.stage' could not be found.


 Serverless Warning --------------------------------------

  A valid service attribute to satisfy the declaration 'self:custom.stage' could not be found.


  Serverless Error ---------------------------------------

  Trying to populate non string value into a string for variable ${self:custom.stage}. Please make sure the value of the property is a strin
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 0

在您的中,serverless.common.yml您必须将其视为serverless.yml. 在这种情况下${self:custom.stage}不存在,但${self:custom.common.stage}确实存在。

service: ixxxx
custom:
  stage: ${opt:stage, self:provider.stage}
  resourcesStages:
    prod: prod
    dev: dev
  resourcesStage: ${self:custom.common.resourcesStages.${self:custom.common.stage}, self:custom.resourcesStages.dev}

lambdaPolicyXRay:
  Effect: Allow
  Action:
    - xray:PutTraceSegments
    - xray:PutTelemetryRecords
  Resource: "*"
Run Code Online (Sandbox Code Playgroud)