Objective:
This section explores serverless architecture in AWS. We delve into its meaning and the associated services available in AWS, including Lambda (serverless functions) and API Gateway (serverless APIs) as means for building modern, cost-effective applications in the cloud.
Key IT Terminology: Serverless 🧠
Serverless
doesn't mean "no servers" — it just means we don’t manage them. AWS handles all the infrastructure provisioning, scaling, and maintenance of servers behind the scenes. We just focus on writing code or storing data!
Serverless Examples:
DynamoDB
– Fully managed NoSQL databaseAWS Lambda
– Run functions without managing serversAPI Gateway
– Expose APIs to the internet without managing servers
Here's an AWS Serverless overview, if you would like to dive deeper: https://aws.amazon.com/serverless/
AWS Lambda 🔁
AWS Lambda
lets us run code in response to events — without provisioning or managing servers. It supports various languages, including:
- Node.js (JavaScript)
- Python
- Java
- C#
- Ruby
- Custom Runtime APIs
How Lambda works
We upload our code (as a function), and Lambda runs it automatically when triggered by an event (e.g. file upload, API request, database update).
With Lambda, we only pay for:
- Number of invocations
- Execution duration × memory allocated
It is still a server at the end of the day that requires CPU and memory to work efficiently!
For example, let’s say we upload images to an S3 bucket. A Lambda function can automatically resize those images and save them to another bucket — all without a single server setup.
// Sample Node.js Lambda function with Sharp and S3
const AWS = require('aws-sdk');
const sharp = require('sharp');
const s3 = new AWS.S3();
exports.handler = async (event) => {
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key;
const original = await s3.getObject({ Bucket: bucket, Key: key }).promise();
const resizedBuffer = await sharp(original.Body)
.resize(300, 300)
.jpeg({ quality: 80 })
.toBuffer();
const newKey = `resized/${key.replace(/\.[^/.]+$/, '')}-resized.jpg`;
await s3.putObject({
Bucket: 'my-resized-images-bucket',
Key: newKey,
Body: resizedBuffer,
ContentType: 'image/jpeg'
}).promise();
return {
statusCode: 200,
body: JSON.stringify({ message: 'Resized successfully!', original: key, resized: newKey })
};
};
Uploading code to Lambda - 3 Options depending on Complexity
(1) Inline Editor
We can directly write or paste code into the inline editor on the console. This approach is best for small/test functions.
(2) Upload .zip file
We can write code locally in an IDE and compress it into a .zip file (up to 50MB), which we then upload via the AWS console or CLI.
(3) From S3
For larger codebases (>50MB compressed, up to 250MB uncompressed) or CI/CD pipelines, we upload our .zip file to an S3 bucket. Lambda then retrieves and deploys the code directly from S3.
Concurrency: What If Multiple Events Trigger Lambda?
Lambda has what we call concurrency
- it automatically scales, creating multiple instances of our function to handle simultaneous requests.
- 1 event → 1 function instance
- 1000 events → 1000 isolated instances run in parallel
Each event gets its own container, so processing is isolated and doesn’t block others.
💡 For example, if 1000 users uploaded photos simultaneously to our S3 bucket, Lambda automatically spins up 1000 separate instances of our resize function - each processing one image.
AWS API Gateway 🚪
API Gateway
is a serverless service for building, publishing, and managing REST/HTTP API endpoints. It basically acts as the solitary front door for applications to access data or services.
Analogy: Hospital Reception
- Reception Desk = API Gateway
- Patients = Make API requests
Each department = An endpoint
/emergency → A&E
/results → Lab reports
/appointments → Booking system
API Gateway routes traffic, handles authentication, enforces rate limits, and provides monitoring — all without server management.
What Do We Pay For?
Even though API Gateway is serverless, we pay for the infrastructure AWS handles behind the scenes:
- Requests (number of API calls)
- Data Transfer
- Caching (if enabled)
- Custom domain usage
💡 Just like paying reception staff to direct patients efficiently, we pay for API Gateway to route and process traffic.
🎯 TL;DR
- Serverless means AWS manages the servers for us - we just focus on our code.
- Lambda runs our code as functions when triggered by events - we only pay when it runs.
- API Gateway creates and manages our API endpoints without server management.
✨ This is part of a mini-series where I delve into everything cloud-related. Check out my other posts for further learning! ✨
Top comments (0)