2017-10-25 2 views
1

私は弾性検索にDynamoDBの中で、私が持っているデータを追加するには、以下を使用します。は、私のラムダコードからAWS弾性検索にレコードを追加することはできません

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

だからダイナモに成功し、ラムダ、その後、上記のコードでの引き金DB私は、私はすべてがこれまでに良いことがわかり、それをデバッグ:

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上記+ =チャンクで。常に空であり、resBodyに何も追加されません。また、私はawsの弾性検索をチェックするときに私は何も追加されて表示されませんが、同時に私は私のラムダコンソールでエラーが表示されません?コードが間違っているのですか、何か不足していますか?

+0

AWSNodeHttpクライアントと全体postToEsを交換することができるはずはhttps://www.elastic.co/guide/en/elasticsearch/ ...私が使用してきた非常に素晴らしいではありませんclient/javascript-api /をこのプラグインとともに使用してIAMロールhttps://github.com/TheDeveloper/http-aws-esを取得します。 – razboy

+0

役立つ場合は、コードサンプルを投稿することができます。 異なるメモでは、dynamodbはelasticsearchと統合されていません。あなたもこれをしなくてはいけませんか? https://aws.amazon.com/about-aws/whats-new/2015/08/amazon-dynamodb-elasticsearch-integration/ – razboy

+0

@razboyありがとうございます。サンプルコードを投稿してください。 –

答えて

2

私のインポートでは、次のことを行います。弾性検索クラスタでは、アカウントにクラスタへのアクセスを許可するIAMポリシーを設定しました。以下のようなものは、より制限的になりたいかもしれません。私のラムダで

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

私はラムダに利用できるAWSのcredsをを使用してクライアント接続を作成するために、次の手順を実行します
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をインストールします(これは開発者専用です)。

あなたはclient.bulk build insert

関連する問題