-
-
Save rxgx/7e1b24de5936ff1b2b815a3d9cc3897a to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk'); | |
const client = new AWS.SecretsManager({}); | |
// Call the AWS API and return a Promise | |
function getAwsSecret(secretName) { | |
return client.getSecretValue({ SecretId: secretName }).promise(); | |
} | |
// Create a async function to use the Promise | |
// Top level await is a proposal | |
async function getAwsSecretAsync (secretName) { | |
var error; | |
var response = await getAwsSecret(secretName).catch(err => (error = err)); | |
return [error, response]; | |
} | |
// Call the async function and return NodeJS callback style | |
module.exports = function asyncExample () { | |
var [error, secret] = getAwsSecretAsync('dev/MySecret/MyService'); | |
if (error) { | |
// Trigger an error and halt | |
console.error(error); | |
return; | |
} | |
// Use the result | |
console.debug(secret); | |
} |
@Harrisonkamau still having the same issue with the promised value even with await :(
Hi @dc-currenxie, you can try this one. Hope this help.
async secretManager(secretName) {
try {
const data = await client.getSecretValue({
SecretId: secretName
}).promise();
if (data) {
if (data.SecretString) {
const secret = data.SecretString;
const parsedSecret = JSON.parse(secret);
return {
data: parsedSecret,
error: null
};;
}
const binarySecretData = data.SecretBinary;
return {
data: binarySecretData,
error: null
};
}
} catch (error) {
return {
data: null,
error: error
};
}
}
@ToungMartin @dc-currenxie
Sorry to hear. Normally, a Promise returns Promise<Pending>
if there's no await
so there no way to access the resolved value.
Are you able to send me a formatted (using markdown) code that you're running and how you're calling the functions?
const AWS = require('aws-sdk');
const client = new AWS.SecretsManager({});
// Call the AWS API and return a Promise
function getAwsSecret(secretName) {
return client.getSecretValue({ SecretId: secretName }).promise();
}
// Create a async function to use the Promise
// Top level await is a proposal
async function getAwsSecretAsync (secretName) {
var error;
var response = await getAwsSecret(secretName).catch(err => (error = err));
return [error, response];
}
// Call the async function and return NodeJS callback style
module.exports = function asyncExample () {
var [error, secret] = getAwsSecretAsync('dev/MySecret/MyService');
if (error) {
// Trigger an error and halt
console.error(error);
return;
}
// Use the result
console.debug(secret);
}
This might be a dumb question but how do you then import and use this from elsewhere?
const { asyncExample } = require('./foobar');
module.exports.createStore = () => {
// console.log(asyncExample())
};
This code works :
const clientsecrets = new SecretsManager( { region: 'eu-west-3' } );
const getMySecret = async ( SecretId ) =>
{
const s = await clientsecrets.getSecretValue( { SecretId } ).promise();
return s.SecretString;
};
const secret = await getMySecret( 'STRIPE_PUBLISHABLE_TEST_KEY' );
console.log( 'secret : ', secret );
@rudyhadoux That'll work! You'll have to watch out for SecretString
not being found on s
if the result of clientsecrets.getSecretValue
isn't perfect (rejected).
I don't understand your message. The only thing I can say is I tested this code today within a Typescript AWS Lamba and it works.
If it works once, it always works in the same type of environment.
s
could be null
or undefined
if clientsecrets.getSecretValue
takes too long or something happens on AWS servers.
This code works :
const clientsecrets = new SecretsManager( { region: 'eu-west-3' } ); const getMySecret = async ( SecretId ) => { const s = await clientsecrets.getSecretValue( { SecretId } ).promise(); return s.SecretString; }; const secret = await getMySecret( 'STRIPE_PUBLISHABLE_TEST_KEY' ); console.log( 'secret : ', secret );
I'm getting this error
SyntaxError: await is only valid in async functions and the top level bodies of modules
This code works :
const clientsecrets = new SecretsManager( { region: 'eu-west-3' } ); const getMySecret = async ( SecretId ) => { const s = await clientsecrets.getSecretValue( { SecretId } ).promise(); return s.SecretString; }; const secret = await getMySecret( 'STRIPE_PUBLISHABLE_TEST_KEY' ); console.log( 'secret : ', secret );
I'm getting this error SyntaxError: await is only valid in async functions and the top-level bodies of modules
const secret = await getMySecret( 'STRIPE_PUBLISHABLE_TEST_KEY' );
console.log( 'secret : ', secret );
This block of code needs to be wrapped in an async
function. Node.js doesn't, yet, natively support calling await
outside of an async
unlike Deno.
To fix your issue, you can do this:
// Either
// using an IIFE (Immediately-invoke Function Expression)
(async () => {
const secret = await getMySecret( 'STRIPE_PUBLISHABLE_TEST_KEY' );
console.log( 'secret : ', secret );
})();
// or just use a Promise expression
getMySecret
.then((secret) => console.log( 'secret : ', secret ))
.catch((error) => console.error(error));
Top level async
/await
is now available in Node.js LTS (v16.15.0 as of now).
I think you need to add an
await
togetAwsSecretAsync('mysecret');
to get the resolved Promise value