0
0
DynamoDBquery~30 mins

API Gateway to DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
API Gateway to DynamoDB
📖 Scenario: You are building a serverless REST API for a product catalog. Instead of using Lambda functions, you want API Gateway to connect directly to DynamoDB to reduce latency and cost. You need to create the request and response mapping templates for GET and POST operations.
🎯 Goal: Write the VTL mapping templates that transform HTTP requests into DynamoDB API calls and DynamoDB responses into clean JSON for the client.
📋 What You'll Learn
Create a GetItem request mapping template
Create a PutItem request mapping template
Write a response mapping for GetItem that strips DynamoDB type descriptors
Add error handling for missing items
💡 Why This Matters
🌍 Real World
Direct API Gateway to DynamoDB integration is used in high-performance serverless APIs where simple CRUD operations don't need Lambda compute overhead.
💼 Career
AWS developers use this pattern to build cost-effective APIs for catalogs, configurations, and metadata services that need sub-50ms response times.
Progress0 / 4 steps
1
GetItem request mapping template
Create a JSON request mapping template for GET /products/{id}. Set TableName to "Products" and Key to an object with "id" of type "S" using $input.params('id') to read the path parameter.
DynamoDB
Need a hint?

DynamoDB GetItem needs TableName and Key. The key value uses type descriptor S for strings.

2
PutItem request mapping template
Create a JSON request mapping template for POST /products. Set TableName to "Products" and Item with three fields: "id" (type "S") from $input.path('$.id'), "name" (type "S") from $input.path('$.name'), and "price" (type "N") from $input.path('$.price').
DynamoDB
Need a hint?

Use $input.path('$.field') to read JSON body fields. Numbers use type N, strings use S.

3
GetItem response mapping template
Write a VTL response mapping template that extracts the item from $input.path('$.Item') into a variable $item using #set, then returns a JSON object with id from $item.id.S, name from $item.name.S, and price from $item.price.N (without quotes for the number).
DynamoDB
Need a hint?

Use #set to assign the item, then access typed values like $item.id.S. Leave numbers unquoted.

4
Add error handling for missing items
Wrap the response template in a VTL conditional. Check if $input.path('$.Item') is empty using #if($input.path('$.Item') == ''). If empty, set the response status to 404 with #set($context.responseOverride.status = 404) and return {"error": "Product not found"}. Otherwise return the normal mapped response from step 3.
DynamoDB
Need a hint?

Use #if/#else/#end for conditional logic. Override the HTTP status with $context.responseOverride.status.