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

已提问 1 年前1580 查看次数
3 回答
3
已接受的回答
AWS
Nuno_Q
已回答 1 年前
profile pictureAWS
专家
已审核 1 年前
  • 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
已回答 1 年前
  • 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.

专家
已回答 1 年前
  • 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.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则