Course contentsShow
← Cloud and Infrastructure
Lesson 2538 of 2,98954. Serverless ArchitecturesFree lesson

API Gateway with Database Integration

Connect API Gateway directly to databases using service integrations, eliminating the need for function code.

API Gateway with Database Integration

What You'll Learn

You'll discover how to wire API Gateway directly to databases without writing Lambda function code, creating a faster, simpler, and more cost-effective serverless API.

The Direct Connection Advantage

Until now, most serverless APIs follow this flow: API Gateway → Lambda Function → Database. But cloud providers now let you skip the middle step entirely. API Gateway can talk directly to services like DynamoDB (AWS) or Cosmos DB (Azure) using service integrations or direct integrations.

Think of it like ordering at a restaurant: traditionally, you tell a waiter (Lambda), who tells the kitchen (database). With direct integration, you're placing your order straight into the kitchen's system—no waiter needed for simple requests.

How It Works

Instead of routing requests through function code, you configure API Gateway with:

  1. Mapping templates that transform HTTP requests into database API calls (e.g., converting JSON into DynamoDB query format)
  2. IAM roles granting API Gateway permission to access the database
  3. Response mapping to transform database responses back into HTTP-friendly JSON

For example, a GET /items/{id} request can directly trigger a DynamoDB GetItem operation, with the path parameter mapped to the partition key—all through configuration, zero code.

When to Use This Pattern

Perfect for simple CRUD operations: fetching a record by ID, inserting predefined structures, or basic queries. If your logic is just "read this key" or "write this object," direct integration eliminates cold starts, reduces costs (no Lambda invocations), and simplifies your architecture.

However, complex business logic, data validation, or multi-step operations still benefit from functions orchestrating the work.

Key Takeaway: Direct API Gateway-to-database integration removes unnecessary compute layers for simple operations, cutting latency and costs while maintaining the serverless model you've built with DynamoDB, Cosmos DB, and API patterns learned earlier.