npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flypapertech/awsenv

v2.0.0

Published

Loads environment from aws secrets manager

Downloads

730

Readme

awsenv

Awsenv is a module that loads environment variables from AWS Secrets Manager into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.

Awsenv draws heavily on the concepts from the great package dotenv. In fact even this readme was created to read similarly :)

NPM version

Install

# with npm
npm install @flypapertech/awsenv

# or with Yarn
yarn add @flypapertech/awsenv

Requirements

Node.js version >= 10.0

Usage

awsenv can be used anywhere! Lambda functions, EC2 instances, locally, EKS clusters, etc.

If you haven't already, create a secret in AWS Secrets Manager. A single secret can contain multiple key value pairs. Take note of the name of the secret.

Wherever you plan to use awsenv you must make sure that the secrets you are trying to load are accessible to the running environment/user. For more information check out the AWS Secrets Manager access docs.

Preload (preferred approach)

You can use the --require (-r) command line option to preload awsenv. By doing this, you do not need to require and load awsenv in your application code.

In order to use this method you must have the AWSENV_SECRET_NAME environment variable set in your environment to tell awsenv which secrets to load.

$ AWSENV_SECRET_NAME=someSecretName node -r @flypapertech/awsenv/config your_script.js

You can optionally set AWS_REGION to tell awsenv where to get the secrets from. If you do not provide AWS_REGION awsenv will fall back to the defaults as outlined in the AWS SDK docs

$ AWS_REGION=us-east-1 AWSENV_SECRET_NAME=someSecretName node -r @flypapertech/awsenv/config your_script.js

Require

As early as possible in your application, require and configure awsenv.

require('@flypapertech/awsenv').config('someSecretName', 'optionalAWSRegion')

process.env now has the keys and values you defined in your AWS Secret.

const db = require('db')
db.connect({
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS
})

FAQ

Should I use this in development or production

awsenv was created to be used in a production environment that has access to your AWS Secrets. There is nothing stopping you from using it in development mode instead of a .env file as long as your development environment is set up with an IAM policy that allows access to the secrets.

How do I use awsenv with import?

ES2015 and beyond offers modules that allow you to export any top-level function, class, var, let, or const.

When you run a module containing an import declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.

ES6 In Depth: Modules

You must run awsenv.config('someSecretName') before referencing any environment variables. Here's an example of problematic code:

errorReporter.js:

import { Client } from 'best-error-reporting-service'

export const client = new Client(process.env.BEST_API_KEY)

index.js:

import * as awsenv from '@flypapertech/awsenv/config'
import errorReporter from './errorReporter'

awsenv.config('someSecretName', 'optionalAWSRegion')
errorReporter.client.report(new Error('faq example'))

client will not be configured correctly because it was constructed before awsenv.config() was executed. There are (at least) 3 ways to make this work.

  1. Preload awsenv: AWSENV_SECRET_NAME=someSecretName node -r @flypapertech/awsenv/config index.js (Note: you do not need to import awsenv with this approach)
  2. Import @flypapertech/awsenv/config instead of @flypapertech/awsenv (Note: you do not need to call awsenv.config() and must pass options via environment variables with this approach)
  3. Create a separate file that will execute config first.