Serverless Functions in JavaScript: AWS Lambda from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to the World of Serverless and AWS Lambda



Modern cloud development is increasingly moving to a serverless architecture. Despite the name, servers don't disappear — you just stop thinking about them. AWS Lambda is a service from Amazon that allows you to run code without provisioning or managing servers. You pay only for the actual execution time and the number of requests.



In this article, we will cover AWS Lambda with JavaScript (Node.js) from the very basics to real-world examples. You will learn how to create, configure, and deploy functions, as well as get acquainted with best practices.



AWS Lambda Basics for Node.js



What is a Lambda Function?

A Lambda function is an isolated block of code that runs in response to an event (trigger). The function receives event (event data) and context (runtime environment information) as input and returns a result.



Structure of a Simple Lambda Function

A basic function in Node.js looks like this:



exports.handler = async (event, context) => {    const name = event.queryStringParameters?.name || 'Guest';    const response = {        statusCode: 200,        body: JSON.stringify({ message: `Hello, ${name}!` }),    };    return response;};


  • exports.handler — the mandatory entry point.
  • event — contains data passed by the trigger (e.g., an HTTP request from API Gateway).
  • context — provides methods for managing execution (time, logger, identifier).
  • The function must be async or return a Promise.


Triggers (Invocation Sources)

Lambda does not exist in a vacuum. It can be invoked by:

  • API Gateway — HTTP requests (REST/WebSocket).
  • S3 — file uploads/deletions.
  • DynamoDB Streams — database changes.
  • SQS/SNS — message queues.
  • CloudWatch Events — scheduling (cron).


Creating and Deploying a Lambda Function



Step 1: Setting Up the Environment

You will need:

  • An AWS account.
  • Installed AWS CLI with configured credentials.
  • Node.js (version 18 or 20, recommended by AWS).


Step 2: Creating a Function via the Console

This is the fastest way for beginners:

  1. Go to AWS Management Console → Lambda.
  2. Click "Create function" → "Author from scratch".
  3. Enter a name, select the runtime Node.js 20.x.
  4. Click "Create function".

In the code editor, paste our example above and click "Deploy". Done!



Step 3: Deploying with AWS CLI

To automate deployment, use the command:

zip -r function.zip index.js node_modulesaws lambda update-function-code \    --function-name my-first-lambda \    --zip-file fileb://function.zip


Advanced Scenarios and Best Practices



Working with Environment Variables

Never store secrets in code. Use process.env:

const dbPassword = process.env.DB_PASSWORD;const tableName = process.env.TABLE_NAME;

Variables are set in the function settings (Configuration → Environment variables).



Error Handling and Logging

Use the built-in logger console.log — it automatically sends logs to CloudWatch:

exports.handler = async (event) => {    console.log('Received event:', JSON.stringify(event, null, 2));    try {        // your code    } catch (error) {        console.error('Error:', error.message);        throw e                

Blogs

Book Recommendations