Yariv Adam
Yariv Adam

Reputation: 884

How can I trigger a Lambda function when a getObject operation is performed on my S3 bucket?

I'm looking to send a notification to a user when an object (say, a photo they uploaded through my app) is downloaded by another user, as well as update a DynamoDB entry. To do this, I wanted to create an event trigger on my S3 bucket that will fire when a getObject is performed, calling a Lambda that will do the work. Unfortunately, there's no such trigger available - there's a trigger only for putObject (and some other stuff). I'm not sure why.

Anyway, does anyone have an idea how this can be done (other than relying on the app to send a notification)? I would rather this all be designed into the backend as much as possible.

Thanks!

Upvotes: 2

Views: 1629

Answers (2)

Michael - sqlbot
Michael - sqlbot

Reputation: 178956

Unfortunately, there's no such trigger available - there's a trigger only for putObject (and some other stuff). I'm not sure why.

The S3 events are designed for capturing requests that change the bucket contents so you can react to changes immediately. A GET request, of course, doesn't change the bucket.

Triggering an event for GET would potentially mean a lot of work to be done.

Potentially, a good way to accomplish this would be to use CloudFront to access the content in S3 (usually a good idea anyway) and then create a Lambda@Edge Viewer Response trigger.

The trigger would invoke a new Lambda function, and what this new function would do is examine the path of the original request (to find the object key) and verify that the status code of the response was indeed 200, and send a request to the Lambda service to asynchronously invoke your other Lambda function to do the actual tracking and notification work.

Why two Lambda functions? The Lambda@Edge function will block the response until its work is complete, and you have limited resources on the viewer side at the edge -- these triggered functions are designed to be used for fast and relatively unobtrusive hooks. Using a relatively simple function here to invoke the function that does the real work, and invoke that second function asynchronously allows the "real work" function to take as much time as is required, without suspending the response back to the viewer. If that function fails, you still want the viewer to see the image, so an async invocation makes sense.

Note that Lambda@Edge functions are built and tested in us-east-1, but they run in the context of the AWS region that is nearest to the viewer -- so you always need to set the region on the instance of the AWS SDK in the edge function to specify the region where the function that does the actual work will need to be invoked.

Upvotes: 3

Murtaza Kanchwala
Murtaza Kanchwala

Reputation: 2483

I haven't tried this before, But you can engineer this with CloudWatch Logs & CloudWatch Events.

You can log S3 events and can monitor that log group with above two options in Amazon Lambda to Trigger your function.

Log S3 Event Data to CloudWatch

Let me know if this approach works for you.

Upvotes: 0

Related Questions