05. AWS Lambda Overview
AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. It executes your code in response to events such as API calls, file uploads, or scheduled jobs.
๐งฉ EC2 vs Lambdaโ
| Feature | EC2 | AWS Lambda |
|---|---|---|
| Type | Virtual servers in the cloud | Virtual functions โ no servers to manage |
| Execution | Continuously running | Run on-demand |
| Limits | Bounded by RAM & CPU | Bounded by execution time |
| Scaling | Manual / Auto Scaling setup | Fully automated scaling |
| Billing | Pay for uptime (even if idle) | Pay only per request & duration |
๐ก Why AWS Lambda?โ
- ๐งฑ No Server Management โ AWS handles provisioning and scaling.
- โ๏ธ Automatic Scaling โ Scales instantly from 1 request/day to thousands/second.
- ๐ Event-Driven โ Runs only when triggered by an event (e.g., S3 upload, API call).
- ๐ฐ Simple Pricing โ Pay per request and per compute time.
- ๐งฐ Fully Integrated โ Works with other AWS services like S3, DynamoDB, API Gateway, CloudWatch.
- ๐ Easy Monitoring โ Integrated with Amazon CloudWatch.
- ๐ Flexible Resources โ Up to 10GB RAM per function, with proportional CPU & network improvements.
๐ง Example Use Casesโ
๐ผ๏ธ Serverless Thumbnail Creationโ
| Step | Service | Action |
|---|---|---|
| 1 | Amazon S3 | User uploads an image |
| 2 | AWS Lambda | Triggered by S3 upload event |
| 3 | Lambda Function | Generates thumbnail & metadata |
| 4 | Amazon S3 / DynamoDB | Stores thumbnail or metadata |

โ Fully serverless โ no servers for S3, Lambda, or DynamoDB.
โฐ Serverless CRON Jobsโ
Use CloudWatch Events / EventBridge to trigger a Lambda function on a schedule (e.g., every hour or every day).
This replaces traditional Linux CRON jobs โ completely serverless and automated.
๐งโ๐ป AWS Lambda Language Supportโ
Supported Languages:
- Node.js, Python, Java, C# (.NET Core / PowerShell), Ruby, Go,
- Custom Runtime (via API โ e.g., Rust, Golang)
- Container Image (with Lambda Runtime API)
๐ข For exam purposes: Use ECS or Fargate for running general Docker containers, even though Lambda supports them.
๐ต AWS Lambda Pricingโ
AWS Lambda charges you for how often your function runs (requests) and how long it runs (duration).
You get a free tier every month, and billing is extremely precise โ per millisecond.
โ๏ธ Pricing Breakdownโ
| Pricing Metric | Details |
|---|---|
| Requests | 1M requests/month free, then $0.20 per 1M requests |
| Duration (GB-seconds) | 400,000 GB-seconds/month free |
| Example | 400,000 sec @ 1GB RAM OR 3,200,000 sec @ 128MB RAM |
| After Free Tier | $1 per 600,000 GB-seconds |
| Billing Granularity | Per 1 millisecond |
๐ง How It Worksโ
- Requests: Every time your function runs = 1 request.
- Duration: Based on execution time ร memory size (GB-seconds).
- GB-second:
- 1 second at 1GB = 1 GB-second
- 1 second at 128MB = 0.125 GB-seconds
๐ฐ Exampleโ
If your function runs 2 million times, uses 128 MB memory, and each run takes 1 second:
- Requests: 1M free โ 1M paid = $0.20
- Duration: 2M ร 1 sec ร 0.125 GB = 250,000 GB-sec โ within free tier
โ Total Cost = $0.20
๐ก Lambda pricing = pay per call + pay per duration
It โs highly cost-effective for event-driven, short, and scalable workloads.
๐งพ Summaryโ
- Serverless compute platform for running code in response to events.
- No servers, auto-scaling, event-driven, and cost-efficient.
- Pay only for what you use, with a generous free tier.
- Ideal for: automation scripts, backend APIs, data processing, image processing, and scheduled tasks.
5.1. Creating an AWS Lambda Function
This guide explains step-by-step how to create, configure, test, and monitor an AWS Lambda function using the AWS Management Console.
๐งฑ Step 1: Open the Lambda Consoleโ
- Sign in to the AWS Management Console.
- Search for Lambda in the search bar and open the AWS Lambda service.
- Click Create function.
โ๏ธ Step 2: Choose a Creation Methodโ
Select Author from scratch (you can also use a Blueprint, Container image, or upload existing code later).
- Function name:
HelloLambda - Runtime: Python 3.x (or Node.js, Java, etc.)
- Architecture: x86_64 (default)
- Permissions: Create a new role with basic Lambda permissions
๐ Step 3: Set Permissionsโ
- Lambda requires an IAM role to execute.
- Choose Create a new role with basic Lambda permissions.
- This role allows your Lambda to write logs to CloudWatch.
You can later modify this role to grant access to:
- Amazon S3
- DynamoDB
- SNS / SQS
- Other AWS services
๐งโ๐ป Step 4: Add Your Function Codeโ
AWS automatically creates a sample Hello World function. Example (Python):
def lambda_handler(event, context):
# Get the 'name' value from the event input
name = event.get('name', 'Guest')
# Log a message (goes to CloudWatch)
print(f"Hello {name}, this is a simple Lambda test!")
# Return a greeting message
return {
"message": f"Hello {name}, welcome to AWS Lambda!"
}
Key points:
- Handler:
lambda_handlerโ main entry point. - Event: carries input data in JSON format.
- Context: contains runtime info (timeout, memory, etc.).
๐พ Step 5: Deploy the Functionโ
After editing or verifying the code:
- Click Deploy to save your Lambda code.
- Your function is now ready to invoke manually or via event triggers.

๐งช Step 6: Test the Functionโ
- Click Test (top-right).
- Choose Configure test event.
- Use sample event data:
{
"name": "Himanshu"
}
- Name the event (e.g.,
Test). - Click Save and Test.
Expected output: Hello Himanshu, welcome to AWS Lambda!
๐ง Step 7: Understand Function Resultsโ
- Execution results appear in the console's Execution results panel.
- Context and logs available for debugging.
Common metrics to check:
- Invocation time
- Memory usage
- Execution duration

๐ Step 8: Monitor Function Activityโ
Go to the Monitor tab to view:
- Invocation count
- Duration
- Errors
- Throttles
Click View logs in CloudWatch to see detailed logs like:

โ๏ธ Step 9: Configure Function Settingsโ
You can customize:
- Memory (MB): allocate between 128 MB โ 10 GB
- Timeout: max execution time before Lambda stops
- Ephemeral storage: temporary space (default 512 MB, max 10 GB)
- Environment variables: define configuration parameters
- VPC: connect Lambda to private networks

๐ Step 10: Add Event Triggersโ
Connect Lambda to various event sources:
- Amazon S3 (on file uploads)
- DynamoDB Streams
- API Gateway
- EventBridge
- SNS / SQS
- Kinesis Data Streams
Example flow: S3 โ PutObject โ triggers Lambda โ Lambda processes file/image

๐ Step 11: IAM Role and Permissionsโ
- Go to Configuration โ Permissions.
- Click the role name to open it in the IAM Console.
- Review attached policy, e.g.,
AWSLambdaBasicExecutionRoleโ grants CloudWatch logging permissions. - Add more permissions as required (e.g., S3 access).

โ Step 12: Summaryโ
You have successfully:
- Created a Lambda function (
HelloWorld) - Deployed and tested it
- Viewed CloudWatch logs and metrics
- Learned how to add triggers and manage permissions
AWS Lambda lets you run event-driven, auto-scaling, pay-per-use code without managing servers.