AWS S3 Event notification using Lambda function in Java

NGR :

I am trying to use Lambda function for S3 Put event notification. My Lambda function should be called once I put/add any new JSON file in my S3 bucket. The challenge I have is there are not enough documents for this to implement such Lambda function in Java. Most of doc I found are for Node.js

I want, my Lambda function should be called and then inside that Lambda function, I want to consume that added json and then send that JSON to AWS ES Service.

But what all classes I should use for this? Anyone has any idea about this? S3 abd ES are all setup and running. The auto generated code for lambda is `

@Override
public Object handleRequest(S3Event input, Context context) {
    context.getLogger().log("Input: " + input);

    // TODO: implement your handler
    return null;
}

What next??

NGR :

Finally, here are the steps for S3 --> Lambda --> ES integration using Java.

  1. Have your S3, Lamba and ES created on AWS. Steps are here.
  2. Use below Java code in your lambda function to fetch a newly added object in S3 and send it to ES service.

    public Object handleRequest(S3Event input, Context context) {
    AmazonS3Client s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());        
    
    for (S3EventNotificationRecord record : input.getRecords()) {
        String s3Key = record.getS3().getObject().getKey();
        String s3Bucket = record.getS3().getBucket().getName();
        context.getLogger().log("found id: " + s3Bucket+" "+s3Key);
        // retrieve s3 object
        S3Object object = s3Client.getObject(new GetObjectRequest(s3Bucket, s3Key));
        InputStream objectData = object.getObjectContent();
    
        //Start putting your objects in AWS ES Service
        String esInput = "Build your JSON string here using S3 objectData";
    
        HttpClient httpClient = new DefaultHttpClient();
    
        HttpPut putRequest = new HttpPut(AWS_ES_ENDPOINT + "/{Index_name}/{product_name}/{unique_id}" );
    
        StringEntity input = new StringEntity(esInput);
        input.setContentType("application/json");
        putRequest.setEntity(input);
    
        httpClient.execute(putRequest);
        httpClient.getConnectionManager().shutdown();
    
    }        
    return "success";}
    
  3. Use either Postman or Sense to create Actual index & corresponding mapping in ES.

  4. Once done, download and run proxy.js on your machine. Make sure you setup ES Security steps suggested in this post

  5. Test setup and Kibana by running http://localhost:9200/_plugin/kibana/ URL from your machine.

  6. All is set. Go ahead and set your dashboard in Kibana. Test it by adding new objects in your S3 bucket

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=456203&siteId=1