What is a Lambda function?
AWS Lambda is a serverless compute service that runs your code in response to events. A Lambda “function” is the unit of deployment that contains your code, runtime configuration, and execution role. Triggers like API Gateway, S3, SNS, and EventBridge tell Lambda when to run your code.Create a Lambda function (Console)
In the AWS Console, navigate to Lambda and choose Create function. You can:- Author from scratch (used in this guide)
- Use a blueprint (pre-built templates for common triggers and runtimes)
- Provide a container image (run Lambda as a container image)

- Function name
- Runtime (language + version)
- Architecture (x86_64 or arm64)
- Execution role (creates one by default or you can use an existing role / template)

Function configuration and triggers
After creating the function, the main configuration screen appears. A Lambda function needs a trigger to run. Common triggers:| Trigger | Use case |
|---|---|
| API Gateway | Expose a Lambda as an HTTP endpoint |
| S3 | Run code on object creation/deletion |
| SNS | Fan-out or topic-driven invocation |
| SQS | Asynchronous queue processing |
| EventBridge | Scheduled or event-driven orchestration |

Handler and event object (Node.js example)
The handler is the entry point for your function. The runtime invokes this handler with an event object (and optional context). For API Gateway, return an HTTP-like response object. Example Node.js (ESM) handler template:Using API Gateway as a trigger
You can attach API Gateway to expose your function as a public HTTP endpoint. Choose API type (REST, HTTP) and authorization (for demos, Authorization = NONE). API Gateway will forward requests to Lambda and return the function response. Example simple output returned by the Lambda via API Gateway: “Lambda trigger example” API Gateway example configuration:- API type: HTTP
- Authorization: NONE
- Example endpoint: https://rvhf5t2oe7.execute-api.us-east-1.amazonaws.com/default/firstFunction
Third‑party libraries for Node.js (bcryptjs example)
If your function needs third‑party packages (for example, bcryptjs), you must either include the dependencies with your function code or provide them via a Layer. We’ll create a second function that hashes a password using bcryptjs. Create a new function in the console (for example: secondFunction) with Node.js 18.x and x86_64 architecture, then paste the handler code below.
If your function imports third‑party modules, you must supply them either bundled with the function or via a Layer. Missing dependencies cause runtime import errors.
Packaging dependencies — ZIP upload
To include third‑party libraries in your deployment package:- Locally install packages using npm (creates node_modules).
- Zip your function file(s) together with node_modules and package.json.
- Upload the ZIP to the Lambda console or deploy via CLI.
- index.mjs
- node_modules/
- package.json
- package-lock.json
Lambda Layers — share dependencies across functions
Instead of bundling the same node_modules for multiple functions, create a Lambda Layer to share common code and libraries. Requirements for Node.js layers:- The ZIP must include nodejs/node_modules at the top level.

Using Layers is an efficient way to share common dependencies across multiple Lambda functions and to keep individual deployment packages smaller.

Packaging options summary
| Packaging method | When to use | Pros | Cons |
|---|---|---|---|
| ZIP upload (include node_modules) | Single function or simple deployments | Simple, immediate | Duplicate libraries across functions, larger packages |
| Lambda Layer | Shared dependencies across multiple functions | Reuse, smaller function packages | Extra management step, must match compatible runtimes |
| Container image | Complex dependencies or large binaries | Up to 10 GB image, familiar container tooling | More complex build and deployment |
Monitoring, logging, and troubleshooting
- Lambda provides built-in monitoring: invocations, duration, errors, throttles.
- Use the Monitoring tab for metrics and click through to CloudWatch Logs for detailed logs and request IDs.
- CloudWatch REPORT lines include Duration, Billed Duration, Memory Size, Max Memory Used, and Init Duration — useful for performance tuning.
- API Gateway docs: https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html
- Lambda Layers: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
- CloudWatch docs: https://learn.kodekloud.com/user/courses/aws-cloudwatch
Quick checklist / Best practices
- Choose an appropriate runtime and memory allocation for your workload.
- Keep functions small and single-purpose.
- Use Layers for shared dependencies to reduce package size.
- Monitor and set alarms for invocation errors and throttles.
- Always test with representative event payloads.
Summary
- Create functions by authoring from scratch or using blueprints.
- Configure runtime, name, architecture, and execution role.
- Use triggers (API Gateway, S3, SNS, SQS, EventBridge) to invoke Lambda.
- Test with console test events or via API endpoints.
- Bundle dependencies via ZIP or share them via Layers.
- Monitor with Lambda metrics and CloudWatch Logs to troubleshoot and optimize.