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

질문됨 일 년 전1581회 조회
3개 답변
3
수락된 답변
AWS
Nuno_Q
답변함 일 년 전
profile pictureAWS
전문가
검토됨 일 년 전
  • 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
답변함 일 년 전
  • 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.

전문가
답변함 일 년 전
  • 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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠