How to use the EnvironmentCredentials function from aws-sdk

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

aws-sdk.EnvironmentCredentials is a class in the AWS SDK for JavaScript that retrieves AWS credentials from environment variables.

7
8
9
10
11
12
13
14
15
16
17
18
    region: process.env.AWS_REGION,
    index: 'records',
    doctype: 'movie'
};


var creds = new AWS.EnvironmentCredentials('AWS');
var endpoint = new AWS.Endpoint(openSearchDomain.endpoint);


function postDocumentToOpenSearch(doc, context) {
    var req = new AWS.HttpRequest(endpoint);
fork icon201
star icon952
watch icon0

28
29
30
31
32
33
34
35
36
37
/**
 * @class glueHelper
 * @constructor
 */
constructor() {
  this.creds = new AWS.EnvironmentCredentials('AWS'); // Lambda provided credentials
  this.config = {
    credentials: this.creds,
    region: process.env.AWS_REGION,
  };
fork icon29
star icon34
watch icon4

How does aws-sdk.EnvironmentCredentials work?

aws-sdk.EnvironmentCredentials is a class in the AWS SDK for JavaScript that allows you to retrieve AWS credentials from environment variables. When an instance of the EnvironmentCredentials class is created, it reads the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables by default. It uses these values to create an instance of the Credentials class, which represents AWS access credentials. If the AWS_SESSION_TOKEN environment variable is present, it is also used to create a session token for temporary security credentials. Once the Credentials instance is created, it can be used to make authenticated requests to AWS services, such as Amazon S3 or Amazon EC2. Here's an example of how to use EnvironmentCredentials to authenticate requests to an AWS service using the AWS.S3 client: javascript Copy code {{{{{{{ const AWS = require('aws-sdk'); // Create an instance of EnvironmentCredentials const credentials = new AWS.EnvironmentCredentials('AWS'); // Create an S3 client with the credentials const s3 = new AWS.S3({ credentials: credentials, region: 'us-west-2' }); // Make a request to the S3 service s3.listBuckets((err, data) => { if (err) { console.log(err); } else { console.log(data); } }); In this example, we create an instance of EnvironmentCredentials and pass in the string 'AWS' as a parameter. This tells the EnvironmentCredentials class to look for the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables. We then create an instance of the AWS.S3 client and pass in the credentials object we created earlier as a parameter. This sets the credentials for the S3 client to the values we retrieved from the environment variables. Finally, we make a request to the listBuckets method of the S3 service. The AWS.S3 client automatically uses the credentials we provided to authenticate the request and return a list of the buckets in our S3 account. Overall, aws-sdk.EnvironmentCredentials provides a simple and flexible way to authenticate requests to AWS services using environment variables. It is especially useful in environments like AWS Lambda or Docker containers, where credentials need to be provided dynamically at runtime.

806
807
808
809
810
811
812
813
814
815
    apiVersion: config.get('ES.API_VERSION'),
    hosts: esHost,
    connectionClass: require('http-aws-es'), // eslint-disable-line global-require
    amazonES: {
      region: config.get('AMAZON.AWS_REGION'),
      credentials: new AWS.EnvironmentCredentials('AWS')
    }
  })
} else {
  esClient = new elasticsearch.Client({
fork icon44
star icon17
watch icon25

+ 15 other calls in file

847
848
849
850
851
852
853
854
855
856
    apiVersion: config.get("ES.API_VERSION"),
    hosts: esHost,
    connectionClass: require("http-aws-es"), // eslint-disable-line global-require
    amazonES: {
      region: config.get("AMAZON.AWS_REGION"),
      credentials: new AWS.EnvironmentCredentials("AWS"),
    },
  });
} else {
  esClient = new elasticsearch.Client({
fork icon44
star icon17
watch icon25

+ 24 other calls in file

Ai Example

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

// Create an instance of EnvironmentCredentials
const credentials = new AWS.EnvironmentCredentials("AWS");

// Create an S3 client with the credentials
const s3 = new AWS.S3({
  credentials: credentials,
  region: "us-west-2",
});

// Make a request to the S3 service
s3.listBuckets((err, data) => {
  if (err) {
    console.log(err);
  } else {
    console.log(data);
  }
});

In this example, we create an instance of EnvironmentCredentials and pass in the string 'AWS' as a parameter. This tells the EnvironmentCredentials class to look for the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables. We then create an instance of the AWS.S3 client and pass in the credentials object we created earlier as a parameter. This sets the credentials for the S3 client to the values we retrieved from the environment variables. Finally, we make a request to the listBuckets method of the S3 service. The AWS.S3 client automatically uses the credentials we provided to authenticate the request and return a list of the buckets in our S3 account.

436
437
438
439
440
441
442
443
444
445
//     apiVersion: config.get('ES.API_VERSION'),
//     hosts: esHost,
//     connectionClass: require('http-aws-es'), // eslint-disable-line global-require
//     amazonES: {
//       region: config.get('AMAZON.AWS_REGION'),
//       credentials: new AWS.EnvironmentCredentials('AWS')
//     }
//   })
// } else {
//   esClient = new elasticsearch.Client({
fork icon11
star icon4
watch icon18

+ 32 other calls in file

86
87
88
89
90
91
92
93
94
95
request.headers['host'] = domain;
// Content-Length is only needed for DELETE requests that include a request
// body, but including it for all requests doesn't seem to hurt anything.
request.headers['Content-Length'] = Buffer.byteLength(request.body);

var credentials = new AWS.EnvironmentCredentials('AWS');
var signer = new AWS.Signers.V4(request, 'es');
signer.addAuthorization(credentials, new Date());

var client = new AWS.HttpClient();
fork icon2
star icon4
watch icon0

5
6
7
8
9
10
11
12
13
14
15
16
var type = '_search';


exports.lambdaHandler = (event, context, callback) => {
	let endpoint = new AWS.Endpoint(domain);
	let request = new AWS.HttpRequest(endpoint);
	// let credentials = new AWS.EnvironmentCredentials('AWS');


	let query = {
		"size": 25,
		"query": {
fork icon0
star icon1
watch icon0

58
59
60
61
62
63
64
65
66
67
aws_request.headers['Content-Type'] = 'application/json';
// Content-Length is only needed for DELETE requests that include a request
// body, but including it for all requests doesn't seem to hurt anything.
aws_request.headers['Content-Length'] = Buffer.byteLength(aws_request.body);

const credentials = new AWS.EnvironmentCredentials('AWS');
const signer = new AWS.Signers.V4(aws_request, 'es');
signer.addAuthorization(credentials, new Date());

const client = new AWS.HttpClient();
fork icon1
star icon0
watch icon0

14
15
16
17
18
19
20
21
22
23
24
  accessKeyId: accessKeyId,
  accessKeySecret: accessKeySecret,
})


const signRequest = (request) => {
  const credentials = new AWS.EnvironmentCredentials('AWS')
  const signer = new AWS.Signers.V4(request, 'es')
  signer.addAuthorization(credentials, new Date())
}

fork icon0
star icon0
watch icon0

18
19
20
21
22
23
24
25
26
27
28
29
request.body = payload;
request.headers['host'] = domain;
request.headers['Content-Type'] = contentType;
request.headers['Content-Length'] = Buffer.byteLength(request.body).toString();


let credentials = new AWS.EnvironmentCredentials('AWS');
let signer = new AWS.Signers.V4(request, service);
signer.addAuthorization(credentials, new Date());


let client = new AWS.HttpClient();
fork icon0
star icon0
watch icon0

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


exports.updateConfig = function (region) {
  AWS.CredentialProviderChain.defaultProviders = [
    function () {
      return new AWS.EnvironmentCredentials('AWS');
    },
    function () {
      return new AWS.EnvironmentCredentials('AMAZON');
    },
fork icon0
star icon0
watch icon0

+ 3 other calls in file

112
113
114
115
116
117
118
119
120
121
	return resolve();
}

// 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(),
];
fork icon0
star icon0
watch icon0

+ 3 other calls in file

3
4
5
6
7
8
9
10
11
const start = ({ config }, cb) => {
  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()
  ];
fork icon0
star icon0
watch icon1

+ 13 other calls in file