AWS Builder Center
AWS Compute Series — Day 2: AWS Lambda

AWS Compute Series — Day 2: AWS Lambda

Understanding Serverless Computing

Series: AWS Compute Series (5 articles)

  1. 2
    AWS Compute Series — Day 2: AWS Lambda This article
DevOps & Software Engineering
In our  previous article , we explored Amazon EC2, a service that allows us to provision and manage virtual servers in the cloud.
But here's another question:
What if you need to execute code without managing a server or keeping one running all the time?
Imagine an application that receives a file upload, processes it, and stores the result.
Do you really need to maintain a virtual server just to execute that operation?
This is where AWS Lambda comes in.

1. What is AWS Lambda?

AWS Lambda is a serverless, event-driven compute service that lets you run code without provisioning or managing servers.
You provide your code, configure its execution environment, and define how it should be invoked.
AWS manages the underlying infrastructure and execution environments.
Your function runs when triggered by an event, and Lambda manages the execution capacity required to process incoming invocations.
Examples:
  • Process an image when it is uploaded to Amazon S3.
  • Execute business logic when an API request arrives.
  • React to changes in a DynamoDB table.
  • Send notifications when a specific event occurs.
  • Run scheduled background tasks.
Important: Serverless does not mean there are no servers. It means you don't manage the underlying servers yourself.

2. How does AWS Lambda work?

To understand Lambda, let's introduce its main building blocks.

A. Function

A Lambda function is the code you want AWS to execute.
It is usually designed to perform a specific task.
For example, a function could validate a payment notification or resize an uploaded image.

B. Runtime

The runtime provides the execution environment for your code.
Lambda offers managed runtimes for supported programming languages, including Python, Node.js, Java and .NET.
You can also use custom runtimes.

C. Handler

The handler is the entry point Lambda invokes when your function runs.
It receives the event and context information, executes your application logic and returns a result when appropriate.
Think of it as the function's starting point.

D. Event

An event contains information about what happened and what your function needs to process.
For example, when someone uploads a file to an S3 bucket, the event can contain information about that object.
Your function receives this information and decides what to do.

E. Trigger

A trigger is the mechanism that causes a function to execute.
Common examples include:
  • Amazon S3 events.
  • Amazon API Gateway requests.
  • Amazon EventBridge rules.
  • Amazon SQS messages.
  • DynamoDB Streams.
The event is the information being processed; the trigger is what initiates the execution.

F. Execution Role

Lambda needs permissions to interact with other AWS services.
An IAM execution role defines what the function is allowed to do.
For example, an image-processing function may need permission to read an object from one S3 bucket and write the processed image to another.
Apply the principle of least privilege: grant only the permissions the function actually needs.

3. What happens when a Lambda function is invoked?

Let's use a practical example.
A customer uploads an image to an Amazon S3 bucket.
The execution flow looks like this:
 S3 Upload → Event Trigger → Lambda Invocation → Image Processing → Store Result 
Step by step:
  1. A customer uploads an image.
  1. Amazon S3 generates an event notification.
  1. The event invokes the Lambda function.
  1. Lambda prepares an execution environment if necessary.
  1. The runtime passes the event to the handler.
  1. Your code processes the image.
  1. The function writes the result to the destination.
  1. The invocation completes.
AWS manages the underlying execution infrastructure.
You focus on the code and its configuration.

4. Understanding the Lambda execution lifecycle

A Lambda function does not necessarily start a completely new environment for every invocation.
Its execution environment has three main phases:
  • Initialization (Init): Lambda prepares the runtime and loads the function.
  • Invocation (Invoke): Lambda executes the handler in response to an event.
  • Shutdown: the execution environment is eventually removed when no longer needed.
Lambda can reuse an existing execution environment for subsequent invocations.
This introduces an important concept: Cold Starts.
A cold start occurs when Lambda needs to prepare a new execution environment before processing an invocation.
This initialization can add latency.
Subsequent invocations may reuse an existing environment and avoid some of that initialization overhead.
For applications where startup latency matters, AWS offers features such as Provisioned Concurrency and SnapStart for supported runtimes and configurations.
One important engineering lesson: never assume that an execution environment will remain available indefinitely.
Persist important application data in appropriate external services rather than relying on local memory or temporary files.

5. How does Lambda scale?

One of Lambda's major advantages is automatic scaling.
Imagine your function normally receives 10 requests per minute.
Suddenly, a marketing campaign generates 1,000 requests.
Lambda can create additional execution environments to process concurrent invocations.
When demand decreases, it can reduce the number of active environments.
This is elasticity without having to manually provision a fleet of servers.
However, scaling is subject to account quotas, concurrency limits and service-specific constraints.
You should also understand:
  • Concurrency: the number of invocations being processed simultaneously.
  • Reserved Concurrency: capacity reserved for a function, also limiting its maximum concurrency.
  • Provisioned Concurrency: execution environments initialized in advance to reduce cold-start latency.
Automatic scaling does not mean unlimited scaling.
Architecture and capacity planning still matter.

6. How is Lambda billed?

Unlike a continuously running EC2 instance, standard Lambda pricing is primarily based on:
  • The number of requests.
  • The execution duration.
  • The configured memory allocation.
In other words, you generally pay for the compute time consumed by your function rather than maintaining a server that remains running between requests.
But remember: Lambda is not the only possible source of cost.
An application may also generate charges from:
  • API Gateway.
  • Amazon S3.
  • Amazon SQS.
  • CloudWatch Logs.
  • Data transfer.
  • Other services used by the function.
Serverless does not automatically mean free or inexpensive.
A poorly designed function, excessive invocations or inefficient processing can still create unexpected costs.
Always review the complete architecture, not just the Lambda pricing.

7. When should you use AWS Lambda?

Lambda is particularly useful when:
  • You need event-driven processing.
  • You have short-running background tasks.
  • You want to build APIs with serverless components.
  • Your workload has variable or unpredictable demand.
  • You want to automate reactions to events across AWS services.
  • You want to reduce the operational burden of managing servers.
However, Lambda is not the best solution for every workload.
Standard Lambda functions have a maximum execution timeout of 15 minutes.
It may also be less suitable when you need:
  • Long-running processes.
  • Persistent local state.
  • Full operating-system control.
  • Specialized server configurations.
  • Workloads that require sustained computing capacity.
For those cases, EC2, containers or other compute services may be more appropriate.

8. EC2 vs Lambda: What's the difference?

Both services provide computing capacity, but they solve different operational problems.

Amazon EC2
AWS Lambda
Virtual server
Function execution
You manage the OS
AWS manages the runtime infrastructure
Instance lifecycle management
Invocation-based execution
Suitable for long-running workloads
Suitable for event-driven tasks
You configure scaling
Automatic execution scaling
Instance-based pricing options
Request and execution-based pricing
Think of it this way:
With EC2, you manage a server that runs your application.
With Lambda, you provide code that AWS executes when needed.
Neither is universally better. The right choice depends on your workload and operational requirements.

9. What about security and reliability?

AWS manages the underlying infrastructure, but you remain responsible for securing your function and its interactions.
Important practices include:
  • Use IAM execution roles with least privilege.
  • Protect secrets instead of hardcoding credentials.
  • Validate incoming events.
  • Monitor executions and errors with Amazon CloudWatch.
  • Configure appropriate timeout and memory settings.
  • Design functions to handle retries safely.
  • Make operations idempotent where possible to avoid duplicate side effects.
  • Protect access to connected resources and databases.
Remember that event-driven systems can retry failed processing depending on the invocation mechanism.
Your application must be designed to handle those behaviors correctly.

10. The practical takeaway

AWS Lambda changes the way we think about computing.
Instead of starting with the question:
"Which server should I provision?"
We can start with:
"What event should trigger this operation, and what code should execute in response?"
That shift is fundamental to event-driven and serverless architectures.
Before moving on, make sure you can explain:
  • What is a Lambda function?
  • What is the difference between an event and a trigger?
  • What role does the handler play?
  • What is an execution role?
  • What causes a cold start?
  • How does Lambda scale?
  • What is the difference between EC2 and Lambda?
Coming next in our Compute journey: AWS Elastic Beanstalk — deploying and managing applications without having to manually manage every infrastructure component.
Learn • Build • Share • Certify


Further reading — AWS official documentation:

Series: AWS Compute Series (5 articles)

  1. 2
    AWS Compute Series — Day 2: AWS Lambda This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article