What is AWS Lambda?
AWS Lambda is a serverless computing service from Amazon.
- No servers to manage – AWS handles all the infrastructure.
- Automatic scaling – if 10,000 users call your API, AWS runs 10,000 copies of your function.
- Cost-efficient – you pay only for execution time, not idle time.
- Fast to deploy – perfect for small tasks or microservices.
How does it work?
Each Lambda function is triggered by an event — like an HTTP request, a file upload, or a message. When that event happens, AWS automatically runs your code.
Here’s a simple example in Node.js:
exports.handler = async (event) => {
console.log("Event:", event);
return {
statusCode: 200,
body: JSON.stringify({ message: "Hello from Lambda!" }),
};
};
When the event occurs (for example, an API call), AWS runs this function and returns the response.
Common use cases
- Building simple REST APIs with API Gateway + Lambda.
- Running background jobs (for example, sending emails).
- Processing files uploaded to S3.
- Event-driven automation, like reacting to messages in SNS or SQS.
Things to keep in mind
- Cold starts: when your function hasn’t run for a while, the first run may take longer.
- Time limits: a Lambda can run for up to 15 minutes.
- Size: smaller packages start faster — keep your code light.
Best practices
- Keep each function small and focused — one clear job.
- Use environment variables for secrets and configuration.
- Reuse database connections and avoid heavy dependencies.
- Log useful information for debugging (CloudWatch helps with that).