Search code examples
serverless-framework

Setting the env using a .env file in Serverless


Using the Serverless framework, I want to be able to change the AWS region from an envrionment variable.

provider:
  name: aws
  region: ${env:AWS_REGION}

Then, AWS_REGION can be set to eu-west-2.

However, I want to have that set in a .env file:

AWS_REGION=eu-west-2

And then have that .env read by Serverless.

There are many topics about setting variables in the serverless.yml file, and exporting them from that file, but I want to put them into the file.


Solution

  • Out of the box serverless doesn't parse .env, that part belongs to you.

    I see three options for you:

    1. Use the serverless-dotenv-plugin.

    2. Write a script that exports .env vars to your local environment before you run serverless.

    3. Run serverless in docker-compose which is .env aware -- I use this in combination with Makefile, even in a CI/CD context.