A Middle Way Between Authenticity and Forgery

One thing that has helped me in thinking about Pauline authorship in the New Testament epistles is that while we may think about someone writing in another person’s name today as forgery and the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Deploying Next.js on AWS Lambda

Belong is a company with a simple vision — “Help folks discover where they Belong”. One way in which we help companies be discovered by these folks is by providing talent branding and engagement solutions.

Some of these solutions involved building microsites with about 4–5 pages. Since, Belong caters to many customers, we quickly realised that we will end up having many such microsites with a lot of things in common technically. So, we started discussing how do we set up an efficient development & deployment pipeline.

As developers working on these projects, we wanted to utilise a stack that checked the following:

3. Great development experience: For us the following comprise great experiences:

4. Isolation & Reliability: Given that these sites represent our client’s brand, we need a setup that is highly reliable. Nobody wants 2AM pagers on downtime/degraded performance. Additionally, a bug in one client site should not affect other clients.

5. Maintainability: Every client has 2 site instances, staging and production. Thus, need a scalable process to maintain these sites without having to reconfigure each server independently.

6. Low expenses: Need to optimise costs for nearly 2*(no. of clients) instances. Each of these clients would have different workloads based on their talent brand and hiring needs. Needed a setup where manual tuning of each client instance is avoided.

Repository structure

We have a single repository to maintain all the microsites with each customer having a separate branch.

The master branch serves as a template for the microsites. When a new customer signs up, a new branch is forked from master and the necessary changes are made to the serverless.yml file (Example: the domain name that maps to the API gateway endpoint). The customer specific assets are also checked into this branch.

The serverless config accepts a bunch of environment variables and parameters that help gain control over which resources are deployed.

What is the deployment like?

This greatly reduces the complexity that comes with deploying a plethora of microsites while also keeping the costs down. Consider a scenario where we have to deploy microsites for 4 customers while maintaining isolation. Traditionally, we would have had to spin up 8 instances which would have cost us north of $160(assuming we pick the smallest t2.small instance type with no hardware abstraction using Kubernetes and the likes).

With serverless, given the fact that the number of requests most of these microssites handle is pretty low, the cost adds up to only $10! That is a huge cost saving that would scale impressively as the number of microsites go up.

Note: In scenarios where you plan to handle a million requests/day and above, it would be economical to deploy the application on traditional servers/containers as opposed to using the serverless architecture.

While the setup works seamlessly now, the path to get here was not all that straightforward. As we started to experiment with setting up the Next.js application on AWS Lambda, we stumbled upon a set of challenges with serving static files.

In retrospect, we would not recommend serving static files via Lambda for two reasons:

Which is why we had to include the step in the CI pipeline that would push the static files to S3 as part of the deployment process and made the path available to the application via the Lambda environment variables.

If you still want to serve the static files via Lambda (cause you are a rebel 😉), make sure you whitelist the correct mime-types in the API gateway configuration.

Testing serverless locally

DIY

Along with these learnings we also wish to share a simple starter kit on Github, which can aid you to evaluate this architecture/setup!

The starter kit will:

Challenges ahead:

About Us:

I am a frontend engineer working with Product teams at Belong. I love building web apps with JS and then regretting why I built them with JS 😅! If not coding, you will find me playing FIFA ⚽️, reading history 📖 or learning cartooning 🎨 !!

I like complicating things. 💥

Add a comment

Related posts:

How Much Should You Like Your Swinging Partners?

This is one of our most enduring debates with close friends in the swinging lifestyle — how much should you ‘like’ the couple you are planning to play with? My wife and I have always had a pretty…

My Artist Way. Hiatus Post.

I have been doing a bit of reflection during this period, and for those who follow my content, truly sorry for the hiatus. I have taken my time the past two weeks to work on more content and catching…

Commit for One Month to One Goal and See Your Life Change

Neither is making a commitment a risky endeavor that you should avoid at all costs. However, many people equate the word “committing” with things like relationships, phobias, procrastination, and…