Last active
April 8, 2021 06:18
-
-
Save parthdesai93/1bd3a25ad4cf788d49ce4a00a1bb3268 to your computer and use it in GitHub Desktop.
http-aws-es compatible with new Elasticsearch client.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* requires AWS creds to be updated. | |
* if they aren't, update using AWS.config.update() method before instatiing the client. | |
* | |
* import this module where you instantiate the client, and simply pass this module as the connection class. | |
* | |
* eg: | |
* const client = new Client({ | |
* node, | |
* Connection: AwsConnector | |
* }); | |
*/ | |
import AWS from 'aws-sdk'; | |
import { Connection } from '@elastic/elasticsearch'; | |
class AwsConnector extends Connection { | |
async request(params, callback) { | |
try { | |
const creds = await this.getAWSCredentials(); | |
const req = this.createRequest(params); | |
const { request: signedRequest } = this.signRequest(req, creds); | |
super.request(signedRequest, callback); | |
} catch (error) { | |
throw error; | |
} | |
} | |
createRequest(params) { | |
const endpoint = new AWS.Endpoint(this.url.href); | |
let req = new AWS.HttpRequest(endpoint); | |
Object.assign(req, params); | |
req.region = AWS.config.region; | |
if (!req.headers) { | |
req.headers = {}; | |
} | |
let body = params.body; | |
if (body) { | |
let contentLength = Buffer.isBuffer(body) | |
? body.length | |
: Buffer.byteLength(body); | |
req.headers['Content-Length'] = contentLength; | |
req.body = body; | |
} | |
req.headers['Host'] = endpoint.host; | |
return req; | |
} | |
getAWSCredentials() { | |
return new Promise((resolve, reject) => { | |
AWS.config.getCredentials((err, creds) => { | |
if (err) { | |
if (err && err.message) { | |
err.message = `AWS Credentials error: ${e.message}`; | |
} | |
reject(err); | |
} | |
resolve(creds); | |
}); | |
}); | |
} | |
signRequest(request, creds) { | |
const signer = new AWS.Signers.V4(request, 'es'); | |
signer.addAuthorization(creds, new Date()); | |
return signer; | |
} | |
} | |
export { AwsConnector }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AwsConnector } from './aws_es_connector'; | |
import { Client } from '@elastic/elasticsearch'; | |
import AWS from 'aws-sdk'; | |
// load aws keys and region, ideally using env variables. | |
let accessKey = '****'; | |
let secretKey = '****'; | |
let region = '**'; //eg: us-east-1 | |
let node = '***'; //node url | |
AWS.config.update({ | |
credentials: new AWS.Credentials(accessKey, secretKey), | |
region | |
}); | |
const client = new Client({ | |
node, | |
Connection: AwsConnector | |
}); | |
//use this client to talk to AWS managed ES service. | |
export default client; |
Thank you @parthdesai93! Was very helpful
Hey. I find I spot an issue.
Look at this:
`
> w.byteLength 8 `const x = 'ߤߤߤߤ';
x.length
4
w = Buffer.from(x)
Content-Length is provided in bytes. So the header is going to have invalid value - 4 bytes instead of 8.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool .thanks saved my day