Introduction: What is Serverless and AWS Lambda?
\n\nServerless is a cloud computing model where the developer is completely abstracted from managing server infrastructure. You simply write code, upload it to the cloud, and the provider (AWS, Google Cloud, Azure) automatically runs it in response to events, scaling resources on demand. You pay only for the actual execution time of the code (and the number of requests).
\n\nAWS Lambda is the flagship serverless service from Amazon Web Services. It allows you to run code in various languages, including JavaScript (Node.js), without needing to provision and manage servers. Lambda is ideal for processing HTTP requests (via API Gateway), background tasks, processing files from S3, reacting to changes in the DynamoDB database, and many other scenarios.
\n\nIn this article, we will cover AWS Lambda in JavaScript from A to Z: from creating your first function to deploying, monitoring, and optimizing it.
\n\nMain Part: Creating and Managing Lambda Functions in Node.js
\n\n1. Creating Your First Lambda Function
\n\nGo to the AWS Lambda console and click "Create function". Select the "Author from scratch" option. Specify a name (e.g., my-first-lambda) and choose the runtime environment Node.js 20.x (or the latest stable version).
The basic structure of a Node.js function looks like this:
\n\nexports.handler = async (event, context) => {\n // event - data passed to the function (e.g., HTTP request)\n // context - information about the execution environment (e.g., request ID)\n\n console.log('Received event:', JSON.stringify(event, null, 2));\n\n // Your business logic\n const response = {\n statusCode: 200,\n body: JSON.stringify({\n message: 'Hello from Lambda!',\n input: event\n }),\n };\n\n return response; // AWS Lambda expects a return value\n};\n\n\nIn this simple example, the function receives an event and returns a JSON response. If the function is called via API Gateway, event will contain the HTTP method, headers, request body, etc.
Important: Always use async/await for asynchronous operations (database queries, API calls). If the function is not asynchronous, it must return a Promise or use a callback.
2. Triggers and Integrations
\n\nThe power of Lambda is unlocked through triggers — events from other AWS services that invoke your function. Let's look at three of the most popular options in JavaScript.
\n\n2.1. HTTP Requests via API Gateway
\nThis is the most common scenario for creating REST APIs. You attach an API Gateway trigger to your Lambda. In this case, event will contain the HTTP request structure. Example of handling:
exports.handler = async (event) => {\n const httpMethod = event.httpMethod;\n const path = event.path;\n const queryParams = event.queryStringParameters || {};\n const body = event.body ? JSON.parse(event.body) : {};\n\n console.log(`Method: ${httpMethod}, Path: ${path}`);\n\n if (httpMethod === 'GET' && path === '/hello') {\n return {\n statusCode: 200,\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ message: `Hello, ${queryParams.name || 'world'}!` })\n };\n }\n\n return {\n statusCode: 404,\n body: JSON.stringify({ error: 'Not Found' })\n };\n};\n\n\n2.2. Processing Files from S3
\nLambda can automatically trigger when files are uploaded or deleted in an S3 bucket. This is perfect for creating image thumbnails, converting videos, or indexing data.
\n\nconst AWS = require('aws-sdk');\nconst s3 = new AWS.S3();\n\nexports.handler = async (e