"Невозможно прочитать свойство presigned-expires of undefined"
Общий обзор, я получил AWS Lambda, где работает приложение Node.js, которое отправляет через HTTP-вызов JSON на мою AWS Elastic Search DB
Итак, я начал с этой маленькой ошибки: AWS: {"Message":"User: anonymous is not authorized to perform: es:ESHttpPost"}
и по прошествии большого количества времени я, наконец, дошел до того, что понял, что AWS не нравится неподписанный запрос.
Теперь я застрял на этом
Response:
{
"errorMessage": "Cannot read property 'presigned-expires' of undefined",
"errorType": "TypeError",
"stackTrace": [
"V4.isPresigned (/var/runtime/node_modules/aws-sdk/lib/signers/v4.js:206:32)",
"V4.addAuthorization (/var/runtime/node_modules/aws-sdk/lib/signers/v4.js:27:14)",
"Promise (/var/task/index.js:18:16)",
"new Promise (<anonymous>)",
"exports.handler (/var/task/index.js:6:12)"
]
}
Много времени на Google и даже глубже в Интернете не дал мне решение этой проблемы.
Вот мой лямбда-код:
var AWS = require('aws-sdk');
var creds = new AWS.EnvironmentCredentials('AWS');
var http = require('http');
exports.handler = async (event, context) => {
return new Promise((resolve, reject) => {
const options = {
hostname: 'XXX_ES_DOMAIN.eu-central-1.es.amazonaws.com',
path: '/path/1',
method: 'POST'
};
const req = http.request(options, (res) => {
resolve('Success');
});
var signer = new AWS.Signers.V4(req, 'es');
signer.addAuthorization(creds, new Date());
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.write(JSON.stringify({ 'test': 'test' }));
req.end();
});
};
0 ответов
В вашем запросе может отсутствовать заголовок, см. Ниже.
var AWS = require('aws-sdk');
var path = require('path');
/* == Globals == */
var esDomain = {
region: 'us-east-1',
endpoint: 'my-domain-search-endpoint',
index: 'myindex',
doctype: 'mytype'
};
var endpoint = new AWS.Endpoint(esDomain.endpoint);
/*
* The AWS credentials are picked up from the environment.
* They belong to the IAM role assigned to the Lambda function.
* Since the ES requests are signed using these credentials,
* make sure to apply a policy that allows ES domain operations
* to the role.
*/
var creds = new AWS.EnvironmentCredentials('AWS');
/*
* Post the given document to Elasticsearch
*/
function postToES(doc, context) {
var req = new AWS.HttpRequest(endpoint);
req.method = 'POST';
req.path = path.join('/', esDomain.index, esDomain.doctype);
req.region = esDomain.region;
req.headers['presigned-expires'] = false;
req.headers['Host'] = endpoint.host;
req.body = doc;
var signer = new AWS.Signers.V4(req , 'es'); // es: service code
signer.addAuthorization(creds, new Date());
var send = new AWS.NodeHttpClient();
send.handleRequest(req, null, function(httpResp) {
var respBody = '';
httpResp.on('data', function (chunk) {
respBody += chunk;
});
httpResp.on('end', function (chunk) {
console.log('Response: ' + respBody);
context.succeed('Lambda added document ' + doc);
});
}, function(err) {
console.log('Error: ' + err);
context.fail('Lambda failed with error ' + err);
});
}
Я взял это из проекта github aws-samples amazon-elasticsearch-lambda-samples: https://github.com/aws-samples/amazon-elasticsearch-lambda-samples/blob/master/src/kinesis_lambda_es.js