Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send AWS S3 Events in CloudWatch to my server using Lambda in Java

I am trying to send a S3 event log to my server outside AWS in case of object level API call is triggered in my bucket using Lambda (upload, download, etc) but when I run it it returns timeout without any error mentioned. I suspect maybe it is because it can't send it to my server using HttpClient but not entirely sure.

Here is my Lambda function:

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.amazonaws.services.s3.event.S3EventNotification;
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;

public class S3EventProcessor implements RequestHandler<S3Event, String> {

    public String handleRequest(S3Event s3event, Context context) {
        S3EventNotificationRecord record = s3event.getRecords().get(0);
        String event = record.toString();
        String url = "http://myexampleserver.com";



HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        StringEntity entity = null;
        try {
            entity = new StringEntity(event);
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        entity.setContentType("text/plain");
        post.setEntity(entity);
        context.getLogger().log("Setting up post request");

        HttpResponse response = null;
        try {
             response = client.execute(post);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        context.getLogger().log(Integer.toString(response.getStatusLine().getStatusCode()));
        String a = "Event: " +  s3event.getRecords().get(0).getRequestParameters().getSourceIPAddress();
        return a;
    }

}

Is my Lambda function correct?

Here is my log and error messsage:

START RequestId: bea1b956-3f9b-11e7-93c8-5503fb925252 Version: $LATEST
Setting up post requestEND RequestId: bea1b956-3f9b-11e7-93c8-5503fb925252
REPORT RequestId: bea1b956-3f9b-11e7-93c8-5503fb925252  Duration: 30001.43 ms   Billed Duration: 30000 ms   Memory Size: 512 MB Max Memory Used: 73 MB  
2017-05-23T09:40:21.181Z bea1b956-3f9b-11e7-93c8-5503fb925252 Task timed out after 30.00 seconds
 "errorMessage": "2017-05-23T09:40:21.181Z bea1b956-3f9b-11e7-93c8-5503fb925252 Task timed out after 30.00 seconds"

Here is my VPC configuration in my Lambda as I follow from here and here: enter image description here

like image 519
Ihsan Haikal Avatar asked Oct 29 '22 07:10

Ihsan Haikal


1 Answers

I am not Execute your lambda Function. But your lambda function looks like right one.

More than you not able to trigger event for download objects.

The following events are applicable

RRSObjectLost, Put, Post, Copy, Complete Multipart Upload, Delete, Delete Marker Created, ObjectCreate (All), ObjectDelete (All).

test your lambda by passing test data S3 put event from lambda console.

like image 172
Mohan Shanmugam Avatar answered Nov 15 '22 04:11

Mohan Shanmugam