lowcrawler
lowcrawler

Reputation: 7549

Handle an S3 'miss' with a lambda using serverless framework

Noting the architecture design (taken from this deprecated AWS documentation page: https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/)

Planned Architecture

With each step described:

  1. A user requests a resized asset from an S3 bucket through its static website hosting endpoint. The bucket has a routing rule configured to redirect to the resize API any request for an object that cannot be found.
  2. Because the resized asset does not exist in the bucket, the request is temporarily redirected to the resize API method.
  3. The user’s browser follows the redirect and requests the resize operation via API Gateway.
  4. The API Gateway method is configured to trigger a Lambda function to serve the request.
  5. The Lambda function downloads the original image from the S3 bucket, resizes it, and uploads the resized image back into the bucket as the originally requested key.
  6. When the Lambda function completes, API Gateway permanently redirects the user to the file stored in S3.
  7. The user’s browser requests the now-available resized image from the S3 bucket. Subsequent requests from this and other users will be served directly from S3 and bypass the resize operation. If the resized image is deleted in the future, the above process repeats and the resized image is re-created and replaced into the S3 bucket.

Steps 3-7 feel somewhat straight forward... but how do you get an S3 bucket to configure a routing rule to redirect upon a 'missing object'?

Specifically, this neesd to be done in serverless framework.

In theory, an updated version of this concept is laid out in the cloudformation template here: https://docs.aws.amazon.com/solutions/latest/serverless-image-handler/template.html but I'm not seeing any code in that template that configures an S3 bucket. I follow deeper to their gitlab repo and it seems they are deploying with the aws-sdk? https://github.com/aws-solutions/serverless-image-handler/blob/main/source/custom-resource/index.ts

Upvotes: 1

Views: 204

Answers (1)

Allan Chua
Allan Chua

Reputation: 10175

It seems that you can configure a redirect on S3 itself. Here is a link that shares at least 3 steps to do this.

To configure redirection rules for a static website. To add redirection rules for a bucket that already has static website hosting enabled, follow these steps.

  1. Open the Amazon S3 console at https://console.aws.amazon.com/s3/.

  2. In the Buckets list, choose the name of a bucket that you have configured as a static website.

  3. Choose Properties.

  4. Under Static website hosting, choose Edit.

  5. In Redirection rules box, enter your redirection rules in JSON.

  6. In the S3 console you describe the rules using JSON. For JSON examples, see Redirection rules examples. Amazon S3 has a limitation of 50 routing rules per website configuration.

Recommending the layering of your automation by separating provisioning and application automation by using Terraform or/and Cloud Formation for this.

Upvotes: 1

Related Questions