saketh
saketh

Reputation: 813

How to join or merge provider.patterns array from multiple yml files in serverless.yml and also how to set enabled flag on scheduled events

I have serverless_common.yml file for all shared things across all my lambdas and I have serverless.yml in my individual service when I try to import package.patterns in to my individual service yml file I get following error when I do sls print

Serverless Error ----------------------------------------
 
  Configuration error: 
       at 'functions.app.events[0]': unrecognized property 'enabled'
       at 'package.patterns[0]': should be string

Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.16.1
     Framework Version:         2.46.0 (local)
     Plugin Version:            5.4.0
     SDK Version:               4.2.3
     Components Version:        3.12.0
#serverless_common.yml
package:
  patterns:
  - '!target/**'
  - '!tests/**'
  - '!test/**'
  - '!tools/**'
  - '!README.md'
  - '!node_modules/.bin/**'
  - '!serverless/**'
  - '!.*'
#service1/serverless.yml
package:
  individually: true
  patterns: 
    - something-specific-to-service1
    - ${file(../serverless_common.yml):package.patterns}
functions:
  app:
    handler: index.handler
    name: service1
    events:
      - schedule: cron(0 09 * * ? *)
        enabled: false
serverless_common.yml        
/service1
  |- package.json     
  |- index.js
  |- serverless.yml   
/service2
  |- package.json    
  |- index.js
  |- serverless.yml   

Upvotes: 2

Views: 859

Answers (1)

Aaron Stuyvenberg
Aaron Stuyvenberg

Reputation: 3777

The errors are warning you that you've got two indentation issues. The first is in your serverless_common.yml file. The array items should be indented one place further:

#serverless_common.yml
package:
  patterns:
    - '!target/**'
    - '!tests/**'
    - '!test/**'
    - '!tools/**'
    - '!README.md'
    - '!node_modules/.bin/**'
    - '!serverless/**'
    - '!.*'

The second is in your cron expression. The one-line syntax is only if you're not using other arguments. Since you want to pass enabled: false, you'll need to use the multiline syntax:

#service1/serverless.yml
functions:
  app:
    handler: index.handler
    name: service1
    events:
      - schedule:
          rate: cron(0 09 * * ? *)
          enabled: false

Unfortunately, the syntax you've chosen won't merge the two arrays. You'll have to reference each item in your array individually, or rewrite your serverless.yml into a serverless.js file, which allows you to be more programmatic.

package:
  individually: true
  patterns: 
    - something-specific-to-service1
    - ${file(../serverless_common.yml):package.patterns.foo}
    - ${file(../serverless_common.yml):package.patterns.bar}
    # ... etc

Upvotes: 1

Related Questions