Writing Your First Lambda Function with Node.js
Learn to write, deploy, and debug your first Node.js Lambda function. Master the handler structure and execution logs for effective serverless development.
Previously in this course, we covered the foundational concepts of serverless architecture in our Introduction to AWS Lambda: Getting Started with Serverless. Now that you understand the "why" behind the serverless paradigm, it’s time to move to the "how." In this lesson, we will write our first functional Node.js code, deploy it directly via the AWS Console, and master the art of inspecting execution logs to verify our work.
Understanding the Lambda Handler
In Node.js, a Lambda function is simply a module that exports a specific function. We call this the handler. When AWS triggers your function, it executes this handler, passing in two primary arguments: event and context.
event: A JavaScript object containing data from the trigger (e.g., an HTTP request, a file upload notification, or a scheduled timer).context: An object providing information about the runtime environment, such as the function name, memory limits, and the remaining execution time.
Your code must return a result or call a callback to signal completion. For modern asynchronous Node.js, we use async/await syntax, which is cleaner and less error-prone than older callback patterns.
Writing Your First Node.js Lambda
Let’s create a function that returns a simple "Hello from Serverless" JSON response. In your production workflow, you'll eventually use tools like the AWS CDK—which we explored in our earlier lesson on infrastructure automation—but for now, we will use the Console to get comfortable with the runtime.
- Navigate to the Lambda service in the AWS Management Console.
- Click Create function, choose "Author from scratch," and name it
my-first-function. - Set the runtime to Node.js 20.x (or the latest stable version).
- Replace the default code in the "Code" tab with the following:
JAVASCRIPTexport const handler = async (event) => { console.log("Event received:", JSON.stringify(event, null, 2)); const response = { statusCode: 200, body: JSON.stringify({ message: "Hello from Serverless!", timestamp: new Date().toISOString(), }), }; return response; };
Click Deploy to save your changes.
Testing and Inspecting Logs
With the code deployed, it's time to verify it actually works.
- Click the Test tab in the function editor.
- Create a new test event (the default "hello-world" template is fine).
- Click Test.
You should see a green "Execution result" box. Click the Logs tab to see your output. This redirects you to Amazon CloudWatch, which is the "truth" for your serverless applications. You will see your console.log output there, alongside system-generated logs like "REPORT" and "START."
Hands-on Exercise
To move beyond the default example, modify your handler to accept a name from the event object.
- Change your code to look for
event.name. If it's missing, default to "World." - Deploy the code.
- In the Test tab, edit the JSON payload to:
{"name": "Developer"}. - Run the test again and verify the response message changes to "Hello, Developer!".
Common Pitfalls
- Forgetting to Deploy: A common mistake is editing code in the console and immediately clicking "Test" without hitting the orange Deploy button. If you don't deploy, you're testing the previous version of your code.
- Missing JSON.stringify: If you return a raw object instead of a string in the
bodyfield of an API response, clients will likely fail to parse your output. Always ensure your response body is a serialized string. - Ignoring Log Groups: If your function fails silently, the first place to look is always the CloudWatch log stream. Don't rely on the console's "Execution result" alone; it doesn't always capture the full stack trace of an unhandled exception.
FAQ
Q: Do I need to manage Node.js versions in Lambda? A: AWS manages the runtime environment, but you choose the version. Always pick the latest LTS (Long Term Support) version to ensure security and performance.
Q: How much memory should I give my function? A: Start small (128MB). If you notice slow execution times or memory errors in your logs, increase the memory. Note that in Lambda, increasing memory also proportionally increases CPU power.
Q: Can I use external npm packages? A: Yes, but you must bundle them with your code or use a Lambda Layer. We will cover this in later lessons as our project complexity grows.
Recap
In this lesson, we moved from theory to practice. You learned that a Lambda function is just an exported Node.js handler, how to handle basic JSON responses, and how to use CloudWatch logs to debug your execution. By mastering these basics, you've taken the first step toward building the robust backend for our course project.
Up next: We will dive into Managing Lambda Execution Environments, where we'll learn to optimize performance by adjusting memory, timeouts, and execution roles.

