Tuhina Singh
Tuhina Singh

Reputation: 1007

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

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.

Upvotes: 4

Views: 4229

Answers (3)

Tim Babych
Tim Babych

Reputation: 3444

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

Upvotes: -1

user818510
user818510

Reputation: 3622

You can get the globally configured credentials from aws.config.credentials

Get the accessKeyId:

var accessKeyId = aws.config.credentials.accessKeyId;

Get the secretAccessKey:

var secretAccessKey = aws.config.credentials.secretAccessKey;

Upvotes: 8

Taylor Ackley
Taylor Ackley

Reputation: 1437

You want to look at Nodes environment variables.

You can access all enviroment variables through the process.env

Specifically, you want this:

console.log(AWS_SECRET_ACCESS_KEY);
console.log(process.env.AWS_ACCESS_KEY_ID);

Upvotes: 0

Related Questions