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

I try to send an S3 event log to my server outside AWS in case I call the object level API in my bucket using Lambda (load, load, etc.), but when I run it, it returns a timeout without any errors, I suspect, perhaps this is because he cannot send it to my server using HttpClient, but is 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 are my messages and error messages:

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

+4
2

. - .

, .

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

, . S3 -.

+1

, Lambda 5 . :

  • , , " 1" .
  • :

    • -, e.printStackTrace(); , lambda.
    • - Apache HttpClient, , HTTP- 5 .

    , , ​​ SocketTimeoutException, Lambda -.

+1

Source: https://habr.com/ru/post/1677236/


All Articles