1

How can I add new trigger for existing AWS Lambda function using Java SDK?

I would like to add S3 trigger.

I have program which converts an image from one format to another. I have two buckets in first, when I add source image in second I want to get result.

Any examples will be appreciated.

Thanks.

Trigger like this:

enter image description here

I try to do it, but it dosn't work:

    final AWSLambda client = AWSLambdaClientBuilder.standard()
                                                   .withCredentials(credentials)
                                                   .build();

  client.listFunctions().getFunctions()
        .stream()
        .filter(f -> f.getFunctionName().equals(FUNCTION_NAME))
        .findFirst()
        .ifPresent(lambda -> {
          final AddPermissionRequest addPermissionRequest = new AddPermissionRequest();
          addPermissionRequest.setStatementId("s3triggerId");
          addPermissionRequest.withSourceArn("arn:aws:s3:::" + INPUT_BUCKET_NAME);
          addPermissionRequest.setAction("lambda:InvokeFunction");
          addPermissionRequest.setPrincipal("events.amazonaws.com");
          addPermissionRequest.setFunctionName(lambda.getFunctionName());

          AddPermissionResult addPermissionResult = client.addPermission(addPermissionRequest);
          System.out.println("Trigger was added to lambda " + addPermissionResult.getStatement());
        });
2
  • I try to do it, but it dosn't work: What's not working? Commented Dec 5, 2020 at 9:30
  • There is no any trigger in my lambda. Commented Dec 7, 2020 at 7:54

2 Answers 2

2

For aws java sdk v2:

You can add trigger by adding notification configuration such:

  • PutBucketNotificationConfiguration

You can see your current configuration via:

  • GetBucketNotificationConfiguration

And check other from: https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Client.html

  1. Init S3Client with region and credentials provider( in my case,Region.US_WEST_2 and ProfileCredentialsProvider respectively).
  2. Choose method( type of action's configuration ) from s3client for your action(in my case putBucketNotificationConfiguration).
  3. Build request for your notification configuration with bucketName and notification configuration.
  4. Build notification configuration: (types: topicConfiguration(SNS),queueConfiguration(SQS), lambdaFunctionConfiguration(Lambda)) in my case lambdaFunctionConfiguration.
  5. Build lambdaFunctionConfiguration with arn and events that will triger your lambda function(in my case, "arn:aws:lambda:us-west-2:12345678912:function:your-lambda" and Event.S3_OBJECT_CREATED_PUT; I assign one event, but your can add more).

Also read: https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html

For this example:

S3lient s3Client = S3Client.builder()
                .region(Region.US_WEST_2)
                .credentialsProvider(ProfileCredentialsProvider.create())
                .build();

s3Client.putBucketNotificationConfiguration(PutBucketNotificationConfigurationRequest.builder()
                .bucket(BUCKET_NAME)
                .notificationConfiguration(NotificationConfiguration.builder()
                        .lambdaFunctionConfigurations(LambdaFunctionConfiguration.builder()
                                .lambdaFunctionArn("arn:aws:lambda:us-west-2:12345678912:function:your-lambda")
                                .events(Event.S3_OBJECT_CREATED_PUT)
                                .build())
                        .build())
                .build());
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but I use sdk v1, I try to do it, but it dosn't work:
BucketNotificationConfiguration notificationConfiguration = new BucketNotificationConfiguration(); notificationConfiguration.addConfiguration("triggerConfig", new LambdaConfiguration("arn:aws:lambda:us-west-2:658481763507:function:NAME", EnumSet.of(S3Event.ObjectCreatedByPut))); SetBucketNotificationConfigurationRequest request = new SetBucketNotificationConfigurationRequest( INPUT_BUCKET_NAME, notificationConfiguration); s3Client.setBucketNotificationConfiguration(request);
Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: Unable to validate the following destination configurations (Service: Amazon S3; Status Code: 400; Error Code: InvalidArgument; Request ID: 0WFR4Q9J8T8H8Q8R; S3 Extended Request ID: FtVPubXrU4+0om3mB6OLNY5h+zPGel1kT4cY+xEygaUR+agc1Hcsu8Tcy3s9WTuTOSlk+xj/zs0=; Proxy: null), S3 Extended Request ID: FtVPubXrU4+0om3mB6OLNY5h+zPGel1kT4cY+xEygaUR+agc1Hcsu8Tcy3s9WTuTOSlk+xj/zs0=
I set sdk v2 and try to use your version and I got this error:
Exception in thread "main" software.amazon.awssdk.services.s3.model.S3Exception: Unable to validate the following destination configurations (Service: S3, Status Code: 400, Request ID: 04B1E6F5D080E65F, Extended Request ID: rmjA+uzB/h+lusPzSMcUgo/zp3mPB78jfsFGfbsL3fqqMqp+fFT2hYLE/Zceedlczd38faZVchs=)
|
0

You can do it either in the console or via SAM.

3 Comments

I want to use only java SDK
What is the problem? There is a Java example as well. docs.aws.amazon.com/lambda/latest/dg/…
It isn't what I want. I want trigger like in picture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.