如何拆分无服务器配置文件?

huangapple go评论56阅读模式
英文:

How to split a serverless configuration file?

问题

I'm trying to split my serverless configuration file serverless.yml into multiple files using the ${file(...)} syntax. I have a provider file config/serverless/provider.yml with the following content:

provider:
  name: ...
  ...

And in my serverless.yml, I've used it in the following way:

provider: ${file(config/serverless/provider.yml):provider}

However, when I run serverless deploy, I get the following error:

Cannot resolve serverless.yml: Variables resolution errored with:
  - Cannot resolve variable at "provider": Value not found at "file"

Please help me to understand how to properly include other files in my serverless configuration.

英文:

I'm trying to split my serverless configuration file serverless.yml into multiple files using the ${file(...)} syntax. I have a provider file config/serverless/provider.yml with the following content:

provider:
  name: ...
  ...

And in my serverless.yml I've used it in the following way:

provider: ${file(config/serverless/provider.yml):provider}

However, when I run serverless deploy, I get the following error:

Cannot resolve serverless.yml: Variables resolution errored with:
  - Cannot resolve variable at "provider": Value not found at "file"

Please help me to understand how to properly include other files in my serverless configuration.


NOTE: I've also tried with this config without success

provider.yml

name: ...
...

serverless.yml

...
resources:
  - ${file(config/serverless/provider.yml)}

答案1

得分: 2

无法解析serverless.yml文件:变量解析出错,具体错误如下:

  • 在“provider”处无法解析变量:在“file”处找不到值

请仔细检查文件路径。

确保${file(<此处的路径>):provider}中的路径是正确的。

以下是有效的示例:

  • 文件:serverless.yml
service: serverless-framework-include-files
frameworkVersion: "3"

provider: ${file(config/provider.yml):provider}

functions:
  function1:
    handler: index.handler
  • 文件:config/provider.yml
provider:
  name: aws
  runtime: nodejs18.x

或者,您可以从${file(...):provider}中移除:provider

以下是有效的示例:

  • 文件:serverless2.yml
service: serverless-framework-include-files
frameworkVersion: "3"

provider: ${file(config/provider2.yml)}

functions:
  function1:
    handler: index.handler
  • 文件:config/provider2.yml
name: aws
runtime: nodejs18.x

对于resources:,同样适用。

以下是有效的示例:

  • 文件:serverless.yml
resources:
 - ${file(resources/s3-bucket.yml)}
 - ${file(resources/dynamodb-table.yml)}
  • 文件:resources/s3-bucket.yml
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-bucket
      AccessControl: PublicRead
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: error.html

或者,您可以这样做:

  • 文件:serverless2.yml
resources:
  Resources:
    MyBucket: ${file(resources/s3-bucket2.yml)}
    MyTable: ${file(resources/dynamodb-table2.yml)}
  • 文件:resources/dynamodb-table2.yml
Type: AWS::DynamoDB::Table
Properties:
  TableName: ${self:service}
  AttributeDefinitions:
    - AttributeName: id
      AttributeType: S
  KeySchema:
    - AttributeName: id
      KeyType: HASH
  ProvisionedThroughput:
    ReadCapacityUnits: 1
    WriteCapacityUnits: 1

您可以在GitHub上查看一个可用的示例代码:

英文:
Cannot resolve serverless.yml: Variables resolution errored with:
  - Cannot resolve variable at &quot;provider&quot;: Value not found at &quot;file&quot;

The error is not finding a value at "file" double check the file path.

Make sure the path you are using in ${file(&lt;this-path-here&gt;):provider}

It is pointing to the correct file.

The following works:

  • File: serverless.yml
service: serverless-framework-include-files
frameworkVersion: &quot;3&quot;

provider: ${file(config/provider.yml):provider}

functions:
  function1:
    handler: index.handler
  • File: config/provider.yml
provider:
  name: aws
  runtime: nodejs18.x

Alternatively, you can remove the :provider from the ${file(...):provider}:

The following works:

  • File: serverless2.yml
service: serverless-framework-include-files
frameworkVersion: &quot;3&quot;

provider: ${file(config/provider2.yml)}

functions:
  function1:
    handler: index.handler
  • File: config/provider2.yml
name: aws
runtime: nodejs18.x

For resources:, the same applies.

This works:

  • File: serverless.yml
resources:
 - ${file(resources/s3-bucket.yml)}
 - ${file(resources/dynamodb-table.yml)}
  • File: resources/s3-bucket.yml
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-bucket
      AccessControl: PublicRead
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: error.html

Alternatively, you can do:

  • File serverless2.yml
resources:
  Resources:
    MyBucket: ${file(resources/s3-bucket2.yml)}
    MyTable: ${file(resources/dynamodb-table2.yml)}
  • File resources/dynamodb-table2.yml
Type: AWS::DynamoDB::Table
Properties:
  TableName: ${self:service}
  AttributeDefinitions:
    - AttributeName: id
      AttributeType: S
  KeySchema:
    - AttributeName: id
      KeyType: HASH
  ProvisionedThroughput:
    ReadCapacityUnits: 1
    WriteCapacityUnits: 1

You can check the GitHub code for a working example:

huangapple
  • 本文由 发表于 2023年2月14日 00:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定