Skip to content

Commit

Permalink
Upgrade to AWS SDK v3
Browse files Browse the repository at this point in the history
  • Loading branch information
tallpants committed Mar 27, 2023
1 parent fa5a8fb commit 98d3537
Show file tree
Hide file tree
Showing 7 changed files with 853 additions and 167 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ https://www.npmjs.com/package/ssm-parameter-store

## Changelog

#### `3.0.0`

- Now uses AWS SDK v3 and supports the Node.js 18.x runtime on AWS Lambda. If you need to use AWS SDK v2, use the `2.1.2` release of this package.

#### `2.1.2`

- Take an SSM parameter store instance as a constructor parameter instead of initializing it within the package.

## Usage

```js
const AWS = require('aws-sdk');
const { SSMClient } = require('@aws-sdk/client-ssm');
const SSMParameterStore = require('ssm-parameter-store');

const parameters = new SSMParameterStore(new AWS.SSM(), {
const parameters = new SSMParameterStore(new SSMClient(), {
SomeParameter: 'some_parameter',
SomeNestedParameter: '/some/nested/parameter',
NonExistentParameter: 'this_parameter_doesnt_exist_on_ssm'
Expand Down Expand Up @@ -66,8 +70,3 @@ exports.handler = async (event, context) => {
// If you're using TypeScript you'll also get a compile time error
}
```

## Autocompletion

![](docs/autocomplete-1.png)
![](docs/autocomplete-2.png)
Binary file removed docs/autocomplete-1.png
Binary file not shown.
Binary file removed docs/autocomplete-2.png
Binary file not shown.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ssm-parameter-store",
"version": "2.1.2",
"version": "3.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": "https://github.com/tallpants/ssm-parameter-store",
Expand All @@ -18,12 +18,12 @@
"prepublishOnly": "rm -rf dist && tsc"
},
"devDependencies": {
"aws-sdk": "^2.814.0",
"@types/node": "^12.0.8",
"ts-node": "^8.3.0",
"typescript": "^3.9.7"
"@aws-sdk/client-ssm": "3.299.0",
"@types/node": "^18.15.10",
"ts-node": "^10.9.1",
"typescript": "^5.0.2"
},
"peerDependencies": {
"aws-sdk": "2.x"
"@aws-sdk/client-ssm": "3.x"
}
}
38 changes: 12 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type AWS from "aws-sdk";
import { GetParameterCommand, GetParametersCommand, ParameterNotFound, SSMClient } from '@aws-sdk/client-ssm';

function chunk<T>(array: T[], chunkSize: number): Array<T[]> {
const arrays: Array<T[]> = [];
Expand All @@ -24,46 +24,42 @@ interface Options {
}

class SSMParameterStore<TParameters extends Record<string, string>> {
private ssm: AWS.SSM;
private ssm: SSMClient;

private parameterNamesToKeys = {} as TParameters;
private parameterKeysToValues: Record<string, string> = {};
private keyLoaded: Record<string, boolean> = {};

constructor(ssm: AWS.SSM, parameterNamesToKeys: TParameters) {
constructor(ssm: SSMClient, parameterNamesToKeys: TParameters) {
this.ssm = ssm;
this.parameterNamesToKeys = parameterNamesToKeys;
for (const key of Object.values(parameterNamesToKeys)) {
this.parameterKeysToValues[key] = "";
this.parameterKeysToValues[key] = '';
this.keyLoaded[key] = false;
}
}

private async fetchOne(key: string) {
try {
const ssmResponse = await this.ssm
.getParameter({ Name: key, WithDecryption: true })
.promise();
const ssmResponse = await this.ssm.send(new GetParameterCommand({ Name: key, WithDecryption: true }));
return ssmResponse.Parameter!.Value!;
} catch (err) {
if (err.code === "ParameterNotFound") {
return "";
if (err instanceof ParameterNotFound) {
return '';
}
throw err;
}
}

private async fetchTen(keys: string[]) {
const ssmResponse = await this.ssm
.getParameters({ Names: keys, WithDecryption: true })
.promise();
const ssmResponse = await this.ssm.send(new GetParametersCommand({ Names: keys, WithDecryption: true }));
const responseKeysToValues: Record<string, string> = {};

for (const parameter of ssmResponse.Parameters!) {
responseKeysToValues[parameter.Name!] = parameter.Value!;
}

return keys.map((key) => responseKeysToValues[key] || "");
return keys.map((key) => responseKeysToValues[key] || '');
}

private async loadAll() {
Expand All @@ -83,26 +79,16 @@ class SSMParameterStore<TParameters extends Record<string, string>> {
}

async preload(options: Options = { ignoreCache: false }) {
if (
options.ignoreCache ||
Object.values(this.keyLoaded).some(
(keyLoadedState) => keyLoadedState === false
)
) {
if (options.ignoreCache || Object.values(this.keyLoaded).some((keyLoadedState) => keyLoadedState === false)) {
return this.loadAll();
}
}

async get(
name: keyof TParameters,
options: Options = { ignoreCache: false }
) {
async get(name: keyof TParameters, options: Options = { ignoreCache: false }) {
const key = this.parameterNamesToKeys[name];

if (!key) {
throw new Error(
`Unknown parameter ${name}. Not in new SSMParameterStore({ }) declaration`
);
throw new Error(`Unknown parameter ${String(name)}. Not in new SSMParameterStore({ }) declaration`);
}

if (options.ignoreCache || !this.keyLoaded[key]) {
Expand Down
20 changes: 10 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const AWS = require("aws-sdk");
const SsmParameterStore = require("./src");
const { SSMClient } = require('@aws-sdk/client-ssm');
const SsmParameterStore = require('./src');

async function main() {
const params = new SsmParameterStore(new AWS.SSM({ region: "us-east-1" }), {
TestParameter: "test_parameter",
TestNestedParameter: "/this/is/a/test/nested/parameter",
NonExistentParameter: "doesntexist",
const params = new SsmParameterStore(new SSMClient({ region: 'us-east-1' }), {
TestParameter: 'test_parameter',
TestNestedParameter: '/this/is/a/test/nested/parameter',
NonExistentParameter: 'doesntexist',
});

console.log(await params.get("NonExistentParameter"));
console.log(await params.get("TestNestedParameter"));
console.log(await params.get("TestNestedParameter", { ignoreCache: true }));
console.log(await params.get('NonExistentParameter'));
console.log(await params.get('TestNestedParameter'));
console.log(await params.get('TestNestedParameter', { ignoreCache: true }));

console.log(await params.getAll());

console.log(await params.getAll({ ignoreCache: true }));

console.log(await params.preload());

console.log(await params.get("UndeclaredParameter"));
console.log(await params.get('UndeclaredParameter'));
}

main();
Loading

0 comments on commit 98d3537

Please sign in to comment.