Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if aws-sdk is has global credentials configured

From the information provided in the AWS SDK config guide there are multiple ways to configure the SDK:

  1. Loaded from IAM roles for Amazon EC2 (if running on EC2),
  2. Loaded from the shared credentials file (~/.aws/credentials),
  3. Loaded from environment variables,
  4. Loaded from a JSON file on disk,
  5. Hardcoded in your application

Is there a way to determine:

a) If the SDK has global config (credentials) and b) how those credentials/settings were loaded/configured?

like image 457
developerjack Avatar asked Oct 24 '25 23:10

developerjack


1 Answers

So it appears you can look into the config's credentialProvider object which is a CredentialProviderChain. Its resolve() method returns an instance of a AWS.Credentials which will identify which method is used for configuring credentials.

Example as follows:

AWS.config.credentialProvider.resolve(function(err, credential) {
   if(credential !== null) {
     awsCredential = credential.constructor.name;      
 })
 console.log("AWS configured? ", awsCredential ? 'yes'.green : 'no'.red);
 if(awsCredential) {
   console.log("    Provided by: ", awsCredential);
 }

Specifically noting that the returned credential reveals its classname via credential.constructor.name.

Edit: I've now written this up as a post comparing the use of callbacks to promises to achieve the same goal - just in case anyone else encounters the same question.

like image 59
developerjack Avatar answered Oct 27 '25 20:10

developerjack