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.handler = function(event, context, callback) { // console.log('Received event:', JSON.stringify(event, null, 4)); var message = event.Records[0].Sns.Message; console.log('Message received from SNS:', message); callback(null, "Success"); };

    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 index.js.

  3. Zip the index.js file as LambdaWithSNS.zip.

Next Step

Step 2.2: Create the Execution Role (IAM Role)

Java

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

Copy
package example; import java.text.SimpleDateFormat; import java.util.Calendar; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.events.SNSEvent; public class LogEvent implements RequestHandler<SNSEvent, Object> { public Object handleRequest(SNSEvent request, Context context){ String timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime()); context.getLogger().log("Invocation started: " + timeStamp); context.getLogger().log(request.getRecords().get(0).getSNS().getMessage()); timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime()); context.getLogger().log("Invocation completed: " + timeStamp); return null; } }

Using the preceding code (in a file named LambdaWithSNS.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)

Python

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

    Copy
    from __future__ import print_function import json print('Loading function') def lambda_handler(event, context): #print("Received event: " + json.dumps(event, indent=2)) message = event['Records'][0]['Sns']['Message'] print("From SNS: " + message) return message

  2. Save the file as lambda_handler.py.

  3. Zip the lambda_handler.py file as LambdaWithSNS.zip.

Next Step

Step 2.2: Create the Execution Role (IAM Role)

On this page: