AWS Lambda
Run code on events
Runs code functions when events happen (API requests, new files, messages…) without managing servers; you pay per invocation and run time.
- Cost$ Low
- Difficulty●●●●●
- Costs money when idle?No
- Serverless
- In examsCLF · SAA · DVA · SOA
Think of it asLike taking a taxi: you only pay for the ride, not for owning a car.
Where does Lambda fit?
Calls Lambda
A real example, step by step
- A user uploads a profile picture to your app's S3 bucket.
- S3 emits a “new file” event and invokes a Lambda function — no server is sitting there waiting.
- Lambda starts, reads the image, shrinks it to 200×200 and saves it to the thumbnails/ folder in S3.
- The function runs for about 300 ms and stops. You pay for exactly those 300 ms; if nobody uploads all day, it costs almost nothing.
- On a day when 10,000 people upload at once, Lambda runs many copies in parallel (up to your account's concurrency limit) without you adding servers.
When to use it
✓ Use it when
Short processing (≤ 15 minutes), spiky or intermittent load, event-driven architecture.
✗ Avoid it when
Long-running tasks or continuous steady load → ECS/Fargate or EC2.
Common uses
- API backend together with API Gateway
- Process files as soon as they're uploaded to S3 (thumbnails, virus scanning)
- Consume messages from SQS, Kinesis, DynamoDB Streams
- Scheduled (cron) tasks via EventBridge
Lambda, Fargate or EC2?
| How it runs | A function runs when an event arrives | Containers run continuously or as tasks | A virtual server runs continuously |
|---|---|---|---|
| Maximum run time | 15 minutes per invocation | No limit | No limit |
| Server management | None | None | You handle the OS, patching and scaling |
| Billing | Requests + run time | vCPU + memory per second while tasks run | Time the instance is on, even when idle |
| Best for | APIs, event processing, short tasks | Web services, long-running workers | Traditional apps that need full control of the machine |
What does it cost?
- Pricing model: Per request (~$0.20/1 million) + run time billed in GB-seconds (~$0.0000166667/GB-second on x86; ARM/Graviton is ~20% cheaper).
- Free tier: Always Free: 1 million requests + 400,000 GB-seconds per month.
Attaching Lambda to a VPC that needs Internet access requires a NAT Gateway (expensive); excessive logging to CloudWatch Logs; recursive S3 → Lambda → S3 loops; Provisioned Concurrency is billed even when idle.
When a question says “under 15 minutes”, “event-driven” or “no servers to manage”, think Lambda; if it says “runs for hours” or “runs continuously”, rule Lambda out. Lambda needs permissions to call other services → use an execution role, never access keys in code.
Try real exam questions
1Which IAM role do you use to grant AWS Lambda permission to access a DynamoDB Stream?
- ADynamic role.
- BInvocation role.
- CExecution role.
- DEvent Source role.
Show answer
Correct answer: C
The execution role is the role Lambda assumes when it runs to access other services, including permission to read a DynamoDB stream. There is no “Dynamic role” or “Event Source role”; an invocation role is an older concept for whoever invokes Lambda.
2A company is designing an application. The application uses an AWS Lambda function to receive information through Amazon API Gateway and to store the information in an Amazon Aurora PostgreSQL database. During the proof-of-concept stage, the company has to increase the Lambda quotas significantly to handle the high volumes of data that the company needs to load into the database. A solutions architect must recommend a new design to improve scalability and minimize the configuration effort. Which solution will meet these requirements?
- ARefactor the Lambda function code to Apache Tomcat code that runs on Amazon EC2 instances. Connect the database by using native Java Database Connectivity (JDBC) drivers.
- BChange the platform from Aurora to Amazon DynamoDB. Provision a DynamoDB Accelerator (DAX) cluster. Use the DAX client SDK to point the existing DynamoDB API calls at the DAX cluster.
- CSet up two Lambda functions. Configure one function to receive the information. Configure the other function to load the information into the database. Integrate the Lambda functions by using Amazon Simple Notification Service (Amazon SNS).
- DSet up two Lambda functions. Configure one function to receive the information. Configure the other function to load the information into the database. Integrate the Lambda functions by using an Amazon Simple Queue Service (Amazon SQS) queue.
Show answer
Correct answer: D
Split it into two functions with an SQS queue in between: SQS holds messages safely and the database-writing function pulls them in batches at a controlled rate, so the Lambda quotas don't need raising. SNS pushes messages immediately, so it doesn't reduce load; moving to EC2 or DynamoDB is a large, costly change.
Frequently asked questions
What is a Lambda cold start?
When no execution environment is ready, Lambda must initialize a new one before running your code, which makes that first request slower. You can reduce it with provisioned concurrency, SnapStart (for supported runtimes) or a smaller deployment package.
How is Lambda priced?
By the number of requests and by run time multiplied by the memory allocated to the function. With no requests you pay almost nothing. A function that runs constantly under steady load can cost more than running the same work on containers or EC2.
What do AWS exams ask about Lambda?
The 15-minute limit, memory settings (CPU scales with memory), concurrency and throttling, connecting Lambda to a VPC, event sources (S3, SQS, API Gateway, EventBridge), error handling with dead-letter queues or destinations, and permissions through the execution role.