How to Use DynamoDB with API Gateway: Simple Integration Guide
To use
DynamoDB with API Gateway, create an API Gateway REST API that integrates with DynamoDB using AWS service proxy or Lambda functions. This setup lets you expose DynamoDB operations like GetItem or PutItem through HTTP endpoints for easy data access.Syntax
Using API Gateway with DynamoDB involves setting up an integration request that connects API Gateway methods to DynamoDB actions. You can do this either by:
- Direct AWS service integration (service proxy) where API Gateway calls DynamoDB APIs directly.
- Using AWS Lambda as a middle layer to handle requests and interact with DynamoDB.
Key parts include:
- HTTP Method: Defines the API action (GET, POST, etc.).
- Integration Type: AWS Service or Lambda function.
- Request Mapping: Transforms incoming HTTP requests into DynamoDB API calls.
- Response Mapping: Converts DynamoDB responses back to HTTP responses.
plaintext
POST /items
Integration Type: AWS Service
AWS Service: dynamodb
Action: PutItem
Request Mapping Template (application/json):
{
"TableName": "YourTableName",
"Item": {
"id": {"S": "$input.path('$.id')"},
"data": {"S": "$input.path('$.data')"}
}
}
Response Mapping Template:
$input.path('$.Attributes')Example
This example shows how to create a simple API Gateway POST method that writes an item to a DynamoDB table named Products using AWS service proxy integration.
plaintext
POST /products
Integration Type: AWS Service
AWS Service: dynamodb
Action: PutItem
Request Mapping Template (application/json):
{
"TableName": "Products",
"Item": {
"ProductId": {"S": "$input.path('$.ProductId')"},
"Name": {"S": "$input.path('$.Name')"},
"Price": {"N": "$input.path('$.Price')"}
}
}
Response Mapping Template:
{
"statusCode": 200,
"body": "Item added successfully"
}Output
{
"statusCode": 200,
"body": "Item added successfully"
}
Common Pitfalls
Common mistakes when integrating DynamoDB with API Gateway include:
- Incorrect request mapping templates causing malformed DynamoDB requests.
- Missing or wrong IAM permissions for API Gateway to access DynamoDB.
- Not handling response mapping properly, leading to confusing API responses.
- Using GET method with a request body, which is not supported.
Always test your mapping templates and verify IAM roles.
plaintext
Wrong Request Mapping (missing attribute type): { "TableName": "Products", "Item": { "ProductId": "$input.path('$.ProductId')", "Name": "$input.path('$.Name')" } } Correct Request Mapping (with attribute types): { "TableName": "Products", "Item": { "ProductId": {"S": "$input.path('$.ProductId')"}, "Name": {"S": "$input.path('$.Name')"} } }
Quick Reference
Summary tips for using DynamoDB with API Gateway:
- Use AWS service proxy integration for simple direct calls to DynamoDB.
- Use Lambda integration for complex logic or data processing.
- Always specify DynamoDB attribute types (S, N, BOOL) in request mappings.
- Ensure API Gateway has proper IAM permissions to access DynamoDB.
- Test your API methods with sample requests in the API Gateway console.
Key Takeaways
API Gateway can directly call DynamoDB using AWS service proxy integration for simple APIs.
Request mapping templates must include DynamoDB attribute types to form valid requests.
IAM permissions are required for API Gateway to access DynamoDB securely.
Use Lambda integration if you need custom processing before accessing DynamoDB.
Test your API Gateway methods thoroughly to avoid common mapping and permission errors.