CHAPTER 15
Beginner
AWS Lambda Serverless Computing
Updated: May 15, 2026
25 min read
# CHAPTER 15
AWS Lambda Serverless Computing
1. Introduction
Historically, if you wrote a small Python script to resize images, you had to launch an entire EC2 Linux instance, install Python, keep the server running 24/7, and pay for the server even when no one was uploading images. This is wildly inefficient. AWS revolutionized cloud computing by introducing AWS Lambda—a "Serverless" compute service that allows you to run code without provisioning or managing any servers at all. In this chapter, we will explore Event-Driven Architecture and write our first Serverless function.2. Learning Objectives
By the end of this chapter, you will be able to:- Define "Serverless Computing".
- Understand how AWS Lambda executes code in response to Events.
- Identify common triggers for Lambda functions.
- Understand the Lambda pricing model (paying by the millisecond).
- Create and execute a simple Lambda function in the AWS Console.
3. Beginner-Friendly Explanation
Imagine a traditional taxi vs. a teleporting chauffeur.- EC2 (Traditional): You hire a taxi driver for the whole day. Even if you stay inside a mall for 5 hours, the taxi waits outside, the engine runs, and you pay him by the hour.
- Lambda (Serverless): You step outside the mall and snap your fingers. A chauffeur instantly teleports into existence, drives you to your destination, drops you off, and instantly vanishes. You are billed precisely for the 4 minutes and 12 seconds the drive took. If you don't snap your fingers for a month, your bill is $0.00.
AWS Lambda functions are invisible snippets of code that sleep until an Event wakes them up. They execute their logic, and then instantly vanish.
4. Event-Driven Architecture
Lambda functions do not run continuously. They are Triggered by events in the AWS ecosystem.Common Triggers:
- Amazon S3: A user uploads a high-resolution photo to an S3 bucket. This "upload event" instantly triggers a Lambda function that compresses the photo and saves it back to S3.
- API Gateway: A user visits a website and requests data. API Gateway catches the HTTP request and triggers a Lambda function to fetch data from DynamoDB.
- EventBridge (Cron): A timer triggers the Lambda function every day at midnight to run an automated database backup script.
5. Supported Languages
You can write Lambda functions natively in:- Node.js (JavaScript/TypeScript)
- Python
- Java
- Go
- C# / .NET
- Ruby
6. The Pricing Model (Micro-Billing)
With Lambda, you pay ONLY for compute time. You are billed based on the number of requests and the duration of the execution, calculated down to the 1 millisecond. AWS provides a massive Free Tier: 1 Million free Lambda requests per month, forever! Because of this, many startups build their entire backend infrastructure on Lambda and pay literally $0.00 a month for compute.7. Mini Project: Create a Lambda Function
Let's write a simple serverless function in Python.Step-by-Step Tutorial:
- 1. Open the AWS Console and search for Lambda.
- 2. Click Create function.
- 3. Select Author from scratch.
-
4.
Function name:
HelloCloudFunction.
- 5. Runtime: Select Python 3.12.
- 6. Click Create function.
-
7.
Scroll down to the Code source editor. You will see a default
lambda_handlerfunction. Replace it with this code:
python
- 8. Click Deploy to save the code.
-
9.
Click the Test button. Name the test event
Test1.
-
10.
In the Event JSON box, enter:
{"name": "Alice"}. Save it.
- 11. Click Test again.
-
12.
Look at the Execution Results! You will see the successful response:
"Hello, Alice! Your Serverless code is running!"Notice the "Billed Duration" is likely under 2 milliseconds!
8. Best Practices
- Stateless Design: Lambda functions are fundamentally stateless. If Function Execution #1 saves a temporary file to local disk, Function Execution #2 will likely run on a completely different invisible server and cannot see that file. Any persistent data MUST be saved to an external database (like DynamoDB) or S3.
9. Common Mistakes
- The Cold Start Problem: If a Lambda function hasn't been triggered in 15 minutes, AWS puts it in "deep sleep." When a user triggers it, AWS has to fetch the code, provision a micro-container, and boot the runtime environment. This can cause the first execution to take 1 or 2 seconds (a "Cold Start"). All subsequent executions happen in milliseconds. (If 1 second of lag is unacceptable for your architecture, you must use "Provisioned Concurrency" to keep functions warm).
10. Exercises
- 1. Define the pricing model of AWS Lambda compared to Amazon EC2.
- 2. List three AWS services that can act as "Triggers" to execute a Lambda function.
11. MCQs with Answers
Question 1
Which AWS compute service is fundamentally designed to execute code in response to events without requiring the user to provision, manage, or maintain the underlying operating system?
Question 2
In an Event-Driven Architecture, which of the following scenarios is a perfect use case for AWS Lambda?
12. Interview Questions
- Q: Explain the concept of "Serverless" computing. Does serverless mean there are no servers physically involved?
- Q: Describe the "Cold Start" phenomenon in AWS Lambda. How does it impact application latency, and what architectures are most sensitive to it?