3

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 :)

1
  • 1
    Your stream event should look just like a regular SNS event inside that your Lambda. What is the problem exactly? Commented Jul 23, 2015 at 17:11

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Also, mapbox has an excellent Lambda DDB table replication tool here: github.com/mapbox/dynamodb-replicator. Their Lambda function uses NodeJS, however.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.