aws-sdk v3 cannot find credentials

4.6k views Asked by At

I'm using v3 of aws-sdk, trying to follow the reference docs exactly, but cannot load credentials.

const {fromIni} = require("@aws-sdk/credential-provider-ini");

const credentials = fromIni({});

... gives the error:

Unhandled Rejection (Error): Profile default could not be found or parsed in shared credentials file.

And:

const {parseKnownFiles} = require("@aws-sdk/credential-provider-ini");

const pkf = parseKnownFiles();

... gives the error that I think may be the cause:

TypeError: Cannot read property 'loadedConfig' of undefined

If it can't find a known credentials file then it's certainly not going to find default in there.

Yet I'm certain the credentials are there:

PS C:\> aws sts get-caller-identity --profile="default"
{
    "UserId": "*********************",
    "Account": "************",
    "Arn": "arn:aws:iam::************:user/*****"
}

How do I load my credentials in aws-sdk v3?

3

There are 3 answers

0
Sedat Polat On BEST ANSWER

You need to pass AWS profile into fromIni(). It is default in your case.

const {fromIni} = require("@aws-sdk/credential-provider-ini");

const credentials = fromIni({profile: 'default'});
0
Optcron On

i had the same problem just follow these steps

https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_profiles.html

place a .aws folder with the credentials file in home (linux) or %UserProfile% (windows)

1
Magraz On

I've been struggling too with the shared credential files. I know this is not the appropriate way due to security reasons, but you could try the following example in case you are pressured by time:

const client = new LexRuntimeV2Client({
    region: process.env.REACT_APP_AWS_REGION,
    credentials: {
        accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY,
        secretAccessKey: process.env.REACT_APP_AWS_SECRET_ACCESS_KEY
    },
});

In the snippet I am using environment variables and a particular client(AWS Lex) but you can substitute those for your keys, region, and client. I don't recommend hard coding your keys, but during development this should work fine as a temporary solution.