Search code examples
aws-sdk-js

How to list existing AWS profiles using the JS SDK?


I would like to list profiles using the AWS JS SDK, so that users can select the AWS profile they want to use for the rest of the execution of the code.

I know I can ask the SDK to load credentials from a profile. But I want to list those profiles.

Profiles are stored in ~/.aws/credentials by default, but their location can be affected by different environment variables. That is why I'd rather avoid reading that file directly, but instead rely of the SDK's logic.

The final application is a CLI application with full access to the user's machine (NodeJS, not running in the browser).


Solution

  • Turns out v3 of the JS SDK has a package just for that:

    npm install @aws-sdk/shared-ini-file-loader
    
    const sharedIniFileLoader = require('@aws-sdk/shared-ini-file-loader');
    
    const profiles = await sharedIniFileLoader.loadSharedConfigFiles();