AWS Lambda

Last Updated : 12 Jan, 2026

Running code on traditional servers can be inefficient. If you need to run a task only occasionally, like resizing an image when a user uploads it paying for a server to run 24/7 is a waste of money and maintenance effort.

AWS Lambda solves this by introducing serverless computing. It runs your code only when needed, scales automatically, and charges you only for the milliseconds used.

The Core Concepts: Serverless & Event-Driven

To understand Lambda, you must understand two key terms:

  • Serverless: This doesn't mean there are no servers; it means you don't manage them. AWS handles provisioning, patching, OS updates, and scaling. You only focus on the code.
  • Event-Driven: Lambda functions sit idle (and cost nothing) until they are triggered by an event.

The Event-Driven Model:

  1. The Trigger (Event Source): Something happens (e.g., a file is uploaded to S3, an HTTP request hits API Gateway).
  2. The Lambda Function: AWS spins up your code to process that specific event.
  3. The Destination: The code performs an action (e.g., saves data to DynamoDB, returns a response)

For example : A new image uploaded to an Amazon S3 bucket (Event Source) triggers a Lambda Function (Your Code) that resizes the image and saves the new version back to another S3 bucket (Downstream Service)

Key Features and Benefits

  • No Servers to Manage (Serverless): This is the core benefit. You can focus entirely on writing business logic, not on managing infrastructure. AWS handles the rest.
  • Pay-Per-Execution Pricing: You are charged only for what you use. Pricing is based on the number of requests for your function and the duration (in milliseconds) it takes for your code to execute. There is no charge when your code is not running.
  • Continuous, Automatic Scaling: Lambda automatically scales your application in response to every trigger. It can scale from a handful of requests per day to thousands per second without any configuration changes.
  • Tight Integration with the AWS Ecosystem: Lambda is the glue that can connect dozens of AWS services. You can trigger functions from services like S3, API Gateway, DynamoDB, SQS, Kinesis, and more.
  • Broad Language Support: You can write Lambda functions in your favourite language. Supported runtimes include Python, Node.js, Java, Go, Ruby, .NET, and you can even bring your own custom runtime.

Example:

  1. Process data from Amazon S3 buckets.
  2. Respond to HTTP requests.
  3. Build serverless applications.

The Anatomy of a Lambda Function

When you write a Lambda function (in Python, Node.js, Java, etc.), you are primarily interacting with three components. Here is a standard Python example:

def lambda_handler(event, context):
# 1. The Handler
print("Received event:", event)

# Your business logic here
message = "Hello, " + event.get("key1", "World")

return {
'statusCode': 200,
'body': message
}
  1. The Handler Function: This is the entry point (lambda_handler above). AWS calls this function when execution starts.
  2. The event Object: This contains the data from the trigger. If triggered by an API, this holds the HTTP request body. If triggered by S3, it holds the filename and bucket details.
  3. The context Object: This provides runtime information, such as how much memory is allocated, the function name, and how much time is left before timeout.

Use Cases of AWS Lambda Functions

Use CaseDescriptionExample
File ProcessingAutomatically process files as they arrive.A user uploads a photo to S3; Lambda triggers instantly to create a thumbnail version.
Web APIsBuild serverless backends.An HTTP request hits API Gateway; Lambda runs the logic to fetch data and return it to the user.
Data StreamsProcess real-time data.IoT sensors send temperature data to Kinesis; Lambda filters and analyzes it in real-time.
Cron JobsScheduled automation.EventBridge triggers a Lambda function every night at 12:00 AM to generate daily reports.

AWS lambda will help you to focus more on your code than the underlying infrastructure. The infrastructure maintenance in AWS was taken care of by AWS lambda.

Working of AWS Lambda Functions

Start off by uploading the code to AWS Lambda. From there, set up the code to trigger from other AWS services, HTTP endpoints, or mobile apps. AWS Lambda will only run the code when it's triggered and will also only use the computing resources needed to run it. The user has to pay only for the compute time used.

AWS (Amazon Web Services) Lambda

Getting Started With AWS Lambda Function

The following are the steps mentioned below to create your own customized AWS lambda functions by using the AWS console. Create an AWS account.

Steps for Creating AWS Lambda Functions Using AWS Console

Step 1: Log in to your AWS console and search for Lambda. As shown in the following image.

Lambda

Step 2: Click on Create function.

Create Function

Step 3: Here we are going to use a sample hello world program by using Author from scratch and configure the details according to your requirement.

Configure the function

Step 4: Successfully our function is created.

Function Created

Pricing: Pay As Per Use

AWS Lambda offers a generous Free Tier that includes 1 Million requests and 400,000 GB-seconds of compute time per month forever.

Beyond the free tier, you are billed on two factors:

  1. Requests: The total number of times your function triggers.
  2. Duration: The time (in milliseconds) your code takes to execute, multiplied by the amount of RAM you allocated to the function.

Note: If your code is inefficient and takes 10 seconds to run, you pay for 10 seconds. If you optimize it to run in 200ms, your bill drops significantly.

Common Use Cases

  • Real-time File Processing: Trigger a function to resize images, transcode videos, or perform OCR on documents as they are uploaded to S3.
  • Serverless Web Backends: Use Amazon API Gateway to create a REST API that triggers Lambda functions to handle HTTP requests for your web or mobile application.
  • Data Processing (ETL): Process and transform real-time data from streams like Amazon Kinesis or DynamoDB Streams.
  • Automation / "Cron in the Cloud": Schedule functions to run at regular intervals using Amazon EventBridge to perform tasks like generating daily reports, cleaning up resources, or running backups.

Monitoring Your AWS Lambda Usage

Monitoring your AWS Lambda usage is crucial for understanding and controlling costs, especially as your workloads scale beyond the AWS Free Tier limits. Here are some ways to effectively track and manage Lambda usage:

  • CloudWatch Metrics: Track invocation counts, execution duration, error rates and memory use. You can set alarms to alert you when usage exceeds specific thresholds​.
  • Cost Explorer: Visualize and analyze your spending patterns by breaking down Lambda expenses. Set budgets and alerts to manage monthly costs effectively​.
  • Usage Reports: Access detailed reports in the AWS Billing Console for monthly usage across AWS services, helpful for teams needing cost allocation across functions​.
  • Third-Party Tools: Use tools like Datadog, Lumigo and New Relic for advanced monitoring and insights, allowing for more detailed performance tracking and cost optimization​.
Comment