Skip to content

Instantly share code, notes, and snippets.

@eduardinni
Created January 10, 2020 00:42
Show Gist options
  • Save eduardinni/9976b19fa9bd846f223256e7a84b0df2 to your computer and use it in GitHub Desktop.
Save eduardinni/9976b19fa9bd846f223256e7a84b0df2 to your computer and use it in GitHub Desktop.
Code example 2

Database sync, using data streams (JavaScript)

This is a simple AWS Lambda function to syncronize data from DynamoDB to ElasticSearch, it is triggered via DynamoDB streams. Also transforms data types.

const fetch = require('node-fetch');
const es_url = "https://elasticsearch.us-east-1.es.amazonaws.com";

exports.handler = async (event) => {
  if (event.Records !== undefined && event.Records.length > 0) {
    for (let record of event.Records) {
      if (record.eventName === "INSERT" || record.eventName === "MODIFY") {
        const newImage = record.dynamodb.NewImage;
        const location = newImage.location;

        await fetch(`${es_url}/index/marker/${newImage.markerId.S}`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({
            map_id: newImage.mapId.N,
            user_posted: newImage.userPosted.N,
            date_posted: newImage.datePosted.S,
            reaction_total_verify: newImage.reactionTotalVerify.N,
            reaction_total_notexist: newImage.reactionTotalNotExist.N,
            location: {
              lat: location.M.lat.N,
              lon: location.M.lon.N
            }
          })
        })
          .catch(err => console.error(err));
      }
      else if (record.eventName === "REMOVE") {
        const markerId = record.dynamodb.Keys.markerId.S;

        await fetch(`${es_url}/index/marker/${markerId}`, {
          method: "DELETE"
        })
          .catch(err => console.error(err));

      }
    }
  }

  const response = {
    statusCode: 200,
    body: JSON.stringify('ok'),
  };
  return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment