AWS-SDK for NodeJS: how to get credentials being used in program

Viewed 2385

I am accessing AWS sdk and its services like this in my code:

var aws = require('aws-sdk');
const s3 = new aws.S3();

I want to see what are the credentials being picked up when I initialise the S3 object. I tried following ways and clearly I am unable to figure out from the documentation how to use the methods and classes properly.

var credo = aws.config.Credentials().get();
var credo = aws.config.Credentials;
var credo = aws.config.credentials;
var credo = aws.Credentials().get();
var credo = aws.Credentials();
var credo = aws.Credentials;

Can someone tell me the right way to get this data? I am not finding aws documentation easy to understand for this part.

Edit: I am able to update credentials in code using aws.config.update({accessKeyId: 'xxx', secretAccessKey: 'yyy', sessionToken:'zzz'

I want to see what these values are when I dont set them like this. Process environment variables are not set. I have credentials file set up correctly.

3 Answers

For modern NodeJS the recommended approach is to use awaits:

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

async function main() {
  AWS.config.credentials = new AWS.TemporaryCredentials();
  // retrieves credentials according to configuration precedence
  // https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html
  // updates credentials if expired
  await AWS.config.credentials.getPromise()
  // after we ensured that credentials are ready we use them
  const accessKeyId = AWS.config.credentials.accessKeyId;
  const secretAccessKey = AWS.config.credentials.secretAccessKey;
  const sessionToken = AWS.config.credentials.sessionToken;
}
Related