7

I am using serverless framework to deploy react web application. I've set up a CI/CD pipeline for deployment. when serverless template gets executed I am receiving several deprecation warnings.

Serverless: Deprecation warning: Detected ".env" files. In the next major release variables from ".env" files will be automatically loaded into the serverless build process. Set "useDotenv: true" to adopt that behavior now.
            More Info: https://www.serverless.com/framework/docs/deprecations/#LOAD_VARIABLES_FROM_ENV_FILES
Serverless: Deprecation warning: Variables resolver reports following resolution errors:
              - Cannot resolve variable at "provider.profile": Value not found at "env" source
   From a next major this will be communicated with a thrown error.
            Set "variablesResolutionMode: 20210326" in your service config, to adapt to new behavior now
            More Info: https://www.serverless.com/framework/docs/deprecations/#NEW_VARIABLES_RESOLVER

The way I understand these warnings, it is trying to load environment variables from .env file from the serverless directory. But I've stored my variables from other files(abc.config) and loading them to the serverless template through that file and not from the .env file. and for this reason I am getting these warnings.

Also I've used serverless-dotenv-plugin to use environment variables in .env file for local deployment and that's why the .env file has to be there in the serverless directory. But for now loading variables from abc.config file is working fine and in the future, I want to load variables from the same file(abc.config). But in the future, if I use the same approach for environment variables, that will throw an error instead of giving a warning.

Questions 1: I am not sure how would I tackle this issue. because in the upcoming serverless version release this will throw an error.

Question 2: What if I install a specific serverless version. for example; npm install -g serverless@2.45.0 in that case, in the future will I still get these deprecation warnings? In theory, I'll be still using the old serverless version and this version supports loading variables from other files. So I should not be getting these warnings. I can be wrong. but what should be the ideal approach to resolve these warnings ahead of time?

Any help would be greatly appreciated. Thank you.

Auto geek
  • 464
  • 1
  • 6
  • 21

1 Answers1

11

You should set the deprecation variables in your serverless.yml file and verify that serverless deploy is successful and the framework interpolates your variables as you intend.

The warning messages explain the process. Simply add the rules to your serverless.yml file:

useDotenv: true
variablesResolutionMode: 20210326

The second warning message will be an error, provider.profile wasn't able to be resolved. You can solve this with a conditional, ie:

${provider.profile, 'default'}

Or you can ensure that provider.profile is always set. It's not possible to help further without seeing the serverless.yml file.

Aaron Stuyvenberg
  • 3,437
  • 1
  • 9
  • 21
  • Thank you for responding @Aaron Stuyvenberg. what do the setting deprecation variables mean? and how do I set it in my serverless? can you guild me to the correct example or document that explains this? Thank you. – Auto geek Jun 11 '21 at 11:11
  • You can simply set them liked you'd set anything in your `serverless.yml` file. Copying what I've written directly will convert the framework to use the new v3 features. You can read more in the documentation: https://www.serverless.com/framework/docs/deprecations/ – Aaron Stuyvenberg Jun 15 '21 at 11:53
  • `useDotenv: true` resolved the issue for me. More documentation about loading `.env` variables here https://www.serverless.com/plugins/serverless-dotenv-plugin – Owen May 18 '22 at 03:39