Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 339
ahadcse Avatar asked Oct 17 '18 16:10

ahadcse


People also ask

Where are SSM parameters stored?

We can store these parameters in SSM, as encrypted secure strings, under a common path: /app/production/db/{DB_NAME, DB_USERNAME, DB_PASSWORD, DB_HOST} . Naturally, different environments will get different paths — with testing, staging etc.

How do I get AWS parameter store Arn?

You can locate the Amazon Resource Name (ARN) of the default key in the AWS KMS console on the AWS managed keys page. The default key is the one identified with aws/ssm in the Alias column.

What is AWS :: SSM :: parameter?

Parameter Store, a capability of AWS Systems Manager, provides secure, hierarchical storage for configuration data management and secrets management. You can store data such as passwords, database strings, Amazon Machine Image (AMI) IDs, and license codes as parameter values.


Video Answer


2 Answers

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);
})
like image 177
user1032613 Avatar answered Sep 20 '22 14:09

user1032613


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
        }
like image 27
ahadcse Avatar answered Sep 18 '22 14:09

ahadcse