AWS Parameter Store + Nodejs

XD
2 min readFeb 14, 2023

--

We all have faced this sometimes when it comes to managing environment variables in the backend especially when it is microservice architecture

Let’s hope we are using dotenv to manage a .env file locally, where we can access variables using process.env.ENV_VAR in project.

The challenge we are going to solve is to put these values in the store (AWS parameter store) and use it in multiple microservices, so when we need to update a specific value it gets reflected in all services

1. Store the key/value pair in the parameter store

First, we need to store the key & value in the parameter store, we will follow the official AWS hierarchy. Let’s store a dummy value in /development/webapp/parameter1

2. Integrating dotenv package in nodejs

Now, in order to use a .env file in nodejs we need to install dotenv npm package, run npm i dotenv

Also, add require('dotenv').config() on the entry file, eg: index.js so that we can use values from .env by process.env.<KEY>

Make sure the .env file is in the root level of the project strcture

|---
|--- node_modules
|--- index.js
|--- .env <---------------------- add file here
|--- scripts
|--- getEnvVars.sh
|--- package.json

Now, you can add any value say paramter1=dummyValue in .env file, and get that value anywhere using process.env.parameter1

# .env file
paramter1=dummyValue

and, index.js

// index.js file
...
console.log(process.env.parameter1)
...

3. Creating a .env file with values stored in the AWS parameter store

Now, we know that we can use .env file to access our configs in nodejs, but we need to inject the values stored in the AWS parameter store, so we can update at one place and get reflected in all services

In order to do this, we need to write a bash script that will create a .env file with the current values stored in AWS

Since this script will be using awscli & jq, we need to install these in our machine

Create a file under scripts folder called getEnvVars.sh

|---
|--- node_modules
|--- index.js
|--- .env
|--- scripts
|--- getEnvVars.sh <---------------------- add file here
|--- package.json

and add this code to it

#!/bin/bash

region=${1:-us-east-1} #<------------- change region here

if [ -n "$region" ]; then
aws ssm get-parameters-by-path --region "$region" --path "/development/webapp" --with-decryption | jq -r '.Parameters[]|((.Name|capture(".*/(?<a>.*)").a+"=")+.Value)' > ./.env
else
echo "Input missing. Exiting..."
fi

All set, now execute this script to create/update .env file with the values stored in the AWS parameter store, run sh scripts/getEnvVars.sh

Voila 🎉

--

--

XD
XD

Written by XD

Code by day, hack the system by night. Navigating the cloud like a neon-lit alleyway. APIs, bugs, and glitches – I fix what the future breaks.

No responses yet