15

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.

3 Answers 3

22

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.

Sign up to request clarification or add additional context in comments.

4 Comments

PSA: This will fail horribly if you have an empty .yml file included this way (where empty means having the "Resources:" header and nothing else.
"horribly" is right. It took me hours to figure this out - kept having instance of Fn::GetAtt references undefined resource errors for no apparent reason with no errors shown - a file with just "Resources" (I had commented stuff out) will silently corrupt your setup. Thank you @Techrocket9
The mixed approach is possible, but requires precise (and seemingly inconsistent) indentation (needing 4 spaces where 2 is expected). The error messages that result of incorrect indentation will not be helpful either.
@Techrocket9: The horrible failing part can be avoided by using a fallback 'empty string'; example - ${file(resources/NonExistingFile-cf-resources.yml), ''}
17

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:
    ...

2 Comments

@DeusProx first-cf-resources.yml should also start from Resources:
I always do "resources: Resources: ..." and reference them like "- ${file(./resourcefile.yml):resources}", but it only works when just using references.
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.