How to process an SNS message in AWS Lambda written in Java?
Thanks to the comments by @dnault and @notionquest I found that this object is part of the aws-lambda-java-events
library. I added the dependency:
compile 'com.amazonaws:aws-lambda-java-events:1.3.0'
And now I can do:
public class ArchiveRequestHandler implements RequestHandler<SNSEvent, Void> {
@Override public Void handleRequest(SNSEvent input, Context context) {
// do something with the message
return null;
}
}
The following Lambda code processes the SNS input message/s:
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.SNSEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class LogEvent implements RequestHandler<SNSEvent, Object> {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
public Object handleRequest(SNSEvent request, Context context){
String timeStamp = sdf.format(new Date());
LambdaLogger logger = context.getLogger();
logger.log("-------------------------Invocation started: --------------" + timeStamp);
List<SNSEvent.SNSRecord> snsRecordList = request.getRecords();
if ( snsRecordList != null ){
SNSEvent.SNS recordSNS = null;
for ( SNSEvent.SNSRecord snsRecord : snsRecordList ) {
recordSNS = snsRecord.getSNS();
logger.log(
"Subject:[" + recordSNS.getSubject() + "]" +
"Arn:[" + recordSNS.getTopicArn() + "]" +
"attribs:[" + recordSNS.getMessageAttributes() + "]" +
"message:[" + recordSNS.getMessage() + "]" );
}//for
}
timeStamp = sdf.format(new Date());
logger.log("-------------------------Invocation completed: -------------" + timeStamp);
return null;
}
}