无法从我的lambda代码中添加记录到aws弹性搜索

时间:2017-10-25 22:56:46

标签: amazon-web-services aws-lambda

我使用以下内容将我在dynamodb中的数据添加到弹性搜索中:

https://aws.amazon.com/it/blogs/aws/category/amazon-elasticsearch-service/?nc1=h_ls


var AWS = require('aws-sdk');
var path = require('path');

//Object for all the ElasticSearch Domain Info
var esDomain = {
region: process.env.RegionForES,
endpoint: process.env.EndpointForES,
index: process.env.IndexForES,
doctype: 'onboardingrecords'
};
//AWS Endpoint from created ES Domain Endpoint
var endpoint = new AWS.Endpoint(esDomain.endpoint);
//The AWS credentials are picked up from the environment.
var creds = new AWS.EnvironmentCredentials('AWS');

console.log('Loading function');
exports.handler = (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
console.log(JSON.stringify(esDomain));

event.Records.forEach((record) => {
    console.log(record.eventID);
    console.log(record.eventName);
    console.log('DynamoDB Record: %j', record.dynamodb);

    var dbRecord = JSON.stringify(record.dynamodb);
    postToES(dbRecord, context, callback);
});
 };

function postToES(doc, context, lambdaCallback) {
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);
        lambdaCallback(null, 'Lambda added document ' + doc);
    });
  }, function(err) {
    console.log('Error: ' + err);
    lambdaCallback('Lambda failed with error ' + err);
});
}

所以dynamo db成功触发lamda然后在上面的代码中我调试它时,我发现一切都很好:

send.handleRequest(req, null, function(httpResp) {
    var respBody = '';
    httpResp.on('data', function (chunk) {
        respBody += chunk;
    });
    httpResp.on('end', function (chunk) {
        console.log('Response: ' + respBody);
        lambdaCallback(null,'Lambda added document ' + doc);
    });
}, function(err) {
    console.log('Error: ' + err);
    lambdaCallback('Lambda failed with error ' + err);
});
 }

所以在上面这部分respBody + = chunk;始终为空,并且没有任何内容添加到resBody。此外,当我检查aws弹性搜索时,我没有看到任何添加但同时我在lambda控制台中看不到任何错误?代码是错的还是我遗漏了什么?

1 个答案:

答案 0 :(得分:5)

对于我的导入,我执行以下操作。在弹性搜索群集中,我将IAM策略设置为允许帐户访问群集。如下所示,您可能希望限制更多。

"Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::1234:root"
      },
      "Action": "es:*",
      "Resource": "*"
    }
]

在我的lambda中,我执行以下操作来创建一个客户端连接,该连接使用lambda可用的AWS信用:

const AWS = require('aws-sdk');
const connectionClass = require('http-aws-es');
const elasticsearch = require('elasticsearch');

const client = elasticsearch.Client({
  log: 'trace',
  hosts: 'host',
  connectionClass: connectionClass,
  amazonES: {
    region: 'region',
    credentials: new AWS.EnvironmentCredentials('AWS')
  }
});

然后您应该能够按照doco使用客户端。 Npm安装依赖项http-aws-es,elasticsearch和aws-sdk(这个只用于dev)。

您应该可以将整个postToEs替换为client.bulk build insert