Created
February 24, 2018 06:17
-
-
Save artburkart/f8d3b52774c8399cb8c6d98876bf845d to your computer and use it in GitHub Desktop.
Using aws-sdk npm package with DefaultProviderChain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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