Skip to content

Commit ed0b610

Browse files
author
James Renaud
authored
AWS SDK v3 Support (#34)
* feat: support aws-sdk v3 * chore: lint fixes * chore: doc cleanup for aws-sdk v3
1 parent d0afe44 commit ed0b610

File tree

2 files changed

+71
-23
lines changed

2 files changed

+71
-23
lines changed

README.md

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,77 @@ npm install --save aws-elasticsearch-connector @elastic/elasticsearch aws-sdk
2121
### Using global configuration
2222

2323
```javascript
24-
const { Client } = require('@elastic/elasticsearch')
25-
const AWS = require('aws-sdk')
26-
const createAwsElasticsearchConnector = require('aws-elasticsearch-connector')
24+
const { Client } = require("@elastic/elasticsearch");
25+
const AWS = require("aws-sdk");
26+
const createAwsElasticsearchConnector = require("aws-elasticsearch-connector");
2727

2828
// (Optional) load profile credentials from file
2929
AWS.config.update({
30-
profile: 'my-profile'
31-
})
30+
profile: "my-profile",
31+
});
3232

3333
const client = new Client({
3434
...createAwsElasticsearchConnector(AWS.config),
35-
node: 'https://my-elasticsearch-cluster.us-east-1.es.amazonaws.com'
36-
})
35+
node: "https://my-elasticsearch-cluster.us-east-1.es.amazonaws.com",
36+
});
3737
```
3838

3939
### Using specific configuration
4040

4141
```javascript
42-
const { Client } = require('@elastic/elasticsearch')
43-
const AWS = require('aws-sdk')
44-
const createAwsElasticsearchConnector = require('aws-elasticsearch-connector')
42+
const { Client } = require("@elastic/elasticsearch");
43+
const AWS = require("aws-sdk");
44+
const createAwsElasticsearchConnector = require("aws-elasticsearch-connector");
4545

4646
const awsConfig = new AWS.Config({
4747
// Your credentials and settings here, see
4848
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
49-
})
49+
});
5050

5151
const client = new Client({
5252
...createAwsElasticsearchConnector(awsConfig),
53-
node: 'https://my-elasticsearch-cluster.us-east-1.es.amazonaws.com'
54-
})
55-
````
53+
node: "https://my-elasticsearch-cluster.us-east-1.es.amazonaws.com",
54+
});
55+
```
56+
57+
### Using aws-sdk v3
58+
59+
```javascript
60+
const { STSClient, AssumeRoleCommand } = require("@aws-sdk/client-sts");
61+
const { Client } = require("@elastic/elasticsearch");
62+
const createAwsElasticsearchConnector = require("./src/index.js");
63+
64+
async function ping() {
65+
const creds = await assumeRole(
66+
"arn:aws:iam::0123456789012:role/Administrator",
67+
"us-east-1"
68+
);
69+
const client = new Client({
70+
...createAwsElasticsearchConnector({
71+
region: "us-east-1",
72+
credentials: creds,
73+
}),
74+
node: "https://my-elasticsearch-cluster.us-east-1.es.amazonaws.com",
75+
});
76+
const response = await client.ping();
77+
console.log(`Got Response`, response);
78+
}
79+
80+
async function assumeRole(roleArn, region) {
81+
const client = new STSClient({ region });
82+
const response = await client.send(
83+
new AssumeRoleCommand({
84+
RoleArn: roleArn,
85+
RoleSessionName: "aws-es-connection",
86+
})
87+
);
88+
return {
89+
accessKeyId: response.Credentials.AccessKeyId,
90+
secretAccessKey: response.Credentials.SecretAccessKey,
91+
sessionToken: response.Credentials.SessionToken,
92+
};
93+
}
94+
```
5695

5796
## Test
5897

src/AmazonTransport.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,25 @@ module.exports = awsConfig => {
1717
options = {}
1818
}
1919

20-
// Promise support
21-
if (typeof callback === 'undefined') {
22-
return awaitAwsCredentials(awsConfig)
23-
.then(() => super.request(params, options))
24-
}
20+
// check if getCredentials exists, if so this is an aws-sdk v2 global config object
21+
if (typeof awsConfig.getCredentials !== 'function') {
22+
if (typeof callback === 'undefined') {
23+
return super.request(params, options)
24+
} else {
25+
super.request(params, options, callback)
26+
}
27+
} else {
28+
// Promise support
29+
if (typeof callback === 'undefined') {
30+
return awaitAwsCredentials(awsConfig)
31+
.then(() => super.request(params, options))
32+
}
2533

26-
// Callback support
27-
awaitAwsCredentials(awsConfig)
28-
.then(() => super.request(params, options, callback))
29-
.catch(callback)
34+
// Callback support
35+
awaitAwsCredentials(awsConfig)
36+
.then(() => super.request(params, options, callback))
37+
.catch(callback)
38+
}
3039
}
3140
}
3241

0 commit comments

Comments
 (0)