Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 311
Zoe Edwards Avatar asked Apr 19 '26 19:04

Zoe Edwards


1 Answers

Serverless now supports .env files without the need for a plugin

  1. Add useDotenv: true to your serverless.yml file. The variable should be at the root level, same as service: ...

  2. Add a .env file at the root of your project and serverless will load the variables.

Example:

// .env
MY_TABLE=A_TABLE_NAME
like image 115
Attaque Avatar answered Apr 24 '26 15:04

Attaque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!