Serverless on TypeScript: Your Backend Without Servers (and Headaches)
Hello, colleague! If you've ever configured a VPS, battled with nginx, monitored CPU load, and restarted a crashed PostgreSQL at three in the morning — this article is for you. The world is moving towards serverless computing, and TypeScript is the perfect language to jump on this train at full speed.
Let's figure out what serverless functions are, how to write them in TypeScript, and which platforms (AWS Lambda, Google Cloud Functions, Yandex Cloud Functions) to choose for your project. Spoiler: there will be code, there will be examples, but there will be no servers.
What is Serverless? (Spoiler: Servers Do Exist)
The name "serverless" is a marketing trick. Servers, of course, exist, but you know nothing about them and don't want to. You just write a function (usually in TypeScript), upload it to the cloud, and it executes on demand. You only pay for execution time — not for an idle server, not for scaling.
This is called FaaS (Function as a Service). The main advantages:
- Automatic scaling — from 0 to 10,000 requests per second in a few seconds.
- Cost savings — you pay for milliseconds of execution, not for renting a machine.
- Less DevOps — the cloud updates the runtime environment and patches vulnerabilities itself.
- TypeScript out of the box — all major providers support Node.js, and therefore TypeScript.
But there are also downsides: cold start (the first milliseconds of loading), execution time limits (usually 15 minutes), and debugging difficulties.
Writing Your First Serverless Function on TypeScript for AWS Lambda
AWS Lambda is the king of serverless. Let's start with it. Install aws-sdk and @types/aws-lambda for TypeScript.
npm init -ynpm install typescript @types/node aws-lambda --save-devnpx tsc --initCreate a file handler.ts:
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => { const name = event.queryStringParameters?.name || 'Guest'; return { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: `Hello, ${name}! This is a serverless function on TypeScript.`, timestamp: new Date().toISOString(), }), };};Build the project:
npx tsczip -r function.zip .Upload the archive to AWS Lambda via the console, specify the runtime Node.js 20.x, and set the entry point to handler.handler.
Done! Now you have an HTTP API that doesn't require a server. You can attach it to API Gateway and use it.
Google Cloud Functions: A TypeScript Alternative from Google
If you prefer the Google ecosystem (Firebase, BigQuery, GCP), then Google Cloud Functions is your choice. The syntax is similar to AWS, but there are nuances.
Install the tool:
npm install -g @google-cloud/functions-frameworkCreate index.ts:
import { Request, Response } from 'express';
export const helloWorld = (req: Request, res: Response) => { const name = req.query.name || 'World'; res.json({ message: `Hello, ${name}! This is Google Cloud Functions on TypeScript.`, timestamp: new Date().toISOString(), });};For local testing:
npx @google-cloud/functions-framework --target=helloWorldDeploy with a single command:
gcloud functions deploy hello-world \ --runtime nodejs20 \ --trigger-http \ --allow-unauthenticated \ --entry-point hell