Nithin Kumar Biliya
Nithin Kumar Biliya

Reputation: 3011

Is it possible to use AWS lambda for routing traffic from ELB to S3 bucket?

Current AWS configuration which is serving 3 webapps -

enter image description here

Here ELB is taking care of SSL offloading. Currently the Node JS application is deployed on a EC2 machine. It is taking care of handling the backend APIs as well as serving the static files for the 3 SPA react webapps.

We are looking to improve on this by separating out the frontend and backend. We are thinking of moving the 3 SPA react webapps into a single S3 bucket. Each will sit in its own directory on this S3 bucket. The S3 bucket will be configured for static website hosting. We will still have the node JS instance for service backend APIs.

enter image description here

We are looking to avoid using cloud front to route traffic from ELB to S3 since these are internal webapps with limited number of users from a particular region.

  1. Can we use lambda function to route traffic from ELB to Node JS app on EC2 as well as from ELB to the S3 bucket? If possible is it a good practice performance wise and cost wise to take this approach?

Also the lambda function has to route the traffic based on rules -

Upvotes: 0

Views: 1285

Answers (2)

Parsifal
Parsifal

Reputation: 4516

Yes, you can implement a Lambda that retrieves content from S3. You simply need to extract the request path from the invocation event, translate that to whatever S3 location you're using, retrieve the file, and package it in the response.

BUT

You're limited to 1MB response data, so if you have any images or other large files, you won't be able to serve them (doc).

And you'll be paying Lambda invocation costs for every file that you retrieve, which is admittedly small.

And you'll be increasing the time to retrieve each file, which will slow down your web-app. How much depends on how many static assets you're loading.

A better solution, if you want to split static and dynamic content and don't want to use CloudFront, would be to spin up another Node.JS server on a different port, and have the ALB point at it.

Upvotes: 3

Chris Williams
Chris Williams

Reputation: 35238

Application load balancers already support path based routing. But won't support the S3 bucket as a valid target, so you'd want a Lambda for that to echo the contents.

So

  • /api/* --> target group with the NodeJS app running behind it
  • /ui/site* --> lambda, echoing the contents of the S3 object

Upvotes: 0

Related Questions