Retrieve AWS ssm parameter in bulk

ahadcse picture ahadcse · Oct 17, 2018 · Viewed 10.6k times · Source

How can I retrieve parameters from AWS Systems Manager (parameter store) in bulk (or more than one parameter) at a time? Using aws-sdk, following is the Node.js code I have written to retrieve SSM parameter from parameter store:

      const ssm = new (require('aws-sdk/clients/ssm'))()

      const getSSMKey = async params => {
          const {Parameter: {Value: APIKey}} = await ssm.getParameter(params).promise()
          return APIKey
    }

    const [param1, param2, param3] = await Promise.all([
      getSSMKey({ Name: '/data/param/PARAM1', WithDecryption: true }),
      getSSMKey({ Name: '/data/param/PARAM2', WithDecryption: true }),
      getSSMKey({ Name: '/data/param/PARAM3', WithDecryption: true })
    ])
    console.log(param1, param2, param3)

But with this code, I am sending 3 request for getting 3 parameters which is inefficient in case of large number of parameters. Is there any way to retrieve more than one parameters in one request. if ssm.getParameters() is the method to do that then please give an example (particularly parameter to that method). I tried but I receive nothing.

Answer

user1032613 picture user1032613 · Dec 5, 2018

According to the AWS document, GetParameter gets the value for one parameter, whereas GetParameters gets the value for multiple.

Their usages are very similar too. When using GetParameters to get multiple values, pass in multiple names as a list for Names, instead of passing a single name as string for Name.

Code sample, to get parameters named "foo" and "bar", in "us-west-1" region:

const AWS = require('aws-sdk');
AWS.config.update({ region: "us-west-1" });

const SSM = require('aws-sdk/clients/ssm');
const ssm = new SSM()
const query = {
    "Names": ["foo", "bar"],
    "WithDecryption": true
}
let param = ssm.getParameters(query, (err, data) => {
    console.log('error = %o', err);
    console.log('raw data = %o', data);
})