scagbackbone
scagbackbone

Reputation: 791

Did I get aws CloudFront right? labmda, s3, cloudfront

I was reading and watching videos about aws lambda, s3 and CloudFront(CF). I'd like to confirm here, that I understand (or not) the concept of aws cloudfront and more generaly CDNs.

aim: I'd like to serve images from my app via CF. I intend to deploy aws lambda function. In scenario that I depict - my lambda function is simple flask app with probably just one endpoint. POST endpoint implementing image upload (Creates checsum from a file, checks if the file checksum is already in s3 - save or not and return image). Image resize, runs only if the arguments for it are provided via query string. All images would be stored (original, resized, rotated etc.). S3 is an origin for CF.

deployment: As I use python - I want to use zappa as a deployment tool.

example PROBLEM:

8:00: User is uploading image from Australia. Image was not there before so it is saved to S3 and returned.

9:00 same user is asking for thumbnail of the image. Image is resized and returned. Here I assume that request for image resize (for example https://app.org/resize/image1.jpeg?width=80&height=80) is saved on Australian edge node of cloudfront together with response (image.jpg 80x80).

10:00 user is asking for the same thumbnail with the same query string - he gets image.jpg 80x80 from Australian cloudfront edge cache.

Did I get it right?

Upvotes: 0

Views: 419

Answers (1)

marekful
marekful

Reputation: 15351

Not exactly. What you describe in the aim paragraph between the first and last sentences is something completely unrelated to CDN and should be handled by your application. (I.e. to keep track what was already uploaded, handle subsequent uploads of the same file differently, only generate thumbnail sizes upon first request, etc.)

CloudFront's sole responsibilities are to check if a requested resource exists in any optimal edge location, serve it if so, otherwise check if it exists in origin, download and copy to edge if so and serve it, otherwise respond with error.

9:00 same user is asking for thumbnail of the image. Image is resized and returned.

Now here you have a problem (if I understood correctly and the image is requested from CF and its origin is a static storage like S3) because the requested thumbnail does not exist and S3 obviously won't be able to generate it. For that to work, the origin must be an application server that is able to process requests from the your CF. Not just serve them, but execute some logic and possibly generate the missing thumbnail before serving to CF.

Or, you should generate all needed versions on an image when it's uploaded and store them all on S3.

Upvotes: 2

Related Questions