“无法读取未定义的属性'presigned-expires'”

时间:2018-08-22 14:25:32

标签: node.js amazon-web-services elasticsearch aws-lambda aws-elasticsearch

基本概述,我得到了一个正在运行Node.js应用程序的AWS Lambda,该应用程序通过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上花费了很多时间,甚至在网络上更漫长的时间都没有给我解决这个问题的方法。

这是我的lambda代码:

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();
    });
};

1 个答案:

答案 0 :(得分: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