How can AWS Lambda integrate with Amazon DynamoDB?
Instruction: Describe how AWS Lambda functions can interact with Amazon DynamoDB, including triggers.
Updated
Official answer
Lambda interacts with DynamoDB in two distinct ways.
For direct access, the handler uses an AWS SDK and a least-privilege execution role to read or write the required table/index. Choose keys for the access pattern, paginate results and handle throttling. Use conditional writes or transactions for invariants that must hold when a write commits; do not rely on a later stream handler to prevent an invalid write.
For change processing, enable DynamoDB Streams with the required image fields and create a Lambda event source mapping. Lambda polls the stream and invokes the handler with batches. DynamoDB does not synchronously call the handler as part of the table write. Ordering is preserved for changes to a particular item; there is no global order across the table.
Batch processing is at least once, so duplicate records can reach the handler. Make side effects idempotent. Configure retry/record-age limits, discarded-record handling and partial batch responses where suitable. A partial response must follow the stream mapping's sequence-number contract; it is not an arbitrary list of business IDs.
For a search-index projection, use the item identity and a version/order check so replayed older changes cannot overwrite newer state. Monitor IteratorAge, errors and downstream failures. A stream consumer is an eventually updated projection, not an atomic replacement for the source database.