Lambda and Serverless
Lambda runs your code without any server to manage. You upload a function, and AWS runs it when something happens, such as a web request, a new file in S3 or a timer. You pay only while it runs.
With EC2 you pay for a server all day, even when nobody is using it. With Lambda, there's no server sitting idle: your function wakes up when an event arrives, runs for a few milliseconds or seconds, and stops. For apps with uneven traffic - a contact form, resizing images after upload, a nightly report - this can be much cheaper and needs no maintenance.
The trade-offs: functions have time and memory limits, a function that hasn't run for a while may take a moment to start (a "cold start"), and you design your app as small event-driven pieces. Many modern apps mix Lambda with other services.
A small Lambda function in Python
import json
def lambda_handler(event, context):
name = (event.get("queryStringParameters") or {}).get("name", "guest")
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"message": f"Hello, {name}!"})
}- API Gateway or a Function URL gives your function a web address.
- EventBridge can run it on a schedule, for example every night at 2 am.
- A function can run for up to 15 minutes, so it is best for short tasks.
- Lambda runs code on events without managing servers.
- You pay only while the code runs.
- API Gateway, S3 and EventBridge can trigger functions.
- Best for short tasks (up to 15 minutes).
