CHAPTER 03
Intermediate
AWS Lambda Fundamentals
Updated: May 15, 2026
20 min read
# CHAPTER 3
AWS Lambda Fundamentals
1. Introduction
When people say "Serverless," they are usually talking about AWS Lambda. Introduced in 2014, Lambda single-handedly created the serverless revolution. It remains the most powerful, flexible, and widely adopted Functions as a Service (FaaS) platform in the world. In this chapter, we will master the AWS Lambda dashboard, configure crucial settings like Memory and Timeout, and deploy our very first live cloud function.2. Learning Objectives
By the end of this chapter, you will be able to:- Navigate the AWS Lambda Management Console.
- Configure Lambda Memory and understand its impact on CPU power.
- Configure Timeout settings to prevent runaway billing.
- Understand Synchronous vs. Asynchronous invocation.
- Deploy and test a Node.js Lambda function.
3. Beginner-Friendly Explanation
Imagine a highly customizable vending machine.- The Code (The Candy): You provide the candy (your Node.js or Python code).
- The Memory (The Motor): You decide how strong the vending machine's motor should be. If your code does heavy math, you give it a big motor (1024 MB). If it just says "Hello," you give it a small motor (128 MB).
- The Timeout (The Safety Switch): You tell the machine: "If the candy gets stuck and the motor runs for more than 10 seconds, cut the power." This prevents the machine from running forever and draining your bank account.
4. Configuration: Memory and CPU
In AWS Lambda, you cannot explicitly choose the number of CPU cores. You only configure Memory (RAM), which ranges from 128 MB up to 10,240 MB (10 GB). The Secret: AWS allocates CPU power proportionally to the Memory you choose. If you allocate 256 MB of RAM, your function gets twice the CPU power of a 128 MB function. If your code is running slowly, sometimes doubling the RAM will cut the execution time in half, resulting in the *exact same cost* but a much faster user experience!5. Configuration: Timeouts
By default, an AWS Lambda function times out after 3 seconds. The absolute maximum time a Lambda function is allowed to run is 15 minutes. If your code connects to a database and the database is offline, your code might hang indefinitely. The Timeout setting acts as a circuit breaker, killing the function so you don't pay for 15 minutes of idle waiting.6. Invocation Methods
How does the function run?- 1. Synchronous (Push): The user waits for the answer. Example: A user clicks "Login" on a website. The API Gateway triggers Lambda, Lambda checks the database, and returns "Success" directly to the user's browser.
- 2. Asynchronous (Event): Fire and forget. Example: A user uploads a video. An event triggers Lambda to process the video. The user doesn't wait; Lambda runs in the background and sends an email when it's done.
7. Mini Project: Create Your First Lambda Function
Let's write code that runs in the AWS Cloud.Step-by-Step Tutorial: *(Assumption: You have logged into the AWS Console).*
- 1. Search for Lambda and click Create function.
- 2. Choose Author from scratch.
-
3.
Function name:
MyHelloWorldAPI.
-
4.
Runtime: Select
Node.js 20.x.
-
5.
Architecture:
x86_64(Standard intel).
- 6. Click Create function.
- 7. Scroll down to the Code source editor. You will see this default code:
javascript
-
8.
Change the text to
'Hello from the Cloud!'.
- 9. Click the Deploy button (crucial step, or your changes aren't saved).
- 10. Click the blue Test button.
-
11.
Create a new test event (name it
TestEvent), leave the JSON as default, and click Save.
- 12. Click Test again.
-
13.
A green box will appear. Expand the Execution result. You will see your output:
Hello from the Cloud!You have successfully executed serverless compute!
8. Real-World Scenarios
A media company uses a 15-minute cron job to scrape news websites. They initially built it on a $20/month Virtual Machine. They migrated the code to an AWS Lambda function triggered by an Amazon EventBridge Timer (AWS's serverless cron service). The function wakes up every 15 minutes, scrapes the data in 4 seconds, and goes to sleep. Because 4 seconds is so fast, they fall entirely within the AWS Free Tier, reducing their monthly bill from $20 to $0.00.9. Best Practices
-
Variables Outside the Handler: If your function needs to connect to a database, initialize the connection *outside* the
handlerfunction. Code outside the handler runs during the Cold Start. When the container stays warm, the database connection is preserved, making subsequent invocations significantly faster.
javascript
10. Cost Optimization Tips
- The AWS Free Tier: AWS offers a permanent, non-expiring free tier for Lambda: 1 Million free requests per month and 400,000 GB-seconds of compute time per month. For most learning and personal projects, you will never pay a dime for Lambda.
11. Exercises
- 1. If an AWS Lambda function is executing a CPU-intensive mathematical calculation very slowly, how can you increase its CPU power without rewriting the code?
- 2. Explain the purpose of the Timeout setting. Why is setting it to the 15-minute maximum a dangerous practice for simple API calls?
12. FAQs
Q: Can I run a PHP or Ruby website on Lambda? A: Yes! While Node.js and Python are the most popular, AWS Lambda supports Custom Runtimes, allowing you to run almost any language, including PHP, Go, and Rust.13. Interview Questions
- Q: Explain the relationship between Memory allocation and CPU provisioning within AWS Lambda. How does this relationship impact cost optimization strategies?
- Q: Differentiate between Synchronous and Asynchronous Lambda invocations. Provide an architectural use case for each.