Skip to main content

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โ€‹

FeatureEC2AWS Lambda
TypeVirtual servers in the cloudVirtual functions โ€“ no servers to manage
ExecutionContinuously runningRun on-demand
LimitsBounded by RAM & CPUBounded by execution time
ScalingManual / Auto Scaling setupFully automated scaling
BillingPay 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โ€‹

StepServiceAction
1Amazon S3User uploads an image
2AWS LambdaTriggered by S3 upload event
3Lambda FunctionGenerates thumbnail & metadata
4Amazon S3 / DynamoDBStores thumbnail or metadata
IAM Roles Example

โœ… 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 MetricDetails
Requests1M requests/month free, then $0.20 per 1M requests
Duration (GB-seconds)400,000 GB-seconds/month free
Example400,000 sec @ 1GB RAM OR 3,200,000 sec @ 128MB RAM
After Free Tier$1 per 600,000 GB-seconds
Billing GranularityPer 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โ€‹

  1. Sign in to the AWS Management Console.
  2. Search for Lambda in the search bar and open the AWS Lambda service.
  3. 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.
IAM Roles Example

๐Ÿงช Step 6: Test the Functionโ€‹

  1. Click Test (top-right).
  2. Choose Configure test event.
  3. Use sample event data:
{
"name": "Himanshu"
}
  1. Name the event (e.g., Test).
  2. 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
IAM Roles Example

๐Ÿ“Š 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:

IAM Roles Example

โš™๏ธ 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
IAM Roles Example

๐Ÿ”— 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

IAM Roles Example

๐Ÿ” Step 11: IAM Role and Permissionsโ€‹

  1. Go to Configuration โ†’ Permissions.
  2. Click the role name to open it in the IAM Console.
  3. Review attached policy, e.g., AWSLambdaBasicExecutionRole โ€” grants CloudWatch logging permissions.
  4. Add more permissions as required (e.g., S3 access).
IAM Roles Example

โœ… 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.