---
title: "Build a Serverless API using AWS Lambda, CDK, and Neon"
description: "A step-by-step build of a single-endpoint REST API on AWS Lambda, defined with AWS CDK v2 in TypeScript, that returns a product list from a Neon serverless Postgres database."
url: "https://swe.quest/articles/build-a-serverless-api-using-aws-lambda-cdk-and-neon-939349a37c380ca9"
canonical: "https://neon.com/blog/serverless-api-using-aws-lambda-cdk-and-neon"
source: "Neon"
published: "2023-11-30"
difficulty: "medium"
kind: "How-to"
reading_minutes: 11
tags: ["serverless","infrastructure-as-code","postgres","cloud","typescript"]
---

# Build a Serverless API using AWS Lambda, CDK, and Neon

A post on [Neon](https://swe.quest/sources/neon.md), published 2023-11-30, 11 min read, How-to. Original: [https://neon.com/blog/serverless-api-using-aws-lambda-cdk-and-neon](https://neon.com/blog/serverless-api-using-aws-lambda-cdk-and-neon)

## Summary

A step-by-step build of a single-endpoint REST API on AWS Lambda, defined with AWS CDK v2 in TypeScript, that returns a product list from a Neon serverless Postgres database. The Lambda uses the Neon serverless driver (HTTP/WebSockets instead of TCP) and fetches the connection string from AWS Secrets Manager once via top-level await, so it isn't retrieved on every request. The CDK stack bundles the function as ESM for Node 20, grants a secretsmanager:GetSecretValue policy and wires it to an API Gateway RestApi GET /products route. Open CORS is used for the demo, with a note to restrict origins in production.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Serverless](https://swe.quest/tags/serverless.md) (Backend & APIs)
- [Infrastructure as code](https://swe.quest/tags/infrastructure-as-code.md) (Infrastructure)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Cloud platforms](https://swe.quest/tags/cloud.md) (Infrastructure)
- [TypeScript](https://swe.quest/tags/typescript.md) (Languages)

## Prerequisites

- TypeScript and Node.js ([easy posts on TypeScript](https://swe.quest/tags/typescript.md?difficulty=easy))
- AWS account and IAM basics ([easy posts on Cloud platforms](https://swe.quest/tags/cloud.md?difficulty=easy))
- Basic SQL ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))

## Keywords

AWS Lambda, AWS CDK, Neon, API Gateway, AWS Secrets Manager, @neondatabase/serverless

## Related posts

- [Build a serverless API using Cloudflare Workers, Drizzle ORM, and Neon](https://swe.quest/articles/build-a-serverless-api-using-cloudflare-workers-drizzle-orm-and-neon-f83c472c9d399a17.md): Neon · 2023-05-30 · Medium · 9 min read · Serverless, PostgreSQL, TypeScript, Schema migrations
  A step-by-step setup for a serverless TypeScript API on Cloudflare Workers that uses Hono for routing, Drizzle ORM for schema definition and migrations, and Neon serverless Postgres for storage.
- [Deploy a Serverless FastAPI App with Neon Postgres and AWS App Runner at any scale](https://swe.quest/articles/deploy-a-serverless-fastapi-app-with-neon-postgres-and-aws-app-runner-at-any-6068dfbf055e1e25.md): Neon · 2024-02-09 · Medium · 11 min read · Python, PostgreSQL, Serverless, Cloud platforms, Deployment
  A step-by-step setup for a Python API built with FastAPI and SQLModel, backed by Neon serverless Postgres and deployed to AWS App Runner with automatic deploys on each Git push.
- [Build a Dynamic E-Commerce Store UI with Astro, Neon Postgres, and AWS Amplify](https://swe.quest/articles/build-a-dynamic-e-commerce-store-ui-with-astro-neon-postgres-and-aws-amplify-5d8746c814ec14f2.md): Neon · 2024-04-09 · Medium · 12 min read · PostgreSQL, Frontend, Serverless, E-commerce, Deployment
  An e-commerce storefront built with Astro in server-rendered mode, reading products from Neon serverless Postgres over HTTP through the @neondatabase/serverless driver and deployed to AWS Amplify on every Git commit.
- [Simplify Serverless in AWS: How to Use Neon and Ephemeral Environments](https://swe.quest/articles/simplify-serverless-in-aws-how-to-use-neon-and-ephemeral-environments-81cd9c7a09760341.md): Neon · 2025-05-05 · Medium · 10 min read · PostgreSQL, Serverless, CI/CD, Testing, JavaScript
  Neon's serverless Postgres pairs well with ephemeral environments on AWS Lambda because each environment can get its own instant copy-on-write database branch without paying for extra RDS uptime.
- [Fullstack Serverless CI/CD in AWS Amplify Hosting with Postgres Database Branching](https://swe.quest/articles/fullstack-serverless-ci-cd-in-aws-amplify-hosting-with-postgres-database-5ab5e970ace5ba81.md): Neon · 2024-03-06 · Medium · 10 min read · CI/CD, PostgreSQL, Serverless, Cloud platforms, Deployment
  Each AWS Amplify Hosting app branch can get its own isolated Neon Postgres database branch, created automatically during CI/CD by a custom neon-ci.sh script that calls the Neon CLI and writes the branch connection string into DATABASE\_URL in a .env file.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
