Getting values from parameter store in AWS

假如想象 提交于 2019-12-23 01:52:25

问题


Here is my code:

const AWS = require('aws-sdk');
const { Client } = require('pg');

function main() {
    AWS.config.update({region:'eu-west-2'});

    let db_user = {
        Name: "postgres_db_user",
        WithDecryption: false
    };

    let db_host = {
        Name: "postgres_db_host",
        WithDecryption: false
    };

    let db_database = {
        Name: "postgres_db_name",
        WithDecryption: false
    };

    let db_password = {
        Name: "postgres_db_password",
        WithDecryption: true
    };

    let db_port = {
        Name: "postgres_db_port",
        WithDecryption: false
    };

    let database_user_name;
    let database_host;
    let database_name;
    let database_port;
    let database_password;

    let ssm = new AWS.SSM();
    ssm.getParameter(db_user, function (err, data) {
        if (err) {
            console.log(err, err.stack);
        } else {
            database_user_name = data;
        }
    });

    ssm.getParameter(db_host, function (err, data) {
        if (err) {
            console.log(err, err.stack);
        } else {
            database_host = data;
        }
    });

    ssm.getParameter(db_database, function (err, data) {
        if (err) {
            console.log(err, err.stack);
        } else {
            database_name = data;
        }
    });

    ssm.getParameter(db_password, function (err, data) {
        if (err) {
            console.log(err, err.stack);
        } else {
            database_password = data;
        }
    });

    ssm.getParameter(db_port, function (err, data) {
        if (err) {
            console.log(err, err.stack);
        } else {
           database_port = data;
        }
    });

    console.log(database_user_name);
    console.log(database_host);
    console.log(database_name);
    console.log(database_password);
    console.log(database_port);

    let client = new Client({
        user: database_user_name,
        host: database_host,
        database: database_name,
        password: database_password,
        port: database_port,
    });
}

main();

The problem is that when I print the variables they are undefined, and I have no idea why. The SSM getParameter function is working because if I use the debugger, I see the values, but it doesn't seem to save the data variable in the variables for some strange reason.

If someone could help me as to why this code is printing undefined for the variables I would appreciate it.


回答1:


I was drafting my answer when John commented :).

getParameter is async function and it's being executed while you are initializing the client with properties.

Here is some sample way of coding initialization.(not perfect but you can get the idea).

var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });
var ssm = new AWS.SSM();

function getParameter(param) {
  return new Promise(function (success, reject) {
    ssm.getParameter(param, function (err, data) {
      if (err) {
        reject(err);
      } else {
        success(data);
      }
    });
  });
};

function main() {
  let db_user = {
    Name: "postgres_db_user",
    WithDecryption: false
  };
  let database_user_name;
  var promises = [];
  promises.push(getParameter(db_user));

  Promise.all(promises)
    .then(function (result) {
      database_user_name = result[0].Parameter.Value;
      console.log(database_user_name);
      let client = new Client({
        user: database_user_name
        //rest of the properties
      });
      //rest of the main() function code
    })
    .catch(function (err) {
      console.log(err);
    });
}
main();

Reference - https://stackoverflow.com/a/44868681/5030709



来源:https://stackoverflow.com/questions/53973798/getting-values-from-parameter-store-in-aws

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!