Your First AWS Lambda Function
Lambda is the simplest cloud compute. From zero to a callable function.
Step 1: Write the function
def handler(event, context):
return {"statusCode": 200, "body": "hello"}
Save as app.py.
Step 2: Deploy via SAM
- Install SAM CLI; create a SAM template.yaml referencing app.py.
sam build && sam deploy --guided, deploys the function + API Gateway.
Step 3: Invoke
Invocation is where the abstraction proves itself. The same function answers HTTP, scheduled events, and queue triggers depending on the wiring.
- HTTP.
curl https://<your-api-url>hits the API Gateway endpoint and returns 'hello'. - Cold start. First call after idle takes longer (200ms to 2s); subsequent calls are warm.
- Direct invoke.
aws lambda invoke --function-name myfn out.jsonbypasses API Gateway for testing. - Logs. Every invocation lands in CloudWatch Logs under
/aws/lambda/<function-name>within seconds.
Step 4: Logs and monitoring
CloudWatch is the default observability surface for Lambda. Built-in metrics cover invocation health; custom metrics cover the business logic.
- Logs. Every invocation logged with request ID;
filter-patternqueries find the failing invocations fast. - Built-in metrics. Invocations, Duration, Errors, Throttles, IteratorAge for streams; the four-row dashboard.
- Custom metrics. Emit via
boto3CloudWatch PutMetricData or via embedded metric format in logs. - Alarms. Error-rate alarm and p99 duration alarm at minimum; the equivalent of the four golden signals for Lambda.
Antipatterns
- Lambda without timeout config. Default 3s; many use cases need more.
- Hard-coding secrets. Use Secrets Manager.
- Default memory size. Tune for your workload.
What to do this week
Three moves. (1) Run the tutorial end-to-end on your own laptop / sandbox. (2) Apply the pattern to one production workload. (3) Document the variations you needed; share with the team.