Skip to content

Latest commit

 

History

History
114 lines (81 loc) · 3.54 KB

with-sns-create-package.md

File metadata and controls

114 lines (81 loc) · 3.54 KB

Sample function code

Sample code is available for the following languages.

Topics

Node.js 8

The following example processes messages from Amazon SNS, and logs their contents.

Example index.js

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");
};

Zip up the sample code to create a deployment package. For instructions, see AWS Lambda deployment package in Node.js.

Java 11

The following example processes messages from Amazon SNS, and logs their contents.

Example LambdaWithSNS.java

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;
     }
}

Dependencies

  • aws-lambda-java-core
  • aws-lambda-java-events

Build the code with the Lambda library dependencies to create a deployment package. For instructions, see AWS Lambda deployment package in Java.

Go

The following example processes messages from Amazon SNS, and logs their contents.

Example lambda_handler.go

package main

import (
    "context"
    "fmt"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-lambda-go/events"
)

func handler(ctx context.Context, snsEvent events.SNSEvent) {
    for _, record := range snsEvent.Records {
        snsRecord := record.SNS
        fmt.Printf("[%s %s] Message = %s \n", record.EventSource, snsRecord.Timestamp, snsRecord.Message)
    }
}

func main() {
    lambda.Start(handler)
}

Build the executable with go build and create a deployment package. For instructions, see AWS Lambda deployment package in Go.

Python 3

The following example processes messages from Amazon SNS, and logs their contents.

Example lambda_handler.py

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

Zip up the sample code to create a deployment package. For instructions, see AWS Lambda deployment package in Python.