Bird
Raised Fist0
AWScloud~10 mins

Lambda with DynamoDB Streams in AWS - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Lambda with DynamoDB Streams
DynamoDB Table
↓
Data Change: Insert/Update/Delete
↓
DynamoDB Stream Records
↓
Lambda Triggered
↓
Lambda Processes Records
↓
Lambda Executes Business Logic
↓
Optional: Write to Other Services
When data changes in DynamoDB, the change is sent as a stream record. Lambda listens to these records and runs code to process them.
Execution Sample
AWS
1. Insert item into DynamoDB
2. Stream captures change
3. Lambda triggered with stream event
4. Lambda reads event records
5. Lambda processes each record
This sequence shows how a DynamoDB data change triggers a Lambda function via streams.
Process Table
StepEventStream RecordLambda ActionResult
1Insert item into DynamoDBNew image with item dataNo action yetStream captures change
2Stream record createdRecord with eventName: INSERTLambda triggeredLambda receives event with records
3Lambda starts processingReads record dataParses new imageExtracts item details
4Lambda processes recordEventName is INSERTExecutes insert logicProcesses new item
5Lambda completesNo more recordsReturns successChanges processed
6No new stream recordsNo eventLambda idleWaiting for next trigger
💡 No more stream records to process, Lambda finishes execution.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
streamRecords[][record1][record1][record1][]
lambdaEventnull{records:[record1]}{records:[record1]}{records:[record1]}null
processedItems00111
Key Moments - 3 Insights
Why does Lambda get triggered multiple times?
Lambda triggers each time new stream records appear, as shown in execution_table rows 2 and 6 where Lambda waits for new events.
What happens if multiple records arrive at once?
Lambda receives all records in one event and processes them one by one, as seen in execution_table row 3 where Lambda reads all records.
Does Lambda modify the DynamoDB table directly?
No, Lambda processes stream data and can write elsewhere, but the original DynamoDB change already happened before Lambda runs, as shown in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what triggers the Lambda function?
AA direct API call to Lambda
BA new record appearing in the DynamoDB stream
CA scheduled timer event
DManual invocation by user
💡 Hint
See execution_table row 2 where Lambda is triggered by a stream record.
At which step does Lambda parse the new item data from the stream?
AStep 3
BStep 5
CStep 1
DStep 6
💡 Hint
Check execution_table row 3 where Lambda reads and parses the record.
If no new stream records arrive, what is Lambda's state?
ALambda processes old records repeatedly
BLambda triggers continuously without data
CLambda remains idle waiting for new events
DLambda deletes the DynamoDB table
💡 Hint
See execution_table row 6 where Lambda is idle waiting for new triggers.
Concept Snapshot
DynamoDB Streams capture table changes.
Lambda triggers on new stream records.
Lambda processes each record's data.
Lambda runs code after data change.
Useful for reacting to DB updates automatically.
Full Transcript
When you add, update, or delete data in a DynamoDB table, the change is recorded in a stream. This stream sends records about the change. AWS Lambda listens to these stream records and runs your code automatically. The Lambda function reads each record, understands what changed, and can do tasks like updating other systems or sending notifications. Lambda only runs when there are new stream records, so it waits quietly otherwise. This setup helps you react quickly and automatically to database changes without manual steps.

Practice

(1/5)
1. What is the main purpose of connecting AWS Lambda to DynamoDB Streams?
easy
A. To manually trigger Lambda functions from the AWS console
B. To automatically run code when data in the DynamoDB table changes
C. To backup DynamoDB data to S3 automatically
D. To increase the read capacity of the DynamoDB table

Solution

  1. Step 1: Understand DynamoDB Streams role

    DynamoDB Streams capture data changes in the table as events.
  2. Step 2: Understand Lambda integration

    Lambda can be triggered automatically by these stream events to run code.
  3. Final Answer:

    To automatically run code when data in the DynamoDB table changes -> Option B
  4. Quick Check:

    Lambda triggers on stream events = A [OK]
Hint: Streams trigger Lambda on data changes automatically [OK]
Common Mistakes:
  • Thinking Lambda triggers manually from console
  • Confusing streams with backups
  • Assuming streams increase table capacity
2. Which of the following is the correct way to enable DynamoDB Streams on a table?
easy
A. Create a Lambda function and attach it to the table
B. Enable versioning on the DynamoDB table
C. Set the stream specification to NEW_AND_OLD_IMAGES in the table settings
D. Increase the write capacity units of the table

Solution

  1. Step 1: Identify how to enable streams

    DynamoDB Streams are enabled by setting the stream specification on the table.
  2. Step 2: Choose correct stream view type

    NEW_AND_OLD_IMAGES captures both before and after images of changes.
  3. Final Answer:

    Set the stream specification to NEW_AND_OLD_IMAGES in the table settings -> Option C
  4. Quick Check:

    Stream enabled via table settings = B [OK]
Hint: Enable streams in table settings, not in Lambda [OK]
Common Mistakes:
  • Trying to enable streams by creating Lambda
  • Confusing versioning with streams
  • Changing capacity units instead of streams
3. Given this Lambda event handler code snippet triggered by DynamoDB Streams:
def lambda_handler(event, context):
    for record in event['Records']:
        if record['eventName'] == 'INSERT':
            print(record['dynamodb']['NewImage']['id']['S'])

What will this code print when a new item with id '123' is added to the DynamoDB table?
medium
A. '123' printed once for the new item
B. 'id' key printed instead of its value
C. An error because 'NewImage' does not exist
D. Nothing, because eventName is not 'INSERT'

Solution

  1. Step 1: Understand event structure for INSERT

    For an INSERT event, 'NewImage' contains the new item data including 'id'.
  2. Step 2: Analyze code behavior

    The code prints the string value of 'id' from 'NewImage' when eventName is 'INSERT'.
  3. Final Answer:

    '123' printed once for the new item -> Option A
  4. Quick Check:

    INSERT event prints new id value = D [OK]
Hint: INSERT events have NewImage with new item data [OK]
Common Mistakes:
  • Assuming eventName is different
  • Expecting OldImage on INSERT
  • Confusing key name with value
4. You deployed a Lambda function triggered by DynamoDB Streams but it never runs. Which of the following is the most likely cause?
medium
A. The DynamoDB table does not have streams enabled
B. The Lambda function code has a syntax error
C. The DynamoDB table has no read capacity units
D. The Lambda function has no execution role

Solution

  1. Step 1: Check stream enablement

    Lambda triggers on DynamoDB Streams events, so streams must be enabled on the table.
  2. Step 2: Consider other causes

    While code errors or roles cause failures, the function would still be invoked if streams exist.
  3. Final Answer:

    The DynamoDB table does not have streams enabled -> Option A
  4. Quick Check:

    Streams must be enabled for Lambda trigger = C [OK]
Hint: Check if streams are enabled on the table first [OK]
Common Mistakes:
  • Assuming code errors prevent invocation
  • Ignoring missing streams
  • Confusing read capacity with triggers
5. You want your Lambda function to process only updated attributes from DynamoDB Streams and ignore inserts and deletes. Which stream view type and event filter should you use?
hard
A. Enable NEW_AND_OLD_IMAGES stream view and filter events where eventName is 'INSERT'
B. Enable KEYS_ONLY stream view and filter events where eventName is 'INSERT'
C. Enable OLD_IMAGE stream view and filter events where eventName is 'REMOVE'
D. Enable NEW_IMAGE stream view and filter events where eventName is 'MODIFY'

Solution

  1. Step 1: Choose stream view for updated data

    NEW_IMAGE provides the new state after update, suitable for processing changes.
  2. Step 2: Filter events for updates only

    Filtering on eventName 'MODIFY' ensures only update events trigger Lambda.
  3. Final Answer:

    Enable NEW_IMAGE stream view and filter events where eventName is 'MODIFY' -> Option D
  4. Quick Check:

    Updates = NEW_IMAGE + 'MODIFY' filter [OK]
Hint: Use NEW_IMAGE and 'MODIFY' event filter for updates only [OK]
Common Mistakes:
  • Using KEYS_ONLY which lacks attribute data
  • Filtering on INSERT or REMOVE instead of MODIFY
  • Choosing OLD_IMAGE which shows old data only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard