Search code examples
serverlessserverless-framework

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.


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

provider.yml

name: ...
...

serverless.yml

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

Solution

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

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

    Make sure the path you are using in ${file(<this-path-here>):provider}

    It is pointing to the correct file.

    The following works:

    • File: serverless.yml
    service: serverless-framework-include-files
    frameworkVersion: "3"
    
    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: "3"
    
    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: