Skip to content

Commit

Permalink
Merge pull request #6 from valiton/fix-bug-for-more-than-10-keys-aws-…
Browse files Browse the repository at this point in the history
…limit

Fix bug for more than 10 keys aws limit
  • Loading branch information
ChristophP authored Dec 7, 2018
2 parents 19c3a04 + 9a0104c commit 35143ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@valiton/aws-param-inject",
"version": "1.0.2",
"version": "1.0.3",
"description": "Simple tool to fetch and inject parameters from AWS SSM into a string.",
"bin": "src/index.js",
"main": "src/inject-ssm-params.js",
Expand Down
31 changes: 25 additions & 6 deletions src/inject-ssm-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ const AWS = require("aws-sdk");

const ssm = new AWS.SSM();

// aws has a hard limit on the number of keys it can return and errors of there
// are too many https://amzn.to/2QhXRTf
const keyPerRequestLimit = 10;
const ssmPattern = /\$\{ssm:.*?\}/g;
const extractKey = match => match.slice(6, -1);

const responseToMap = data =>
data.Parameters.reduce(
const responseToParams = data => data.Parameters;

const paramsToMap = params =>
params.reduce(
(acc, current) => ({ ...acc, [current.Name]: current.Value }),
{}
);
Expand All @@ -17,21 +22,35 @@ const makeError = invalidParameters => {
return new Error(errMsg);
};

const createChunks = (chunkSize, array) => {
const numberOfChunks = Math.ceil(array.length / chunkSize);
return Array.from({ length: numberOfChunks }, (_, index) => {
const start = index * chunkSize;
return array.slice(index * chunkSize, start + chunkSize);
});
};

const flatten = arr => arr.reduce((acc, current) => acc.concat(current), []);

const fetchParameters = names =>
ssm
.getParameters({ Names: names, WithDecryption: true })
.promise()
.then(
data =>
data.InvalidParameters.length === 0
? Promise.resolve(responseToMap(data))
? Promise.resolve(responseToParams(data))
: Promise.reject(makeError(data.InvalidParameters))
);

const replaceParameters = input => paramsMap =>
input.replace(ssmPattern, match => paramsMap[extractKey(match)]);

module.exports = input =>
Promise.resolve(input.match(ssmPattern).map(extractKey))
.then(fetchParameters)
module.exports = input => {
const names = input.match(ssmPattern).map(extractKey);
const requests = createChunks(keyPerRequestLimit, names).map(fetchParameters);
return Promise.all(requests)
.then(flatten)
.then(paramsToMap)
.then(replaceParameters(input));
};

0 comments on commit 35143ad

Please sign in to comment.