aws_utils_dynamodb
AWS DynamoDB utilities for Rust, providing a simplified interface for common DynamoDB operations.
Features
- Simple DynamoDB client creation with configurable endpoint
- Record operations (CRUD)
- Table management operations
- Stream-based pagination for scan and query operations
- CSV import functionality from S3
- Error handling with custom error types
Installation
Add this to your Cargo.toml:
[]
= { = "crates/dynamodb" }
Usage
Creating a Client
use ;
use Duration;
// Create client with default timeout settings
let client = make_client_with_timeout_default.await;
// Create client with custom timeout settings
let client = make_client_with_timeout.await;
// Create client with custom endpoint and default timeout
let client = make_client_with_timeout_default.await;
// Create client without timeout configuration (legacy)
let client = make_client.await;
// Create client with custom endpoint and no timeout (legacy)
let client = make_client.await;
Logging AWS Communication
make_client accepts an optional [SharedInterceptor]. By passing an interceptor that
implements aws_sdk_dynamodb::config::Intercept, you can run custom logic — such as
logging — every time the client communicates with AWS.
The interceptor below logs each request, response, and operation result. It uses the
tracing crate, which is also what the AWS SDK uses
internally.
use make_client;
use ;
type BoxError = ;
;
# async
tracing does not emit anything until a subscriber is initialized. Set one up once in your
application (for example with tracing-subscriber) and control verbosity with RUST_LOG:
// Add `tracing-subscriber` to your dependencies.
fmt
.with_env_filter
.init;
Example output (RUST_LOG=info):
INFO DynamoDbLoggingInterceptor: DynamoDB -> AWS request method=POST uri=https://dynamodb.ap-northeast-1.amazonaws.com/
INFO DynamoDbLoggingInterceptor: AWS -> DynamoDB response status=200
INFO DynamoDbLoggingInterceptor: DynamoDB operation succeeded
Record Operations
use ;
use ;
use HashMap;
// Get an item
let mut key = new;
key.insert;
let item = get_item.await?;
// Put an item
let mut item = new;
item.insert;
item.insert;
let output = put_item.await?;
// Update an item
let mut key = new;
key.insert;
let output = update_item.await?;
// Delete an item
let mut key = new;
key.insert;
let output = delete_item.await?;
// Scan all items
let items = scan_all.await?;
// Query items (all)
let items = query_all.await?;
// Query items (with limit, no pagination)
let items = query.await?;
Table Operations
use ;
use ;
// Create a table with on-demand billing
let attrs = vec!;
let output = create_table.await?;
// Create a table with provisioned capacity
let output = create_table.await?;
// Delete a table
let output = delete_table.await?;
// Get table capacity
let = get_capacity.await?;
// Update table capacity
let output = set_capacity.await?;
CSV Import from S3
use import_table;
use TableType;
use ;
// Import CSV data from S3 to a new DynamoDB table
let attrs = vec!;
import_table.await?;
Stream Operations
For handling large datasets, use stream-based operations:
use ;
use TryStreamExt;
// Scan with streaming
let stream = scan_stream;
pin_mut!;
while let Some = stream.try_next.await?
// Query with streaming
let stream = query_stream;
pin_mut!;
while let Some = stream.try_next.await?
Error Handling
The crate provides a custom Error type that wraps AWS SDK errors and includes common error cases:
NotFound- Item not foundValidationError- Invalid parameters or stateInvalid- Invalid response from AWSAwsSdkError- AWS SDK specific errors
Environment Variables
The client uses the AWS SDK's default credential chain, which checks for credentials in the following order:
- Environment variables (
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_REGION) - ECS task role (for Fargate/ECS)
- EC2 instance profile
- AWS credentials file
- Other configured credential providers
License
This project is part of the utilities.aws-utils workspace.