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

lambda-params-secrets

v1.0.3

Published

A Node.js client for the AWS Lambda Parameters and Secrets Extension

Downloads

2,767

Readme

lambda-params-secrets

A Node.js client for the AWS Lambda Parameters and Secrets Extension.

What does it do?

What this package does: This package's zero-dependency Client makes GET requests from your Lambda to the AWS Extension layer's localhost endpoint. It saves you the effort of building the URL, making the HTTP request and parsing the response.

What AWS does: The AWS Parameters and Secrets Lambda Extension is an AWS-managed Lambda Layer. It's one way for Lambdas to retrieve parameters from the AWS Parameter Store and retrieve secrets from AWS Secrets Manager. Instead of making SDK calls to these services directly, an Extension-enabled Lambda function makes a HTTP GET request to the Extension's localhost endpoint. The Extension makes the GetParameter and GetSecretValue SDK calls. It returns the JSON result in the HTTP response to the Lambda. The Extension also caches the results, which is its killer feature.

Getting Started

Prerequisites

  1. Deploy a Node.js Lambda with the AWS-Parameters-and-Secrets-Lambda-Extension layer added.
  2. Grant the Lambda ssm:GetParameter or secrets:GetSecretValue permissions. Using the AWS Extension requires the same IAM permissions as making the SDK calls directly.

Installation

npm install --save lambda-params-secrets

The main Client class itself has zero dependencies. The package has dependencies on the AWS JS SDK v3 clients @aws-sdk/client-secrets-manager and @aws-sdk/client-ssm for their Typescript type definitions only.

Usage

Instantiate a Client in your Lambda function handler. Call the Client's methods to get values:

// my_lambda_handler.ts
import { Client } from 'lambda-params-secrets';

export async function handler(): Promise<void> {
  const client = new Client();
  const fooListParam: string[] = await client.stringListParameter(
    '/my-app/config/foo-list'
  );
  console.log(fooListParam); // -> ["foo", "bar", "baz"]
}

API

Use Client methods to retrieve parameters and secrets values:

| Method | Options | Happy return value | | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ | ------------------ | | stringParameter(name, options) | version, label | string | | stringListParameter(name, options) | version, label | array of strings | | secureStringParameter(name, options) | version, label, withDecryption | string | | stringSecret(secretId, options) | versionId, versionStage | string | | binarySecret(secretId, options) | versionId, versionStage | Buffer | | stringSecretfromParameterStore(secretId, options) | version, label | string | | binarySecretfromParameterStore(secretId, options) | version, label | Buffer |

The above methods return null if the Client receives an error response.

Want the whole response instead of just the value? Call parameterResponse(name, options) or secretResponse(name, options) to retrieve the Extension's full JSON response.

Client options

The Client options have sensible defaults. Instantiating a client with new Client() is a good starting point, which is equivalent to:

const client = new Client({
  token: process.env.AWS_SESSION_TOKEN, // default value.  AWS set this opaque value.
  port: process.env.PARAMETERS_SECRETS_EXTENSION_HTTP_PORT, // default value.  AWS sets this by default to 2773.
  requester: new FetchRequester(), // default value.  Bare-bones getter that uses the Node.js native fetch library.  Use HttpRequester() for NodeJS runtimes < 18.
});

Resources

What if it's not working?

  • Double-check the prerequisites.
  • Check your Lambda's CloudWatch logs. Regardless of the error, the AWS Extension sends clients only a generic an unexpected error occurred while executing request message in the HTTP response. But the Extension sends useful error details to the Lambda's CloudWatch logs.
  • Please submit a Bug Report issue if you are experiencing a package-related error.

Alternatives

  1. Use the AWS Extension without this package. All the Client does is make the HTTP requests. You can do this yourself with any HTTP client.
  2. Use the AWS Javascript SDK to call GetParameter and GetSecretValue directly. This is a great approach if you won't benefit from caching.
  3. Set non-secret values at deploy-time as Lambda environment variables.

Using the AWS Extension without this package

  • The same prerequisites apply.
  • Make a GET request. See the AWS docs for more examples.
    http://localhost:2773/systemsmanager/parameters/get?name=%2Flambda-ext-test-param%2Fdummy-string
  • Set the X-Aws-Parameters-Secrets-Token header value to AWS_SESSION_TOKEN.

The AWS Extension responds with a JSON object:

{
  "Parameter": {
    "ARN": "arn:aws:ssm:us-east-1:123456789012:parameter/lambda-ext-test-param/dummy-string",
    "DataType": "text",
    "LastModifiedDate": "2022-11-16T08:22:40.362Z",
    "Name": "/lambda-ext-test-param/dummy-string",
    "Selector": null,
    "SourceResult": null,
    "Type": "String",
    "Value": "my-string-param-value",
    "Version": 1
  },
  "ResultMetadata": {}
}

Contributing

Please submit Bug Reports and Feature Requests as issues. :tada: Pull Requests are also welcome! See CONTRIBUTING.md for details.

License

This code is made available under the MIT license.