How to use the SharedIniFileCredentials function from aws-sdk

Find comprehensive JavaScript aws-sdk.SharedIniFileCredentials code examples handpicked from public code repositorys.

aws-sdk.SharedIniFileCredentials is a class that allows you to create AWS credentials from a shared credentials file.

28
29
30
31
32
33
34
35
36
37
it("| inspect correctness for constructor when awsRegion is set in configuration", () => {
  const s3Client = new S3Client(TEST_CONFIGURATION);
  expect(s3Client).to.be.instanceof(S3Client);
  expect(s3Client.awsRegion).equal(TEST_AWS_REGION);
  expect(aws.config.region).equal(TEST_AWS_REGION);
  expect(aws.config.credentials).deep.equal(new aws.SharedIniFileCredentials({profile: TEST_AWS_PROFILE}));
});

it("| inspect correctness for constructor when awsRegion is null in configuration", () => {
  const configuration = {
fork icon50
star icon139
watch icon40

+ 13 other calls in file

0
1
2
3
4
5
6
7
8
9
// a fuzzer for undocumented params

const AWS = require('aws-sdk');
const fetch = require('node-fetch');

var credentials = new AWS.SharedIniFileCredentials({profile: 'nothing'});
AWS.config.credentials = credentials;
AWS.config.region = 'us-east-1';

var long_undocumented_test_list = `
fork icon9
star icon117
watch icon9

How does aws-sdk.SharedIniFileCredentials work?

aws-sdk.SharedIniFileCredentials works by creating an instance of the SharedIniFileCredentials class that reads a shared credentials file on disk and retrieves AWS credentials from it.

When creating an instance of the SharedIniFileCredentials class, you can optionally specify a profile name to read from the credentials file. If no profile name is specified, the class will use the default profile.

The SharedIniFileCredentials class reads the accessKeyId, secretAccessKey, and sessionToken from the specified profile in the credentials file. These values are then used to create an AWS.Credentials object, which can be used to authenticate with AWS services.

Once you have created an instance of the SharedIniFileCredentials class, you can use it in your AWS client code to authenticate your requests.

This can be useful when you need to access AWS resources from a local development environment or a non-AWS cloud environment, and you want to use credentials from a shared credentials file rather than hard-coding them into your application code.

17
18
19
20
21
22
23
24
25
26

me.findService = function(input) {
    var params = {region:input.region};
    if (input.creds)  params.credentials = input.creds;
    else if (input.profile) {
        var credentials = new AWS.SharedIniFileCredentials({profile: input.profile});
        AWS.config.credentials = credentials;
    }
    var ec2 = new AWS.EC2(params);
    return ec2;
fork icon6
star icon29
watch icon21

0
1
2
3
4
5
6
7
8
9
var AWS = require('aws-sdk');

// For local testing, credentials are not pre-supplied
//
//AWS.config.region = 'us-east-1';
//var credentials = new AWS.SharedIniFileCredentials({profile: 'rds-snapshot-deleter'});
//AWS.config.credentials = credentials;

var numberOfSnapshotsToKeep = 5;
var identifier = 'production';
fork icon444
star icon2
watch icon2

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const AWS = require("aws-sdk");

// Create an instance of the SharedIniFileCredentials class
const credentials = new AWS.SharedIniFileCredentials({
  profile: "my-profile-name",
});

// Use the credentials to create an AWS client
const s3 = new AWS.S3({ credentials: credentials });

// Use the client to make a request
s3.listBuckets((err, data) => {
  if (err) {
    console.error(err);
  } else {
    console.log(data);
  }
});

In this example, we first import the aws-sdk library. We create an instance of the SharedIniFileCredentials class, specifying the my-profile-name profile from the shared credentials file. We use the credentials to create an S3 client, passing in the credentials using the credentials option. We use the S3 client to make a request to list the user's buckets. If the request is successful, the response data is logged to the console. If there is an error, the error is logged to the console. By using aws-sdk.SharedIniFileCredentials, we are able to authenticate our requests to AWS services using credentials from a shared credentials file.

3
4
5
6
7
8
9
10
11
12
AWS.CredentialProviderChain.defaultProviders = [
  function () {
    return new AWS.EC2MetadataCredentials();
  },
  function () {
    return new AWS.SharedIniFileCredentials({
      profile: aws_profile ? aws_profile : "interactives",
    });
  },
];
fork icon1
star icon0
watch icon0

0
1
2
3
4
5
6
7
8
9
10
11
const fs = require('fs')
const path = require('path')
const AWS = require('aws-sdk')


if (!process.env.AWS_ACCESS_KEY_ID && !process.env.AWS_SECRET_ACCESS_KEY) {
  const credentials = new AWS.SharedIniFileCredentials({ profile: 'ktmap' })
  AWS.config.credentials = credentials
}


const s3 = new AWS.S3({ apiVersion: '2006-03-01' })
fork icon0
star icon1
watch icon2

+ 24 other calls in file

31
32
33
34
35
36
37
38
39
40
    secretAccessKey: parent.options.awsSecretAccessKey,
    sessionToken: parent.options.sessionToken
  }
} else if (useAwsProfile) {
  isAwsCredentials = true
  credentials = new AWS.SharedIniFileCredentials({
    profile: parent.options.awsIniFileProfile,
    filename: path.join(os.homedir(), '.aws', parent.options.awsIniFileName ? parent.options.awsIniFileName : 'config')
  })
}
fork icon0
star icon0
watch icon0

114
115
116
117
118
119
120
121
122
123

// Load AWS credentials from environment
AWS.CredentialProviderChain.defaultProviders = [
	() => new AWS.EnvironmentCredentials("AWS"),
	() => new AWS.EnvironmentCredentials("AMAZON"),
	() => new AWS.SharedIniFileCredentials({ profile: config.profile || "default" }),
	() => new AWS.EC2MetadataCredentials(),
];

(new AWS.CredentialProviderChain()).resolve((err, cred) => {
fork icon0
star icon0
watch icon0

4
5
6
7
8
9
10
11
12
13
14
secret,
decodedBinarySecret;


/*
try{
  var credentials = new AWS.SharedIniFileCredentials({profile: 'tt'});
  AWS.config.credentials = credentials;
  console.log("Running enviroment local")
}catch (e) {
  console.log("Running enviroment cloud")
fork icon0
star icon0
watch icon0

4
5
6
7
8
9
10
11
12
13
const prefix = config.envPrefix;
const region = process.env[`${prefix}_REGION`] ? process.env[`${prefix}_REGION`] : config.region;

AWS.CredentialProviderChain.defaultProviders = [
  () => new AWS.EnvironmentCredentials(prefix),
  () => new AWS.SharedIniFileCredentials({ profile: config.profile || 'default' }),
  () => new AWS.EC2MetadataCredentials()
];

const chain = new AWS.CredentialProviderChain();
fork icon0
star icon0
watch icon1

+ 13 other calls in file