简体   繁体   中英

Retrieve AWS ssm parameter in bulk

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.

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);
})

At last it worked for me. Following is the code:

        const ssmConfig = async () => {
          const data = await ssm.getParameters({ Names: ['/data/param/PARAM1', '/data/param/PARAM2', '/bronto/rest//data/param/PARAM3'],
WithDecryption: true }).promise()
          const config = {}
          for (const i of data.Parameters) {
            if (i.Name === '/data/param/PARAM1') {
              config.param1 = i.Value
            }
            if (i.Name === '/data/param/PARAM2') {
              config.rest.clientId param2 = i.Value
            }
            if (i.Name === '/data/param/PARAM3') {
              config.param3 = i.Value
            }
          }
          return config
        }

This is what I did to retrieve all the parameters from a specific path.

**your SSM function  client :**
'use strict';
const SSM = require('aws-sdk/clients/ssm');
let ssmclient;
module.exports.init = () => {
    const region = process.env.REGION === undefined ? 'us-east-1' : process.env.REGION ;
    ssmclient = new SSM({region: region});
}
module.exports.getParameters = async (path) => {
    try {
        let params = {
            Path: path,
            WithDecryption: true
        };
        let allParameters = [];
        let data =  await ssmclient.getParametersByPath(params).promise();
        allParameters.push.apply(allParameters, data.Parameters);
        while(data.NextToken) {
            params.NextToken = data.NextToken;
            data =  await ssmclient.getParametersByPath(params).promise();
            allParameters.push.apply(allParameters, data.Parameters);
        }
        return allParameters;
    } catch (err) {
        return Promise.reject(err);
    }
}
calling this client:

const ssm = require("yourssmclinet");
ssm.init();
// you call only once to retrieve everything which falls under /data/param
const parameters = await getParameters("/data/param");
//from here you can fetch parameters['what ever needed'].

You essentially have two options to get parameters in bulk.

One is the method provided by @user1032613, but the other is to use the built-in function getParametersByPath().

A Lambda code example in node with all three methods can be seen below. Each method can take different params, for instance with the path you can make filters, etc. to get the exact values you need, see the documentation .

'use strict';
const AWS = require('aws-sdk');
const SSM = new AWS.SSM();

exports.handler = async (event) => {
    //Example get single item
    const singleParam = { Name: 'myParam' };
    const getSingleParam = await SSM.getParameter(singleParam).promise();

    //Example: Get Multiple values
    const multiParams = {
        Names: [ 'myParam1', 'myParam2', 'myParam3' ],
        WithDecryption: true
    };

    const getMultiParams = await SSM(multiParams).promise();

    //Example: Get all values in a path
    const pathParams = { Path: '/myPath/', WithDecryption: true };
    const getPathParams = await SSM.getParametersByPath(pathParams).promise();

    return 'Success';
};

Remember that you can also use environment variables. For example, you could write singleParam like this:

const singleParam = { Name: process.env.PARAM }

That way you can have code that extracts code from DEV, PROD, etc. depending on the stage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM