Can I trigger a Lambda function when a file is not found on S3?

0

I'm hosting images on S3 for my website. When an image is requested and not found, I'd like to be able to trigger a lambda function (or something else if Lambda isn't the right thing). The lambda function itself seems straight forward, but I just can't figure out a trigger for when a file is not found.

The details of why I'm trying to do this:

We have multi environments for the website and we save images in a structure based on the env (ex: https://amoncarterweb.s3.amazonaws.com/website/[WEBENVIRONMENT]/styles/s3/screenshot.png). We are saving hundreds of GBs of images so it is a bit impractical to copy that dir for a dev environment when it might only be there for a short while. What I would like to do is if an image is requested and not found, trigger a lambda function to check if we are requesting an asset not on LIVE, then check if it lives in the LIVE environment and if so copy it to the other environment.

Any thoughts or advice would be appreciated, thanks

gefragt vor einem Jahr1581 Aufrufe
3 Antworten
3
Akzeptierte Antwort
AWS
Nuno_Q
beantwortet vor einem Jahr
profile pictureAWS
EXPERTE
überprüft vor einem Jahr
  • Thanks for this response. Lambda@Edge is something I'm not familiar with, so I will def look into this!

  • This def seemed to be the way to go since I think we really should have CloudFront in front of the S3 bucket.

2

If you enable cloudtrail data events for your S3 bucket you will very easily generate event for objects that do not exist. Once you have the trail defined you can create an Event Bridge alarm for all trails that contain the following value. "errorCode": "NoSuchKey" This error is written to the trail every time there is a GetObject API call for a Key (In your case an image) that doesn't exist. You can then launch a Lambda as a response to the event.

AWS
beantwortet vor einem Jahr
  • Thanks for this response. Another thing to look into. I figured there was probably more than 1 way to handle this.

  • Thanks again for this response. I tried it out but as someone very new to AWS, there just seemed to be a ton of steps I wasn't sure about and ultimately, I couldn't get this working. I'm sure if I spent more time and asked a few more questions, I could and this would be a totally viable answer, However the one before seemed a bit better for my needs and abilities.

0

Rather than referencing your images in S3 directly, you might be better off front-ending your images with a Lambda function that simply returns them if found, but can handle the "not found" scenario too.

EXPERTE
beantwortet vor einem Jahr
  • Overall, I like this idea. I'm using Drupal and the provided modules for hooking up the s3 bucket so this might add slightly more debt to the project. But something I'll look into so I understand it batter. Thanks for your response.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen