KrysKruk / serverless-import-config-plugin

Split your serverless.yaml config file into smaller modules and import them.
MIT License
10 stars 6 forks source link

Overwriting imported values #2

Open PatrykMilewski opened 4 years ago

PatrykMilewski commented 4 years ago

Hey!

I was testing this plugin and it works great, but I found one use case, that is not supported.

I would like to import common base serverless.yml file in each stack and in some cases, I may want to overwrite some of the imported keys values. Example: /serverless.yml

provider:
  aws: aws
  runtime: nodejs10.x
  memory: 128

/childStack/serverless.yml

custom:
  import: ../serverless.yml

provider:
  memory: 256

Is this possible to achieve this using this plugin?

KrysKruk commented 4 years ago

Hi @PatrykMilewski !

All already existing parameters will not be overwritten, so your example should work just fine and the final config would be:

provider:
  aws: aws
  runtime: nodejs10.x
  memory: 256

Let me know if that works for you. If not, that would be a bug.

I guess README should be more precise, sorry for that.

PatrykMilewski commented 4 years ago

Thank you for response @KrysKruk

I can't get it to work, there is another example with outputs:

/tmp/serverless.yml

provider:
  name: aws
  stackName: ${self:custom.baseName}
  runtime: nodejs12.x
  memory: 128

/tmp/child/serverless.yml

service: example-service

plugins:
  - serverless-import-config-plugin

custom:
  baseName: baseName-example
  import: ../serverless.yml

provider:
  memorySize: 256

Then trying to check, if it works:

cd /tmp/child/
sls print

It outputs:

Serverless: Importing ../serverless.yml
service: example-service
plugins:
  - serverless-import-config-plugin
custom:
  baseName: baseName-example
  import: ../serverless.yml
provider:
  memorySize: 256

Is this expected or I'm doing something wrong?

KrysKruk commented 4 years ago

@PatrykMilewski I think the issue is with sls print and not the plugin itself.

sls print as coded here https://github.com/serverless/serverless/blob/master/lib/plugins/print/print.js#L100 loads config file and perform some modifications on it, but unfortunately it doesn't apply other plugins.

The log Serverless: Importing ../serverless.yml is really misleading here, but is refers to the phase when Serverless loads all plugins.

Let me know if the generated config contains informations you need. You can run sls package and then check the generated json file.

PatrykMilewski commented 4 years ago

So I was experimenting a little bit and it looks like, you must provide provider: name: aws to make it work, otherwise Serverless won't generate json files for CloudFormation.

After adding it to child stack, it worked, but still values for provider may cause problems, for example: - - memory is overwritten correctly, but if not provided, the value from parent stack is ignored and replaced with default (1024) value

And probably there are much more edge cases for provider.

@KrysKruk Do you think it would be possible to hook with your plugin to different Serverless phase? Having sls print working for checking if imports works would be extremely helpful.

I will check if it works correctly for custom key and if other plugins still work after importing then let you know, what are the results.

PatrykMilewski commented 4 years ago

memory is overwritten correctly, but if not provided, the value from parent stack is ignored and replaced with default (1024) value

Actually it doesn't, I had typo in memorySize tag name. It works correctly.

The only problems that I've found are with provider: name and provider: deploymentBucket. There may be a lot more problems about import provider tag.

Maybe it's worth to check and mention those problems?