Skip to main content
This guide walks through creating, configuring, testing, and monitoring a basic AWS Lambda function. You’ll learn how to expose Lambda with API Gateway, include third‑party Node.js libraries by bundling them with your deployment package, and share dependencies using Lambda Layers.

Create a Lambda function (step-by-step)

  1. Open the AWS Console, search for Lambda, and choose Create a function.
  2. Choose Author from scratch to start with a blank function (you can also use a blueprint or container image).
  3. Enter a function name (for example, firstFunction) and choose a runtime (Node.js, Python, Java, etc.). Examples below use Node.js with an ESM-style handler.
  4. For execution role, accept Create a new role with basic Lambda permissions so the function can write logs to CloudWatch. Leave advanced settings (VPC, etc.) unchecked for this demo.
  5. Click Create function.

Lambda function configuration and destinations

The Lambda console displays the function’s configuration (triggers, layers, destinations). Destinations route asynchronous invocation records to targets like SNS, SQS, another Lambda, or EventBridge and can be configured for success and failure conditions.
A screenshot of the AWS Lambda console "Add destination" page showing destination configuration options (Source: Asynchronous or Stream invocation; Condition: On failure or On success) and a dropdown for destination types like SNS topic, SQS queue, Lambda function, or EventBridge. The Cancel and Save buttons are visible at the bottom.

Default handler (Node.js ESM)

A minimal ESM-style Node.js handler that returns a basic HTTP response:

Triggers — when your function runs

Triggers determine when Lambda executes. Common triggers include API Gateway, S3 events, SNS/SQS, CloudWatch Events / EventBridge, and more. Add triggers on the function’s Configuration tab.

Test your function without a trigger

Use the Lambda console to run test events. Test events are JSON objects passed as the event parameter to your handler. Example test event:
Lambda returns the handler response and shows CloudWatch logs (RequestId, duration, memory usage, etc.) in the console output.

Reading data from the event

If your test event includes application-specific fields, read them in the handler: Example event:
Example handler that uses event.products:
Remember to deploy code changes in the Lambda console before rerunning tests.

Deploying an API Gateway trigger

A common pattern is to expose Lambda via API Gateway so HTTP clients can invoke it. From Add trigger, select API Gateway and create a new API (HTTP or REST). For simple demos you can leave Authorization as NONE.
Screenshot of the AWS Lambda "Add trigger" screen showing API Gateway trigger configuration with "Create a new API" selected and the "HTTP API" option highlighted.
After adding the trigger the Lambda configuration lists the API endpoint and allowed methods. You can copy the endpoint and test it in a browser, Postman, or with curl.
A screenshot of the AWS Lambda console on the Configuration > Triggers tab showing an API Gateway trigger (firstFunction-API) with its HTTP endpoint and trigger details.
When the API receives a request, API Gateway invokes your Lambda and returns the function response to the client.

Using third‑party Node.js libraries (example: bcryptjs)

Real-world functions often require external packages. For Node.js you install packages locally (they appear under node_modules) and include them with your deployment package or supply them via a Lambda Layer. Install locally:
Example handler that hashes a password (Node.js ESM):
Create a second function for this demo (Node.js 18.x runtime) and paste this code into the Lambda inline editor before running a test.
A web browser screenshot of the AWS Lambda "Create function" console showing the "Author from scratch" option selected and a new function named "secondFunction". The runtime is set to Node.js 18.x and architecture x86_64.
If you paste code that imports a third‑party package into the Lambda console editor without uploading the package, you’ll get a “Cannot find package” error. You must include node_modules in your function deployment or attach a Layer that contains the packages.

Why you might see “Cannot find package”

If you run the function immediately after pasting code that imports bcryptjs you might get:
This happens because the Lambda runtime needs the package files in the function deployment. Installing packages locally on your laptop does not automatically push them to the Lambda inline editor.

Bundling code and node_modules in a ZIP

To include dependencies with your function:
  1. On your machine create a project folder containing:
    • your handler file (e.g., index.mjs)
    • package.json (optional)
    • the node_modules folder created by npm install
  2. Compress those items into a ZIP file (for example, hashLambda.zip).
  3. In the Lambda console choose Upload from → .zip file, upload the ZIP, and deploy.
Example local folder view (showing node_modules) before zipping:
A screenshot of a developer workspace with Visual Studio Code on the left. A Windows File Explorer window on the right shows a "lambda" folder containing node_modules, index.mjs, node_modules.zip, package.json, and package-lock.json.
After uploading the ZIP and deploying, the Lambda runtime will have access to bcryptjs and your test should return a 200 status with the hashed password.

Using Lambda Layers to share libraries

Lambda Layers let you share common libraries across multiple functions so you don’t have to bundle node_modules in every function.
Use Layers to share common libraries across functions. Update the Layer to patch or upgrade dependencies and attach the new Layer version to functions that require the change.
To create a Node.js layer:
  1. Create a folder named nodejs and copy your node_modules into it so the ZIP root contains nodejs/node_modules/....
  2. Zip the nodejs folder (for example, hash-layer.zip).
  3. In the Lambda console go to Layers → Create layer, upload the ZIP, set compatible runtimes (e.g., nodejs18.x), and create the Layer.
  4. In your function’s configuration, add the Layer (choose the desired version). The function can then import packages from the Layer at runtime.
Layer creation success screen example:
A screenshot of the AWS Lambda console showing a layer named "hash" with a green banner reading "Successfully created layer hash version 1." The Version details panel lists Version 1 and a compatible runtime of nodejs18.x.
After attaching the Layer, you can remove node_modules from your function package and rely on the Layer for dependencies.

Monitoring, metrics, and logs

Lambda integrates with CloudWatch to surface metrics and logs. The Lambda console provides at-a-glance metrics such as invocations, duration, error count, throttles, iterator age (for stream-based invocations), and concurrent executions. From there you can jump into CloudWatch Logs for deeper inspection and troubleshooting.
A screenshot of the AWS Lambda console showing the CloudWatch metrics dashboard with panels for Invocations, Duration, Error count & success rate, Throttles, Iterator age, and Total concurrent executions. The top bar includes buttons for viewing CloudWatch logs, X‑Ray traces, Lambda Insights, and CodeGuru profiles.
CloudWatch Logs show per-invocation details (RequestId, Duration, Billed Duration, Memory Size, Max Memory Used, Init Duration). For distributed tracing and deeper performance insights use AWS X‑Ray or Lambda Insights.

Summary

  • Create a Lambda with Author from scratch and choose the correct runtime.
  • Use triggers (API Gateway, S3, SNS, SQS, EventBridge) to invoke Lambda.
  • Test functions in the console with JSON test events.
  • Include third‑party Node.js packages by zipping node_modules with the function or by using a Lambda Layer.
  • Monitor functions with the Lambda console and CloudWatch Logs/Metrics.

Watch Video

Practice Lab