3
votes

I'm trying to create a DynamoDB trigger using DynamoDB Streams and AWS Lambda. I researched a lot but I couldn't find any way to read and process a DynamoDB Stream event in Java 8. I'm completely new to both these technologies so don't know how to work with this.

Essentially, what I want to do is create a record in table B whenever a record is created in table A.

Could any of you please point me to a code or post that handles this use case in Java?

Thanks :)

3
Your stream event should look just like a regular SNS event inside that your Lambda. What is the problem exactly?adamkonrad

3 Answers

5
votes

This code worked for me. You can use it to receive and process DynamoDB events in a Lambda function -

public class Handler implements RequestHandler<DynamodbEvent, Void> {

    @Override
    public Void handleRequest(DynamodbEvent dynamodbEvent, Context context) {

        for (DynamodbStreamRecord record : dynamodbEvent.getRecords()) {

            if (record == null) {
                continue;
            }

            // Your code here
            // Write to Table B using DynamoDB Java API
        }

        return null;
    }
}

When you create your Lambda, add the stream from table A as your event source, and you're good to go

0
votes

Hmm I can't seem to find the documentation integrating a Java Lambda function with DynamoDB streams, but the concept is the same as writing a NodeJS Lambda function with DDB streams, which is documented here: http://docs.aws.amazon.com/lambda/latest/dg/wt-ddb.html. Just replace the NodeJS function with a Java function (see here for the docs for creating a Java Lambda function: http://docs.aws.amazon.com/lambda/latest/dg/java-lambda.html). To replicate your data from table A to B, you can use the AWS Java SDK DynamoDB client to write the stream record from A to B in your Lambda function.

0
votes

DynamoDB Streams will send JSON to the handler. Just create a handler that takes a Java InputStream and deserialize the JSON from the inputstream. I posted an example to a similar question here.