Find code examples for this guide on GitHub.
Initial setup
// index.js
exports.handler = async (event) => {
return {
statusCode: 200,
body: 'Hello World'
}
}
Add dotenvx
$ npm install @dotenvx/dotenvx --save
// index.js
require('@dotenvx/dotenvx').config()
exports.handler = async (event) => {
return {
statusCode: 200,
body: `Hello ${process.env.HELLO}`
}
}
Add .env file
# .env
HELLO="World"
$ dotenvx encrypt
Zip it up
Zip everything—making sure to ignore .env.keys.
zip -r function.zip . -x ".env.keys"
Upload to AWS Lambda
Create a function, select your runtime and x86_64, then upload function.zip.
Click Test and you will see encrypted ciphertext in the body until the private key is set.
Set DOTENV_PRIVATE_KEY
Add an environment variable DOTENV_PRIVATE_KEY with the value from your .env.keys file. Test again—you should see Hello World.
Distributing your lambdas is now safer—they only contain encrypted values.