Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cnnranderson opened this issue Apr 20, 2021 · 7 comments
Open

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

cnnranderson opened this issue Apr 20, 2021 · 7 comments

Comments

@cnnranderson
Copy link

cnnranderson commented Apr 20, 2021

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
Copy link
Owner

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

@whisller
Copy link

whisller commented Aug 2, 2021

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

@chris-feist
Copy link
Owner

@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
Copy link

whisller commented Aug 4, 2021

@chris-feist thank you for the update!

@giacomorebonato
Copy link

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

@chris-feist
Copy link
Owner

chris-feist commented Aug 6, 2021

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
Copy link
Owner

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

serverless/serverless#9311

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants