AWS Lambda and Serverless Basics: Run Code Without Servers, Triggers, Pricing and Your First Function
What Does Serverless Mean?
Serverless does not mean 'no servers'. It means you do not manage servers. AWS provisions, patches and scales them for you. You only provide code and pay for what runs.
| Server-based (EC2) | Serverless (Lambda) |
|---|---|
| You choose and manage servers | No servers to manage |
| Pay while the server runs (even idle) | Pay only while code runs |
| You set up scaling | Scales automatically |
| Good for long-running apps | Good for short, event-driven tasks |
What Is AWS Lambda?
- Lambda runs your function in response to an event
- Languages: Python, Node.js, Java, .NET, Go, Ruby and custom runtimes/containers
- You upload code, set memory (128 MB to 10,240 MB) and timeout (up to 15 minutes)
- CPU grows with memory
- It scales from zero to thousands of parallel runs by itself
Common Triggers
| Trigger | Example |
|---|---|
| Amazon S3 | A photo is uploaded, create a thumbnail |
| Amazon API Gateway or Function URL | A web or mobile app calls an HTTP API |
| EventBridge schedule | Run a cleanup every night at 2 AM |
| SQS / SNS | Process messages from a queue or a notification |
| DynamoDB Streams / Kinesis | React to data changes |
| Alexa, IoT, CloudWatch Logs | Voice skills, devices, log processing |
Your First Function
python
import json
def lambda_handler(event, context):
name = event.get("name", "world")
print(f"Received event for {name}") # goes to CloudWatch Logs
return {
"statusCode": 200,
"body": json.dumps({"message": f"Hello, {name}!"})
}
- Open Lambda → Create function → Author from scratch
- Name
hello-lambda, runtime Python 3.12 (or newer), architecture arm64 (cheaper) or x86_64 - Keep the default execution role (it allows writing logs)
- Paste the code and click Deploy
- Create a test event
{ "name": "Asha" }and click Test - Open the Monitor tab and view logs in CloudWatch
bash
aws lambda invoke --function-name hello-lambda --payload '{"name":"Asha"}' --cli-binary-format raw-in-base64-out out.json && cat out.json
aws logs tail /aws/lambda/hello-lambda --since 10m
Permissions: Two Sides
| Side | Meaning |
|---|---|
| Execution role | What the function is allowed to do (for example read from S3, write to DynamoDB, write logs) |
| Resource-based policy | Who is allowed to invoke the function (S3, API Gateway, another account) |
Pricing and Limits
- You pay for number of requests and duration × memory (GB-seconds). A free tier is available each month
- Duration is billed in milliseconds
- Key limits: 15 minutes max runtime, memory up to 10 GB,
/tmpstorage up to 10 GB, 1,000 concurrent executions by default per Region (can be raised) - Cold start: the first run after idle takes extra time to start. Reduce with smaller packages, SnapStart where available, or provisioned concurrency
Best Practices
- Keep functions small and focused
- Do not store state inside the function. Use S3, DynamoDB or other storage
- Give the least privilege role
- Set a sensible timeout and watch errors and throttles in CloudWatch
- Use environment variables and Secrets Manager for settings
- For async jobs add a dead-letter queue or failure destination
When Is Lambda a Good or Bad Fit?
| Good fit | Poor fit |
|---|---|
| Short tasks, event processing, APIs, automation | Jobs longer than 15 minutes |
| Unpredictable or spiky traffic | Constant heavy compute where a server is cheaper |
| Glue between AWS services | Apps needing special OS-level control |
Practice
- Create the hello function and test it
- Add an S3 trigger that logs the uploaded file name
- Create an EventBridge schedule that runs it every 5 minutes (then remove it)
- Change memory from 128 MB to 512 MB and compare duration and cost
Conclusion
Lambda runs your code in response to events with no servers to manage, scales automatically and charges per request and duration. Use execution roles for permissions, triggers for events, and CloudWatch Logs for debugging.
Quick Summary: serverless; Lambda; triggers; first function; permissions; pricing; best practices; fit.

