Create a Database-Backed API

By Pulumi Team
Published
Updated

The Challenge

You need a backend API that can handle CRUD operations without provisioning or managing servers. A serverless approach with API Gateway, Lambda, and DynamoDB scales automatically, costs nothing at idle, and lets you focus on business logic instead of infrastructure management.

What You'll Build

  • DynamoDB table for data storage
  • Lambda functions for CRUD operations
  • API Gateway with REST routes
  • CORS enabled for frontend access
  • IAM roles scoped to least privilege

Neo Try This Prompt in Pulumi Neo

Run this prompt in Neo to deploy your infrastructure, or edit it to customize.

Best For

Use this prompt when you need a backend API for a web or mobile application. This serverless pattern works well for any application that needs to store and retrieve structured data, from internal tools to customer-facing products. It is also a practical way to learn how API Gateway, Lambda, and DynamoDB fit together.

Architecture Overview

This architecture implements a fully serverless REST API using three AWS services that integrate natively. API Gateway handles HTTP routing, request validation, and CORS. Lambda functions contain your business logic for each CRUD operation. DynamoDB stores the data in a schema-flexible NoSQL table that scales automatically with demand.

The serverless model means you pay only for actual requests. There are no idle servers to maintain, no operating systems to patch, and no capacity to plan for. DynamoDB adjusts its throughput based on traffic patterns, and Lambda scales from zero to thousands of concurrent executions without configuration.

Each Lambda function handles a specific operation. A POST creates a new record, a GET retrieves one or all records, a PUT updates an existing record, and a DELETE removes one. This separation keeps functions small and testable. API Gateway maps HTTP methods and URL paths to the corresponding Lambda function, so the routing logic stays outside your code.

DynamoDB Table

The DynamoDB table stores your records as JSON documents. A partition key (typically an id field) uniquely identifies each item. DynamoDB handles indexing, replication across availability zones, and automatic backups. You can add global secondary indexes later to support queries on other fields, like looking up records by email or status.

Lambda Functions

Each Lambda function receives an event from API Gateway, performs the database operation, and returns a response. The functions run in a managed execution environment with configurable memory and timeout settings. IAM roles restrict each function to only the DynamoDB operations it needs, following the principle of least privilege.

API Gateway

API Gateway exposes your Lambda functions as HTTP endpoints. It handles request routing, input validation, and CORS preflight responses. The gateway also provides built-in throttling and usage plans if you need to rate-limit callers. Each route maps to a specific Lambda function, creating a clean separation between the public API surface and the implementation.

Common Customizations

  • Add authentication: Extend the prompt to include a Cognito user pool or Lambda authorizer so only authenticated users can access the API.
  • Add pagination: Request that the list endpoint supports pagination using DynamoDB’s LastEvaluatedKey for large datasets.
  • Add a custom domain: Ask for a custom domain name on API Gateway with an ACM certificate for a branded API URL.
  • Add input validation: Request API Gateway request models that validate incoming JSON payloads before they reach your Lambda functions.