šŸš€ What is Serverless? Serverless vs Monolith Explained with AWS Lambda

amritadevsamritadevs
3 min read

Learn how serverless architectures differ from traditional monoliths—and get hands-on with AWS Lambda in Node.js.

šŸ“ Introduction

For decades, most web applications were built as monoliths: one big codebase running on servers you managed yourself.
Now, serverless is all the rage—where you write functions, upload them, and let the cloud handle everything else.

In this post, you’ll discover:

  1. What a monolithic architecture looks like

  2. What serverless really means

  3. Key differences between them

  4. A practical AWS Lambda example in Node.js

  5. When to choose serverless over monolith (and vice versa)


šŸ—ļø 1. Monolith Architecture

Definition

A monolith combines all features—UI, business logic, data access—into a single deployable unit.

Typical Stack

  • One codebase (e.g., Express app)

  • One database (e.g., MongoDB)

  • Deployed on VMs or containers (EC2, Docker)

Pros & Cons

ProsCons
Simple to develop & deployHard to scale parts independently
Easy local testingLonger startup times
One unified codebaseLarge codebase → complex changes

ā˜ļø 2. What is Serverless?

Definition

Serverless means you write small, single-purpose functions, deploy them, and the provider auto-scales & manages the servers.

Characteristics

  • Event-driven: e.g., HTTP requests, queue messages

  • Pay-per-use: billed per invocation & duration

  • Auto-scaling: scales down to zero when idle

āš”ļø 3. Monolith vs Serverless Comparison

FeatureMonolithServerless
Deployment unitWhole appIndividual functions
ScalingScale entire appScale each function independently
CostPay for uptime (24/7 servers)Pay only for executions
Cold startsRare (always on)Possible (first invoke latency)
DevOps overheadManage servers, patching, autoscalingMinimal—provider handles infra
Use case best fitComplex, stateful appsMicroservices, event-driven tasks

šŸ› ļø 4. Hands-On: Building an AWS Lambda Function in Node.js

Prerequisites

  • AWS CLI configured

  • Node.js (v14+)

  • SAM CLI (optional)

1. Initialize

mkdir lambda-hello
cd lambda-hello
npm init -y
npm install aws-sdk

2. Write Your Function (index.js)

exports.handler = async (event) => {
  console.log("Event:", event);
  return {
    statusCode: 200,
    body: JSON.stringify({ message: "Hello from AWS Lambda!" })
  };
};

3. Deploy with AWS CLI

zip -r function.zip .
aws lambda create-function \
  --function-name helloServerless \
  --runtime nodejs14.x \
  --handler index.handler \
  --zip-file fileb://function.zip \
  --role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/<YOUR_LAMBDA_ROLE>

4. Test It

aws lambda invoke \
  --function-name helloServerless \
  --payload '{}' response.json
cat response.json

šŸŽÆ 5. When to Use Serverless vs Monolith Choose Monolith when:

  • Choose Monolith when:

    • You need low-latency, always-on services

    • You have complex interdependent modules

    • You prefer simple deployments

  • Choose Serverless when:

    • You build event-driven microservices

    • You want zero-ops for scaling

    • You have unpredictable or spiky traffic

šŸ”‘ Key Takeaways

  • Monolith = one big deployable, full control, but harder to scale per feature

  • Serverless = functions-as-a-service, auto-scale, pay-per-use, but watch out for cold starts

  • AWS Lambda makes it easy to get started—just write your code, upload, and call

0
Subscribe to my newsletter

Read articles from amritadevs directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

amritadevs
amritadevs

Hey there šŸ‘‹I'm a backend developer with 3+ years of experience working with Node.js, Redis, and scalable systems.I write about system design, breaking down complex architectures into simple, real-world examples with HLD, LLD, and working Node.js code.Follow along as I explore how to design systems like chat apps, URL shorteners, notification services, and more — one post at a time šŸš€