4

Wanted to sync up the AWS Opensearch with DynamoDB table data.

I'm triggering my Lambda function via dynamodb streams and wanted to save it in AWS Opensearch via AWS Lambda. I wanted to do it in NodeJs, but everywhere on internet I can found only Python examples.

My DDB table has the following attributes id, name, color, price (example: 0000car1000, Toyota Camary Hybrid, Green, 27,000)

In AWS Opensearch, I wanted to search on name attribute.

I'm easily able to get the new data in Lambda, but stuck on how to store that data in Opensearch.

Here is my Lambda code


const { OpenSearchClient, AcceptInboundConnectionCommand } = require("@aws-sdk/client-opensearch");

const client = new OpenSearchClient({ region: process.env.LAMBDA_REGION });

const host = 'search-ddbsearch-yxxxxxxgu.us-east-2.es.amazonaws.com';
const url = 'https://' + host + '/' + index + '/' + type + '/';
const headers = {"Content-Type": "application/json"};

const index = 'name'; // expecting ddb table column name would map to name in opensearch
const type = '_doc'; // Not sure what is type

module.exports.handler = async (event) => {
    console.log("DDB Stream event === ", JSON.stringify(event, null, 2));

    for (const record of event.Records) {
        console.log(record.eventID);
        console.log(record.eventName);
        console.log('DynamoDB Record: %j', record.dynamodb);

        const newImage = record.dynamodb.NewImage;
        // TODO: Save newImage in Opensearch
    }
    return `Successfully processed ${event.Records.length} records.`;
}

Is there any reference I can follow to make it happen in NodeJS?

Ninja-aman
  • 75
  • 5
  • Have you come across anything? Surprised AWS doesn't have an example in Node. – good1492 Aug 26 '22 at 23:00
  • Code and examples are available on the Opensearch documentation site here: https://opensearch.org/docs/2.7/clients/javascript/index/ – datashaman May 09 '23 at 12:52

0 Answers0