Let's think about this for a second
An EC2 instance running 24/7 costs you hourly whether or not it's getting traffic. A Lambda function, on the other hand, only runs when triggered by an event (an HTTP request, a file upload, a schedule) — you pay by the millisecond it actually runs, and there's zero cost while idle (you never manage servers at all, which is why it's called 'serverless'). Each Lambda function has a timeout limit (max 15 min) and a memory limit (128MB-10GB), which makes it a poor fit for long-running tasks (heavy batch processing) — but it's ideal for short, event-driven tasks (image resizing, API endpoints, notifications).
Let's connect this to a real scenario
If you set up a Lambda function to auto-trigger every time an image is uploaded to an S3 bucket — with an image-resize/thumbnail-creation script written in Lambda — it'll run automatically the moment an upload happens and instantly generate a thumbnail. You never have to keep a server running, and on days with no uploads, it costs nothing at all.
Let's look at this together
# lambda_function.py — triggered by an S3 upload event
import json
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
print(f"New file uploaded: {key} in {bucket}")
# ... resize image, create thumbnail, etc.
return {
'statusCode': 200,
'body': json.dumps(f'Processed {key}')
}$ aws logs tail /aws/lambda/resize-image-function
New file uploaded: photo.jpg in my-tutorial-bucket-2026
Processed photo.jpgTry it in 5 minutes
Create a 'Hello World' Lambda function (Python or Node.js) and try invoking it manually via the Test button in the AWS Console — check the output in CloudWatch Logs.
A quick word of caution
If you've connected a Lambda function to a trigger source (S3, API Gateway, EventBridge), unexpected trigger frequency can suddenly spike your costs — keep an eye on trigger volume.