![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
serverless-dotenv-plugin
Advanced tools
Preload Environment Variables with Dotenv into Serverless Edit
The serverless-dotenv-plugin is a Serverless Framework plugin that automatically loads environment variables from a .env file into your serverless project. This allows you to manage environment-specific configurations and secrets in a centralized and secure manner.
Load Environment Variables
This feature allows you to load environment variables from a .env file into your serverless project. The code sample demonstrates how to configure the serverless-dotenv-plugin in the serverless.yml file and access an environment variable in a function.
module.exports = {
service: 'my-service',
plugins: ['serverless-dotenv-plugin'],
provider: {
name: 'aws',
runtime: 'nodejs14.x'
},
functions: {
hello: {
handler: 'handler.hello',
environment: {
MY_ENV_VAR: process.env.MY_ENV_VAR
}
}
}
};
Custom .env File Path
This feature allows you to specify a custom path for your .env file. The code sample shows how to configure the plugin to load environment variables from a .env file located in a custom directory.
module.exports = {
service: 'my-service',
plugins: ['serverless-dotenv-plugin'],
custom: {
dotenv: {
path: './config/.env'
}
},
provider: {
name: 'aws',
runtime: 'nodejs14.x'
},
functions: {
hello: {
handler: 'handler.hello',
environment: {
MY_ENV_VAR: process.env.MY_ENV_VAR
}
}
}
};
Multiple Environment Files
This feature allows you to use different .env files for different environments. The code sample demonstrates how to configure the plugin to load environment variables from different .env files based on the environment.
module.exports = {
service: 'my-service',
plugins: ['serverless-dotenv-plugin'],
custom: {
dotenv: {
path: {
development: './config/.env.development',
production: './config/.env.production'
}
}
},
provider: {
name: 'aws',
runtime: 'nodejs14.x'
},
functions: {
hello: {
handler: 'handler.hello',
environment: {
MY_ENV_VAR: process.env.MY_ENV_VAR
}
}
}
};
dotenv is a popular package for loading environment variables from a .env file into process.env. It is widely used in Node.js applications but does not have built-in support for the Serverless Framework. You would need to manually integrate it into your serverless project.
serverless-secrets-plugin is another Serverless Framework plugin that helps manage secrets and environment variables. It integrates with AWS Secrets Manager to securely store and retrieve secrets. Unlike serverless-dotenv-plugin, it focuses on using AWS Secrets Manager rather than .env files.
serverless-env-generator is a plugin that generates environment variables for your serverless project based on a configuration file. It provides more flexibility in generating environment variables but requires additional configuration compared to serverless-dotenv-plugin.
Preload Environment Variables Into Serverless
Use this plugin if you have variables stored in a .env
file that you want loaded into your serverless yaml config. This will allow you to reference them as ${env:VAR_NAME}
inside your config and it will load them into your lambdas.
First, install the plugin:
> npm i -D serverless-dotenv-plugin
Next, add the plugin to your serverless config file:
service: myService
plugins:
- serverless-dotenv-plugin
...
Now, just like you would using dotenv in any other JS application, create your .env
file in the root of your app:
DYANMODB_TABLE=myTable
AWS_REGION=us-west-1
AUTH0_CLIENT_ID=abc12345
AUTH0_CLIENT_SECRET=12345xyz
By default, the dotenv package will look for your .env file in the same folder where you run the command, but this can be customized by setting the path
option. Also, be default, ALL env vars found in your file will be injected into your lambda functions. If you do not want all of them to be injected into your lambda functions, you can whitelist them with the include
option.
custom:
dotenv:
path: ../../.env
include:
- AUTH0_CLIENT_ID
- AUTH0_CLIENT_SECRET
The path
option can also be used in combination with serverless variables.
custom:
dotenv:
path: .env-${self:provider.stage}
Once loaded, you can now access the vars using the standard method for accessing ENV vars in serverless:
...
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: ${env:AWS_REGION}
...
Again, remember that when you deploy your service, the plugin with inject these environment vars into any lambda functions you have and will therefore allow you to reference them as process.env.AUTH0_CLIENT_ID
(Nodejs example).
FAQs
Preload environment variables with dotenv into serverless.
We found that serverless-dotenv-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.