Skip to content

Instantly share code, notes, and snippets.

@rxgx
Last active June 20, 2022 02:52
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rxgx/7e1b24de5936ff1b2b815a3d9cc3897a to your computer and use it in GitHub Desktop.
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
Copy link

Harrisonkamau commented Nov 19, 2018

Very helpful indeed! And from it, I created:

const config = require('config');
const AWS = require('aws-sdk');

// These are variables from my config file :)
const endpoint = config.get('AWS.endpoint');
const region = config.get('AWS.region');
const secretName = config.get('secretName');

const secretsManager = new AWS.SecretsManager({
  endpoint,
  region,
});
exports.handler = async (event, context) => {
  try {
    const data = await secretsManager.getSecretValue({
      SecretId: secretName,
    }).promise();

    if (data) {
      if (data.SecretString) {
        const secret = data.SecretString;
        const parsedSecret = JSON.parse(secret);
        return {
          secrets: parsedSecret,
        };
      }

      const binarySecretData = data.SecretBinary;
      return binarySecretData;
    }
  } catch (error) {
    console.log('Error retrieving secrets');
    console.log(error);
  }
};

@adriangabardo
Copy link

You helped me finish up a task I've been on for over a week. Thank you 👍

@JesusAndres2
Copy link

Great!! Thanks a lot!!!

@katiekoschland
Copy link

Thank you, this has been very helpful 👍

@rxgx
Copy link
Author

rxgx commented Jun 18, 2019

You're welcome. Glad to have helped!

@jayboss222
Copy link

Awesome bro!!

@claytonbez
Copy link

You Sir, are a legend!

@MartinEnclave
Copy link

Many thanks, Harrisonkamau.

@Manoj-Chakladar
Copy link

Thank you so much @Harrisonkamau . your code solved my task which i was trying to accomplish since last two weeks. You Sir, are a legend. :)

@Harrisonkamau
Copy link

You're welcome guys ;)

@BogdanMaier
Copy link

Useful, thanks! the , I was missing the .promise()

Copy link

ghost commented Dec 5, 2019

I get this error with serverless framework:

Promise { <pending > }

with:

const AWS = require('aws-sdk');

AWS.config.logger = console;

const client = new AWS.SecretsManager({
    region: "ap-southeast-2"
});

function getAwsSecret(secretName) {
  return client.getSecretValue({ SecretId: secretName }).promise();
}

async function getAwsSecretAsync (secretName) {
  var error;
  var response = await getAwsSecret(secretName);
  return response;
}

module.exports.notify = function asyncExample () {
  var secret = getAwsSecretAsync('mysecret');
  
console.debug(secret);
}

@dsoto3
Copy link

dsoto3 commented Jan 8, 2020

const secretsManager = new AWS.SecretsManager({
  endpoint,
  region,
});

Thanks! It worked like a charm for my Node.10.x lambda

@devalexsales
Copy link

I get this error with serverless framework:

Promise { <pending > }

with:

const AWS = require('aws-sdk');

AWS.config.logger = console;

const client = new AWS.SecretsManager({
    region: "ap-southeast-2"
});

function getAwsSecret(secretName) {
  return client.getSecretValue({ SecretId: secretName }).promise();
}

async function getAwsSecretAsync (secretName) {
  var error;
  var response = await getAwsSecret(secretName);
  return response;
}

module.exports.notify = function asyncExample () {
  var secret = getAwsSecretAsync('mysecret');
  
console.debug(secret);
}

same here

@Harrisonkamau
Copy link

const AWS = require('aws-sdk');

AWS.config.logger = console;

const client = new AWS.SecretsManager({
    region: 'ap-southeast-2',
});

function getAwsSecret(secretName) {
  return client.getSecretValue({ SecretId: secretName }).promise();
}

async function getAwsSecretAsync (secretName) {
  try {
    const response = await getAwsSecret(secretName);
    return response;
  } catch (error) {
    console.error('Error occurred while retrieving AWS secret');
    console.error(error);
  }
}

module.exports.notify = async function asyncExample () {
  const secret = await getAwsSecretAsync('mysecret');
  console.debug(secret);
}

I think you need to add an await to getAwsSecretAsync('mysecret'); to get the resolved Promise value

@dc-currenxie
Copy link

@Harrisonkamau still having the same issue with the promised value even with await :(

@TuongMartin
Copy link

TuongMartin commented Jan 17, 2020

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

@Harrisonkamau
Copy link

@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?

@dep
Copy link

dep commented Mar 11, 2020

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

@rudyhadoux
Copy link

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

@rxgx
Copy link
Author

rxgx commented Aug 4, 2021

@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).

@rudyhadoux
Copy link

rudyhadoux commented Aug 4, 2021

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.

@rxgx
Copy link
Author

rxgx commented Aug 5, 2021

s could be null or undefined if clientsecrets.getSecretValue takes too long or something happens on AWS servers.

@theepag
Copy link

theepag commented Jun 2, 2022

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

@Harrisonkamau
Copy link

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

@rxgx
Copy link
Author

rxgx commented Jun 3, 2022

Top level async/await is now available in Node.js LTS (v16.15.0 as of now).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment