Menu
AWS Lambda
Developer Guide

Step 2.1: Create a Lambda Function Deployment Package

From the Filter View list, choose the language you want to use for your Lambda function. The appropriate section appears with code and specific instructions for creating a deployment package.

Node.js

  1. Open a text editor, and then copy the following code.

    Copy
    console.log('Loading function'); exports.lambda_handler = function(event, context, callback) { console.log(JSON.stringify(event, null, 2)); event.Records.forEach(function(record) { console.log(record.eventID); console.log(record.eventName); console.log('DynamoDB Record: %j', record.dynamodb); }); callback(null, "message"); };

    Note

    The code sample is compliant with the Node.js runtimes v6.10 or v4.3. For more information, see Programming Model (Node.js)

  2. Save the file as ProcessDynamoDBStream.js.

  3. Zip the ProcessDynamoDBStream.js file as ProcessDynamoDBStream.zip.

Next Step

Step 2.2: Create the Execution Role (IAM Role)

Java

In the following code, handleRequest is the handler that AWS Lambda invokes and provides event data. The handler uses the predefined DynamodbEvent class, which is defined in the aws-lambda-java-events library.

Copy
package example; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.DynamodbEvent; import com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord; public class DDBEventProcessor implements RequestHandler<DynamodbEvent, String> { public String handleRequest(DynamodbEvent ddbEvent, Context context) { for (DynamodbStreamRecord record : ddbEvent.getRecords()){ System.out.println(record.getEventID()); System.out.println(record.getEventName()); System.out.println(record.getDynamodb().toString()); } return "Successfully processed " + ddbEvent.getRecords().size() + " records."; } }

If the handler returns normally without exceptions, Lambda considers the input batch of records as processed successfully and begins reading new records in the stream. If the handler throws an exception, Lambda considers the input batch of records as not processed and invokes the function with the same batch of records again.

Using the preceding code (in a file named DDBEventProcessor.java), create a deployment package. Make sure that you add the following dependencies:

  • aws-lambda-java-core

  • aws-lambda-java-events

For more information, see Programming Model for Authoring Lambda Functions in Java.

Your deployment package can be a .zip file or a standalone .jar. You can use any build and packaging tool you are familiar with to create a deployment package. For examples of how to use the Maven build tool to create a standalone .jar, see Creating a .jar Deployment Package Using Maven without any IDE (Java) and Creating a .jar Deployment Package Using Maven and Eclipse IDE (Java). For an example of how to use the Gradle build tool to create a .zip file, see Creating a .zip Deployment Package (Java).

After you verify that your deployment package is created, go to the next step to create an IAM role (execution role). You specify this role at the time you create your Lambda function.

Next Step

Step 2.2: Create the Execution Role (IAM Role)

C#

In the following code, ProcessDynamoEvent is the handler that AWS Lambda invokes and provides event data. The handler uses the predefined DynamoDbEvent class, which is defined in the Amazon.Lambda.DynamoDBEvents library.

Copy
using System; using System.IO; using System.Text; using Amazon.Lambda.Core; using Amazon.Lambda.DynamoDBEvents; using Amazon.Lambda.Serialization.Json; namespace DynamoDBStreams { public class DdbSample { private static readonly JsonSerializer _jsonSerializer = new JsonSerializer(); public void ProcessDynamoEvent(DynamoDBEvent dynamoEvent) { Console.WriteLine($"Beginning to process {dynamoEvent.Records.Count} records..."); foreach (var record in dynamoEvent.Records) { Console.WriteLine($"Event ID: {record.EventID}"); Console.WriteLine($"Event Name: {record.EventName}"); string streamRecordJson = SerializeObject(record.Dynamodb); Console.WriteLine($"DynamoDB Record:"); Console.WriteLine(streamRecordJson); } Console.WriteLine("Stream processing complete."); } private string SerializeObject(object streamRecord) { using (var ms = new MemoryStream()) { _jsonSerializer.Serialize(streamRecord, ms); return Encoding.UTF8.GetString(ms.ToArray()); } } } }

To create a deployment package, follow the steps outlined in .NET Core CLI. In doing so, note the following after you've created your .NET project:

  • Rename the default Program.cs file with a file name of your choice, such as ProcessingDynamoDBStreams.cs.

  • Replace the default contents of the renamed Program.cs file with the code example above.

  • In the project.json file, add the following references to the dependencies node.

    • "Amazon.Lambda.Core":"1.0.0-*"

    • "Amazon.Lambda.Serialiation.Json":"1.0.0-*"

    • "Amazon.Lambda.DynamoDBEvents":"1.0.0-*"

After you verify that your deployment package is created, go to the next step to create an IAM role (execution role). You specify this role at the time you create your Lambda function.

Next Step

Step 2.2: Create the Execution Role (IAM Role)

Python

  1. Open a text editor, and then copy the following code.

    Copy
    from __future__ import print_function def lambda_handler(event, context): for record in event['Records']: print(record['eventID']) print(record['eventName']) print('Successfully processed %s records.' % str(len(event['Records'])))

  2. Save the file as ProcessDynamoDBStream.py.

  3. Zip the ProcessDynamoDBStream.py file as ProcessDynamoDBStream.zip.

Next Step

Step 2.2: Create the Execution Role (IAM Role)

On this page: