Menu
AWS Lambda
Developer Guide

Step 2.1: Create a 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

The following is example Node.js code that receives Amazon Kinesis event records as input and processes them. For illustration, the code writes some of the incoming event data to CloudWatch Logs.

Follow the instructions to create a AWS Lambda function deployment package.

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

    Copy
    console.log('Loading function'); exports.handler = function(event, context, callback) { //console.log(JSON.stringify(event, null, 2)); event.Records.forEach(function(record) { // Kinesis data is base64 encoded so decode here var payload = new Buffer(record.kinesis.data, 'base64').toString('ascii'); console.log('Decoded payload:', payload); }); 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 ProcessKinesisRecords.js.

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

Next Step

Step 2.2: Create the Execution Role (IAM Role)

Java

The following is example Java code that receives Amazon Kinesis event record data as a input and processes it. For illustration, the code writes some of the incoming event data to CloudWatch Logs.

In the code, recordHandler is the handler. The handler uses the predefined KinesisEvent class that is defined in the aws-lambda-java-events library.

Copy
package example; import java.io.IOException; import com.amazonaws.services.lambda.runtime.events.KinesisEvent; import com.amazonaws.services.lambda.runtime.events.KinesisEvent.KinesisEventRecord; public class ProcessKinesisEvents { public void recordHandler(KinesisEvent event) throws IOException { for(KinesisEventRecord rec : event.getRecords()) { System.out.println(new String(rec.getKinesis().getData().array())); } } }

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 ProcessKinesisEvents.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#

The following is example C# code that receives Amazon Kinesis event record data as a input and processes it. For illustration, the code writes some of the incoming event data to CloudWatch Logs.

In the code, HandleKinesisRecord is the handler. The handler uses the predefined KinesisEvent class that is defined in the Amazon.Lambda.KinesisEvents library.

Copy
using System; using System.IO; using System.Text; using Amazon.Lambda.Core; using Amazon.Lambda.KinesisEvents; namespace KinesisStreams { public class KinesisSample { [LambdaSerializer(typeof(JsonSerializer))] public void HandleKinesisRecord(KinesisEvent kinesisEvent) { Console.WriteLine($"Beginning to process {kinesisEvent.Records.Count} records..."); foreach (var record in kinesisEvent.Records) { Console.WriteLine($"Event ID: {record.EventId}"); Console.WriteLine($"Event Name: {record.EventName}"); string recordData = GetRecordContents(record.Kinesis); Console.WriteLine($"Record Data:"); Console.WriteLine(recordData); } Console.WriteLine("Stream processing complete."); } private string GetRecordContents(KinesisEvent.Record streamRecord) { using (var reader = new StreamReader(streamRecord.Data, Encoding.ASCII)) { return reader.ReadToEnd(); } } } }

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 ProcessingKinesisEvents.cs.

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

  • In the project.json file, make sure the following references are included in the dependencies node.

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

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

    • "Amazon.Lambda.Serialization.Json":"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

The following is example Python code that receives Amazon Kinesis event record data as input and processes it. For illustration, the code writes to some of the incoming event data to CloudWatch Logs.

Follow the instructions to create a AWS Lambda function deployment package.

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

    Note

    The from __future__ statement enables you to write code that is compatible with Python 2 or 3. If you are using runtime version 3.6, is not necessary to include it.

    Copy
    from __future__ import print_function #import json import base64 def lambda_handler(event, context): for record in event['Records']: #Kinesis data is base64 encoded so decode here payload=base64.b64decode(record["kinesis"]["data"]) print("Decoded payload: " + payload)

  2. Save the file as ProcessKinesisRecords.py.

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

Next Step

Step 2.2: Create the Execution Role (IAM Role)

On this page: