Serverless offline CUSTOM: using external file AND internally added variables?

894 Views Asked by At

I am having a weird problem where I need to use Serverless "custom:" variables read both from an external file and internally from the serverless.yml file.

Something like this:

custom: ${file(../config.yml)}
   dynamodb:
      stages:
        -local

..except this doesn't work. (getting bad indendation of a mapping entry error) I'm not sure if that's possible and how do you do. Please help :)

The reason is dynamodb local serverless plugin won't work if it's config is set in the exteranl file. But we use the external file config in our project and we don't wanna change that.

So I need to have the dynamodb config separate in the serverless.yml file just not sure the proper way to do it.

Please someone help :) Thanks

1

There are 1 best solutions below

0
On

You will either have to put all your vars in the external file or import each var from the custom file one at the time as {file(../config.yml):foo}

However... you can also use js instead of yml/json and create a serverless.js file instead allowing you to build your file programically if you need more power. I have fairly complex needs for my stuff and have about 10 yml files for all different services. For my offline sls I need to add extra stuff, modify some other so I just read the yaml files using node, parse them into json and build what I need then just export that.

Here's an example of loading multiple configs and exporting a merged one:

import { readFileSync } from 'fs'
import yaml from 'yaml-cfn'
import merge from 'deepmerge'

const { yamlParse } = yaml

const root = './' // wherever the config reside

// List of yml to read
const files = [
  'lambdas',
  'databases',
  'whatever'
]

// A function to load them all 
const getConfigs = () =>
  files.map((file) =>
    yamlParse(readFileSync(resolve(root, `${file}.yml`), 'utf8'))
  )

// A function to merge together - you would want to adjust this - this uses deepmerge package which has options
const mergeConfigs = (configs) => merge.all(configs)

// No load and merge into one
const combined = mergeConfigs(getConfigs())

// Do stuff... maybe add some vars just for offline for ex

// Export - sls will pick that up
export default combined