2 minutes reading time

On this page

AWS Lambda with Node.js - Integrations

2 minutes reading time

AWS Lambda Node.js

AWS Lambda with Node.js

Learn how to configure AWS Lambda with Dotenv Vault in a simple Node.js web app. This tutorial assumes you are already familiar with .env files and know how to sync them.

Initial setup

Create a buildspec.yml file in your root folder to set your AWS Lambda project settings. Add relevant phase and command settings to your project to define the tasks that need to be performed when building. Here’s a basic setup for your buildspec.yml:

// buildspec.yml
version: 0.2
      - npm install
      - npm run build
      - rm -rf ./__build__
      - npm prune --production
      - aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.yml
  type: zip
    - template-export.yml

Once ready, move forward by creating a template.yml file also in your project root to set the SAM settings like so:

// template.yml
AWSTemplateFormatVersion: 2010-09-09
Description: >-
  Start from scratch starter project
Transform: AWS::Serverless-2016-10-31
    PermissionsBoundary: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/${AppId}-${AWS::Region}-PermissionsBoundary'
    Type: String
    Type: AWS::Serverless::Function
      CodeUri: ./
      Handler: src/handlers/hello-from-lambda.helloFromLambdaHandler
      Runtime: nodejs14.x
      MemorySize: 128
      Timeout: 60
      Description: A Lambda function that returns a static string.
        - AWSLambdaBasicExecutionRole

Package installation

With the AWS settings taken care of, you can proceed by installing the dotenv package with npm.

npm install dotenv --save

Create an index.js file in your root folder then reference the dotenv package as early in the code as possible to avoid possible conflicts.

// index.js
console.log(process.env) // for debugging purposes. remove when ready.

Build the Vault

Confirm you are logged in and your Vault is synced locally by running npx dotenv-vault pull ci. Once ready, proceed by building your Vault with npx dotenv-vault build.

npx dotenv-vault build

Once Vault has finished building, it will provide you with access to its decryption keys, which you can use to interact with protected environment variables with ease.

To retrieve a key, just input npx dotenv-vault keys, followed by your preferred environment, like ci, for example. You can do the same with other environments such as development and production.

The outcome of this will be a long URL being returned. You will immediately recognize it as it always starts with dotenv://:key and ends in ?environment= with the environment you have chosen.

npx dotenv-vault keys ci
remote:   Listing .env.vault decryption keys... done

dotenv://:[email protected]/vault/.env.vault?environment=ci

Set deployment

With the decryption key safely in your possession, it is time for you to head over to your AWS Lambda project within the CodeBuild/Build projects path.

From there seek the Build details tab and its Environment section in particular. Click the Edit button at the top right corner of the section and find the Additional configuration accordion.

Underneath the Compute radio selection, you will find the Environment variables section. Put DOTENV_KEY as the key and save the decryption key you obtained earlier for the value field.

Lastly, pick Plaintext as type and confirm your settings by pressing the Update environment button.

Commit and push

That’s it!

Commit those changes safely to code and deploy to AWS Lambda.

When the build runs, it will recognize the DOTENV_KEY, decrypt the .env.vault file, and load the ci environment variables to ENV.

If a DOTENV_KEY is not set when developing on local machine, for example, it will fall back to standard Dotenv functionality.

You’ll know things worked correctly when you see 'Loading .env from encrypted .env.vault' in your AWS Lambda logs.