chris-feist / serverless-plugin-composed-vars

A Serverless plugin that composes custom and environment variables based on the deployment stage
MIT License
8 stars 3 forks source link

Values are no longer properly resolving based on input stage #17

Open cnnranderson opened 3 years ago

cnnranderson commented 3 years ago

There were apparently breaking changes in the way Serverless loads environment variables recently and this plugin no longer properly resolves the values expected for the stage.

See a similar thread here from serverless-dotenv plugin: https://www.npmjs.com/package/serverless-dotenv-plugin serverless>=3.0.0 introduces changes that significantly impacts this plugin. See the discussion thread or the FAQ below for details on the impact of how env vars are loaded with serverless>=2.26.0 and serverless>=3.0.0.

chris-feist commented 3 years ago

@cnnranderson Thanks for reporting the issue. I'll dig into it and see if I can find a resolution

whisller commented 3 years ago

Hey @chris-feist , any update on the topic?

chris-feist commented 3 years ago

@whisller I looked into it and it's going to take a serious effort to make this compatible with the latest serverless. Serverless now supports dotenv type environment variable files, which is a good workaround in the meantime. The major downside is that it doesn't support merging of those files. Here are the docs:

https://www.serverless.com/framework/docs/environment-variables/

whisller commented 3 years ago

@chris-feist thank you for the update!

giacomorebonato commented 3 years ago

Could you provide an example of the workaround to upgrade a project that has X stages and X variables files please?

chris-feist commented 3 years ago

You have to use the stage-specific .env files.

# .env - This is the default when no staging .env file specified

DATABASE_NAME=dev-db
DATABASE_SERVICE=mongodb
# .env.prod - This is the prod stage .env file

DATABASE_NAME=prod-db
# Note that we have to define all env vars since merging files/values is not supported by the serverless implementation
DATABASE_SERVICE=mongodb

Then you can reference the environment variables in any of your configuration files (serverless.yml, variables.yml, etc) using ${env:DATABASE_NAME}. If you want to use them during runtime, you need to declare the runtime name and reference the config value:

  environment:
    DATABASE_NAME: ${env:DATABASE_NAME}

Any environment variables for the process will override the .env values. So you could run an npm script like DATABASE_NAME=temp-db serverless deploy -s dev or set different values in your CI builds.

You can still use a separate variables file like my repo sets up automatically, but you would have to reference the environment variables:

# serverless.yml

custom: ${file(./variables.yml)}
# variables.yml

dbConnection: "https://${env:DATABASE_SERVICE}.com/${env:DATABASE_NAME}"
chris-feist commented 3 years ago

It looks like it may be easier to reintroduce compatibility once the serverless extensions work is finished 🤞

https://github.com/serverless/serverless/issues/9311