AWS Lambda Functions
Write and test your first AWS Lambda function, then tune handlers, events, environment variables, memory, and timeout settings.
A Lambda function is a small unit of code that AWS executes when an event arrives. The function has a handler, which is the entry point, and it receives an event payload plus context information about the invocation. This structure encourages small, focused functions that do one job well, such as validating an API request or processing a file upload.
A beginner-friendly function can be written in Node.js or Python and tested directly in the AWS console. The console lets you define a sample event, invoke the function, and inspect logs quickly. That feedback loop is helpful while learning, though serious teams usually package and deploy functions through CI/CD rather than editing directly in production consoles.
def handler(event, context):
name = event.get("name", "world")
return {"message": f"hello {name}"}
| Setting | Why it matters |
|---|---|
| Handler | Tells Lambda which code entry point to call |
| Environment variables | Store non-secret configuration values |
| Timeout | Prevents runaway execution |
| Memory | Affects both RAM and available CPU |
Timeout and memory sizing are easy to underestimate. Too little memory can make code slow because CPU shares scale with memory. Too short a timeout can create retries and partial work. Start small, test with realistic events, then tune based on CloudWatch duration and error data.
Environment variables are convenient for configuration such as bucket names or feature flags, but sensitive secrets should come from Secrets Manager or Parameter Store rather than plain environment values. Lambda makes it easy to move quickly, so taking a minute to separate configuration from secrets prevents bad patterns from spreading.
Pair this lesson with AWS Lambda for the service-level view and AWS CloudWatch for log-based troubleshooting.
aws lambda invoke --function-name hello-world --payload '{"name":"devops"}' response.json
aws logs tail /aws/lambda/hello-world --since 10m
Operational note
Serverless systems benefit from the same engineering discipline as any other runtime. Keep functions small, log useful context, define retries deliberately, and measure duration and error patterns in CloudWatch. Those practices matter more than the runtime language because they determine whether a Lambda workload stays easy to operate at scale. Shared standards like this make future environments easier to launch, review, and support.
Handler role
What does the handler define in a Lambda function?
Resource tuning
Why does changing a Lambda function’s memory setting often affect performance?