Skip to content

Instantly share code, notes, and snippets.

@artburkart
Created February 24, 2018 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artburkart/f8d3b52774c8399cb8c6d98876bf845d to your computer and use it in GitHub Desktop.
Save artburkart/f8d3b52774c8399cb8c6d98876bf845d to your computer and use it in GitHub Desktop.
Using aws-sdk npm package with DefaultProviderChain
const AWS = require('aws-sdk');
// Gets copy of default provider chain
const chain = AWS.CredentialProviderChain.defaultProviders.slice(0);
// Inserts additional check for specific profile in ~/.aws/credentials file
chain.splice(2, 0, () => new AWS.SharedIniFileCredentials({profile: 'readonly_user'}));
// Creates credential resolver that uses my custom provider chain
const credentialProvider = new AWS.CredentialProviderChain(chain);
// Create sts client for later requests
let sts = new AWS.STS({
credentialProvider,
credentials: null, // This is necessary because of bug (https://github.com/aws/aws-sdk-js/pull/1367)
region: 'us-east-1'
});
// Figure out which set of credentials I ended up using
sts.getCallerIdentity({}, (_, data) => {
console.log(data);
});
// Kill off the sts client for the hell of it
sts = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment