Ad
Getting Values From Parameter Store In AWS
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.
Ad
Answer
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
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad