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

sreda

v0.0.3-dev

Published

Read your env variables from SSM

Downloads

31

Readme

sreda Codeship Status for igorkosta/sreda npm version

What would you use sreda for?

There is a very nice article by Yan Cui that explains very well, why you should use AWS SSM/KMS and also why you should build your own client to do so.

There is a couple of reasons for your own implementation:

  • caching
  • hot-swapping of configurations

You can find more about it in Yan's article on medium.

sreda takes the inspiration from the Yan's medium article and this gist from Aaron Ackerman, which was probably also inspired by Yan Cui and packs it all into an npm package.

How to use?

sreda exposes two functions:

  • load - loads the configuration
  • keys - returns a json object with your key/value pairs

Load the configuration

If you want to load the configuration for certain keys from SSM, you have to:

const { read } = require('sreda')
const AWS = require('aws-sdk')
const ssm = new AWS.SSM({
  region: 'us-west-1'
})

const config = read(
  ssm,
  ['foo', 'bar'],
  30000) // cache configuration for 30 seconds

const anotherConfig = read(
  ssm,
  ['fizz', 'buzz']) // default cache expiration is 3 minutes

exports.handler = async (event, context, callback) => {
  let keys = {
    foo: await config.keys.foo,
    bar: await config.keys.bar,
    fizz: await anotherConfig.keys.fizz,
    buzz: await anotherConfig.keys.buzz
  }

  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: `SSM Keys`,
      keys
    })
  }
  callback(null, response)
}

Alternatively you can make use of the keys function that return a json object with key/value pairs of your ssm keys, e.g.

const { keys } = require('sreda')
const AWS = require('aws-sdk')
const ssm = new AWS.SSM({
  region: 'us-west-1'
})

exports.handler = async (event, context, callback) => {
  let keys = await keys(ssm, ['foo', 'bar'])

  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: `SSM Keys`,
      keys
    })
  }
  callback(null, response)
}

SSM and iamRoleStatements

In order for your lambda function to access the SSM it has to:

  • have access to the internet
  • have rights to get the parameters from SSM

To allow your lambda function to access SSM you have to put similar iamRoleStatements section into your provider block

Please note, in order to be able to use CloudFormation Pseudo Parameters, like ${AWS::Region} and ${AWS::AccountId} you have to use a variableSyntax parameter with the value you see in the example below and you should use 'Fn::Sub' when assembling your Resource

provider:
  name: aws
  runtime: nodejs8.10
  variableSyntax: "\\${((?!AWS)[ ~:a-zA-Z0-9._'\",\\-\\/\\(\\)]+?)}"
  iamRoleStatements:
    - Effect: 'Allow'
      Action: 'ssm:GetParameters'
      Resource:
        - 'Fn::Sub': 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*'
    - Effect: 'Allow'
      Action: 'kms:Decrypt'
      Resource:
        - 'Fn::Sub': 'arn:aws:kms:us-east-1:${AWS::AccountId}:key/<your-kms-key>'

Through the iamRoleStatements you can also granulary manage the access of your lambda function to SSM

provider:
  name: aws
  runtime: nodejs8.10
  variableSyntax: "\\${((?!AWS)[ ~:a-zA-Z0-9._'\",\\-\\/\\(\\)]+?)}"
  iamRoleStatements:
    - Effect: 'Allow'
      Action: 'ssm:GetParameters'
      Resource:
        - 'Fn::Sub':
          'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/foo'
        - 'Fn::Sub':
          'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/bar'
    - Effect: 'Allow'
      Action: 'kms:Decrypt'
      Resource:
        - 'Fn::Sub': 'arn:aws:kms:us-east-1:${AWS::AccountId}:key/<your-kms-key>'

Developing locally

If you use sreda while developing locally, you can set process.env.NODE_ENV = 'local' and use the environment variables set in process.env.