Getting the error CredentialsProviderError: Could not load credentials from any providers #4956
-
I am trying to retrieve secrets from secret manager. I am able to get the access key, session token, region from shared credentials file but somehow, I am getting that error for node. I am attaching the config object. the code I am using : import {
GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";
import { secretsClient } from "./libs/secretsClient.js" ;
import { createRequire } from 'module';
import AWS from "aws-sdk";
const require = createRequire(import.meta.url);
// Set the parameters
const params = {
SecretId: "arn:aws:secretsmanager:REGION:XXXXXXXXXXXX:secret:mysecret-XXXXXX",
};
const run = async () => {
var AWS = require('aws-sdk');
const credentials = new AWS.SharedIniFileCredentials({profile: 'default_source'});
AWS.config.credentials = credentials;
const AWS_REGION = "ap-south-1";
AWS.config.update({ region: AWS_REGION });
AWS.config.getCredentials(function(err) {
if (err) console.log(err.stack);
// credentials not loaded
else {
console.log("Access key:", AWS.config.credentials.accessKeyId);
}
});
console.log("Region: ", AWS.config.region);
console.log(AWS.config);
let data;
try {
data = await secretsClient.send(new GetSecretValueCommand(params));
console.log("data", data);
return data; // For unit tests.
} catch (err) {
console.log("err", err);
}
let secret;
if ("SecretString" in data) {
secret = data.SecretString;
} else {
console.log("else:", data);
// Create a buffer
const buff = new Buffer(data.SecretBinary, "base64");
secret = buff.toString("ascii");
}
return secret;
};
run(); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hi @ANANDAJYOTIBASU, sorry to hear about your issues. The issue here seems to be that you are mixing SDKs. I see you are using both SDKs, v2 and v3, and you are loading credentials for v2 both using v3 to do the call. So, I recommend you to use just one SDK since this could cause confusion in your code.
import { SecretsManagerClient, GetSecretValueCommand } from "@aws-sdk/client-secrets-manager";
import { fromIni } from "@aws-sdk/credential-provider-ini";
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const client = new SecretsManagerClient({
region: "us-east-2",
credentials: fromIni({profile: "default_source"})
});
const params = {
SecretId: "arn:aws:secretsmanager:REGION:XXXXXXXXXXXX:secret:mysecret-XXXXXX",
};
const run = async () => {
let data;
let secret;
try {
const response = await client.send(new GetSecretValueCommand(params));
return response.SecretString;
} catch (err) {
console.log("err", err);
}
};
run(); Please let me know if that helps! Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
-
I got the same error with another client: @aws-sdk/client-transcribe My solutions was:
The credentials field was the key:
|
Beta Was this translation helpful? Give feedback.
Hi @ANANDAJYOTIBASU, sorry to hear about your issues. The issue here seems to be that you are mixing SDKs. I see you are using both SDKs, v2 and v3, and you are loading credentials for v2 both using v3 to do the call. So, I recommend you to use just one SDK since this could cause confusion in your code.
Here is a quick example that could work for you, but using V3 SDK:
@aws-sdk/credential-provider-ini
by runningnpm install @aws-sdk/credential-provider-ini