s3 event is not triggering AWS Lambda function while uploading jar file from bamboo build plan.
And if I upload Jar manually in s3 Bucket then S3Event is triggering AWS Lambda function correctly. Below are the permission Bucket.
Below is my LambdaFuncton code:
package com.lambda;
import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.model.UpdateFunctionCodeRequest;
import com.amazonaws.services.lambda.model.UpdateFunctionCodeResult;
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.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.nio.ByteBuffer;
public class TestLambdaS3Event implements RequestHandler<S3Event, String> {
private AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();
AWSLambda s3EventClient = AWSLambdaClientBuilder.standard().build();
public TestLambdaS3Event() {
}
@Override
public String handleRequest(S3Event event, Context context) {
context.getLogger().log("Received event: " + event);
// Get the object from the event and show its content type
String bucket = event.getRecords().get(0).getS3().getBucket().getName();
String key = event.getRecords().get(0).getS3().getObject().getKey();
try {
S3Object response = s3.getObject(new GetObjectRequest(bucket, key));
String contentType = response.getObjectMetadata().getContentType();
byte[] byteArray = IOUtils.toByteArray(response.getObjectContent());
UpdateFunctionCodeRequest request = new UpdateFunctionCodeRequest().withFunctionName("my_function")
.withZipFile(ByteBuffer.wrap(byteArray));
System.out.println("request## " + request);
UpdateFunctionCodeResult responseUpdate = s3EventClient.updateFunctionCode(request);
System.out.println("responseUpdate## "+responseUpdate);
return contentType;
} catch (IOException e) {
e.printStackTrace();
context.getLogger().log(String.format("Error getting object %s from bucket %s. Make sure they exist and"
+ " your bucket is in the same region as this function.", bucket, key));
return e.toString();
}
}
}