Logo

dev-resources.site

for different kinds of informations.

Using dotenv with the Serverless Framework

Published at
4/27/2021
Categories
dotenv
serverlessframework
Author
neverendingqs
Categories
2 categories in total
dotenv
open
serverlessframework
open
Author
13 person written this
neverendingqs
open
Using dotenv with the Serverless Framework

In my previous post, I advised that the serverless-dotenv-plugin will no longer be able to load environment variables in time to resolve env variables in your service files (e.g. serverless.yml). In this post, I want to go into details on how you can use dotenv without the plugin.

With the Serverless Framework, you can use the file variable to dynamically evaluate variables by executing JavaScript code. Leveraging this feature, you can get the Serverless Framework to run any arbitrary code, such as calling dotenv to load environment variables! See how MY_ENV_VAR is loaded into the framework below:

.env.local:

MY_ENV_VAR=loaded-by-dotenv
Enter fullscreen mode Exit fullscreen mode

configs.js:

const dotenv = require('dotenv');

module.exports = async ({ options, resolveConfigurationProperty }) => {
  // Load env vars into Serverless environment
  // You can do more complicated env var resolution with dotenv here
  const envVars = dotenv.config({ path: '.env.local' }).parsed;
  return envVars;
};
Enter fullscreen mode Exit fullscreen mode

serverless.yml:

service: my-service
# Required to use the new variables engine
# https://www.serverless.com/framework/docs/providers/aws/guide/variables#exporting-a-function
variablesResolutionMode: 20210219

custom:
  dotenvVars: ${file(configs.js)}

functions:
  hello:
    handler: src/hello.handler
    environment:
      MY_ENV_VAR: ${env:MY_ENV_VAR}
Enter fullscreen mode Exit fullscreen mode

When the Serverless Framework sees ${file(configs.js)}, the variables resolver will call the function exported in configs.js. The JavaScript code there includes a call to dotenv.config(), which will load all environment variables defined in .env.local. Because this loads the environment variables into the process, plugins will also have access to them.

With a for loop, you can load multiple dotenv files (e.g. .env and .env.local), or with options.stage, you can load files based on the value of the --stage parameter. You can also load any dotenv plugins you may be interested in using, such as dotenv-expand. Because you are able to execute arbitrary JavaScript code, you are only limited by what you can do in JavaScript!

You can find a full example is available at neverendingqs/serverless-dotenv-example. Got questions or want to see more content like this? Check out neverendingqs/ask-me-anything!

serverlessframework Article's
30 articles in total
Favicon
Build a highly scalable Serverless CRUD Microservice with AWS Lambda and the Serverless Framework
Favicon
Building Scalable Event Processing with Fan-out Pattern using the Serverless Framework
Favicon
Importing CloudFormation Resources to help fix deployments to Production
Favicon
How to add Amazon Cognito Auth to a Web App (part 4)
Favicon
Setting up for Serverless Development with AWS
Favicon
Basic Integration Testing with Serverless Framework
Favicon
Building an API with Ruby and the Serverless Framework
Favicon
How to use multiple runtimes in a single serverless microservice
Favicon
ShreyTheCray Interview: Serverless Framework & Cloud Explained
Favicon
Introducing multi-service deployments via Serverless Framework Compose
Favicon
Simple Serverless Scheduler
Favicon
Serverless (FaaS) vs. Containers — when to pick which?
Favicon
Provisioned Concurrency: What it is and how to use it with the Serverless Framework
Favicon
AWS Lambda Function URLs with Serverless Framework
Favicon
The ABCs of IAM: Managing permissions with Serverless
Favicon
AWS Lambda Destination Support
Favicon
Managing Stages and Environments in Serverless Framework
Favicon
Using API Gateway WebSockets with the Serverless Framework
Favicon
How to Create a REST API with Azure Functions and the Serverless Framework — Part 1
Favicon
How to deploy multiple micro-services under one API domain with Serverless
Favicon
How to Test Serverless Applications
Favicon
DynamoDB On-Demand: When, why and how to use it in your serverless applications
Favicon
The definitive guide to using Terraform with the Serverless Framework
Favicon
Serverless Framework example for Golang and Lambda
Favicon
Using SQS with AWS Lambda and Serverless
Favicon
Container Image Support for AWS Lambda
Favicon
Major changes to serverless-dotenv-plugin
Favicon
Using dotenv with the Serverless Framework
Favicon
API Gateway WebSocket APIs with the Serverless Framework
Favicon
Takeaways from using AppSync

Featured ones: