Jesuspc
Jesuspc

Reputation: 1734

Declaring resources in multiple files in Serverless framework

Is there any way to split the resource definitions in serverless framework into multiple files? Something like:

resources:
  - ${resources/base.yml}
  - ${resources/foo.yml}

I have been trying multiple combinations but I keep getting errors about references not being found.

Upvotes: 15

Views: 10913

Answers (3)

Niv-Mizzet
Niv-Mizzet

Reputation: 371

I can't comment but I'd like to extend Jesuspc's answer.

There is a way to achieve that 'mixed' approach, in serverless.yml:

resources:
  - ${file(resources/first-cf-resources.yml)}
  - ${file(resources/second-cf-resources.yml)}
  - Resources:
      SomeResource:
        Type: ...

In this case files first-cf-resources.yml and second-cf-resources.yml must have the next structure:

Resources:
  SomeResourceA:
    ...
  AnotherResourceB:
    ...

Upvotes: 17

Jesuspc
Jesuspc

Reputation: 1734

Even though dashmug's answer is correct, I found that the way I was trying to make it work was quite close to a valid solution too. As explained in this github comment it is possible to reference other files in the resources section:

resources:
   - ${file(resources/first-cf-resources.yml)}
   - ${file(resources/second-cf-resources.yml)}

Provided that each those files defines a "Resources" key of its own, like:

---
Resources:
  MyCFResource:
    Type:.....

What I didn't manage is to have a mixed approach such as:

resources:
  - ${file(resources/first-cf-resources.yml)}
  - ${file(resources/second-cf-resources.yml)}
  SomeResource:
    Type: ...

So I just have a resources/base.yml for that instead.

Upvotes: 22

Noel Llevares
Noel Llevares

Reputation: 16037

Take note that the resources property has to be an object containing a Resources property, NOT an array of resources like what you wanted in your code snippet.

So, to use external file references, you can do something like...

resources
    Resources:
        UsersTable: ${file(../resources/base.yml):UsersTable}
        FooTable: ${file(../resources/foo.yml):FooTable}

Reference: Reference variables in other files

Upvotes: 4

Related Questions