Bird
Raised Fist0
DynamoDBquery~10 mins

Lambda trigger on stream events in DynamoDB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Lambda trigger on stream events
DynamoDB Table Update
↓
Stream Records Created
↓
Lambda Triggered
↓
Lambda Processes Records
↓
Lambda Executes Business Logic
↓
Optional: Write Back to DB or Other Service
When a DynamoDB table changes, a stream record is created. This triggers a Lambda function that processes the record and runs your code.
Execution Sample
DynamoDB
1. Update DynamoDB item
2. Stream captures change
3. Lambda triggered with event
4. Lambda reads event records
5. Lambda processes each record
Shows how a DynamoDB update triggers a Lambda function via stream events.
Execution Table
StepEvent SourceActionLambda InputLambda Output
1DynamoDB TableItem updatedN/AN/A
2DynamoDB StreamStream record createdRecord with change infoN/A
3Lambda ServiceLambda triggeredEvent with stream recordsN/A
4Lambda FunctionReads event recordsStream records arrayParsed records
5Lambda FunctionProcesses each recordSingle recordBusiness logic result
6Lambda FunctionCompletes executionN/ASuccess or error logged
7EndNo more recordsN/ALambda idle until next event
💡 No more stream records to process, Lambda waits for next trigger
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
streamRecordsemptycontains 1 recordparsed 1 recordprocessed 1 recordempty after processing
lambdaEventnonenoneevent with recordsevent processednone
Key Moments - 2 Insights
Why does Lambda receive an event with multiple records sometimes?
Because DynamoDB streams batch multiple changes before triggering Lambda, as seen in execution_table step 3 and 4 where Lambda input is an event with an array of records.
What happens if Lambda fails to process a record?
Lambda logs an error and may retry depending on configuration. This is implied in execution_table step 6 where Lambda completes execution with success or error logged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Lambda first receive the stream event?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Lambda Input' column to see when Lambda is triggered with the event
According to variable_tracker, what is the state of 'streamRecords' after Lambda processes each record?
Aparsed 1 record
Bcontains 1 record
Cempty after processing
Dnone
💡 Hint
Look at the 'After Step 5' and 'Final' columns for 'streamRecords'
If multiple items update quickly, how does Lambda receive the events?
ABatch of records in one event
BNo events triggered
COne event per item update
DOnly last update triggers event
💡 Hint
Refer to key_moments explanation about batching and execution_table steps 3 and 4
Concept Snapshot
Lambda triggers on DynamoDB stream events when table data changes.
Stream batches changes and sends records to Lambda.
Lambda receives event with array of records.
Lambda processes each record in order.
Errors can cause retries.
Useful for real-time processing of DB changes.
Full Transcript
When you update a DynamoDB table, the change creates a stream record. This stream record triggers a Lambda function. The Lambda function receives an event containing one or more stream records. It reads and processes each record, running your business logic. After processing, Lambda finishes and waits for the next trigger. Sometimes multiple changes batch into one event, so Lambda handles multiple records at once. If Lambda fails, it logs errors and may retry. This setup lets you react instantly to database changes with your code.

Practice

(1/5)
1. What is the main purpose of a Lambda trigger on DynamoDB stream events?
easy
A. To backup the DynamoDB table data to S3
B. To manually update the DynamoDB table from the Lambda console
C. To disable the DynamoDB stream temporarily
D. To automatically run code when data in the DynamoDB table changes

Solution

  1. Step 1: Understand DynamoDB streams

    DynamoDB streams capture data changes like inserts, updates, and deletes.
  2. Step 2: Role of Lambda triggers

    Lambda triggers run code automatically when these stream events occur.
  3. Final Answer:

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

    Lambda triggers = auto-run code on data change [OK]
Hint: Lambda triggers run code automatically on data changes [OK]
Common Mistakes:
  • Thinking Lambda triggers require manual execution
  • Confusing stream enabling with backup
  • Assuming Lambda disables streams
2. Which of the following is the correct way to specify a Lambda trigger for a DynamoDB stream in AWS Console?
easy
A. Enable stream on the table, then add Lambda as a trigger on the stream
B. Create Lambda function first, then enable stream on the table
C. Enable stream on the table and Lambda separately without linking
D. Add Lambda trigger before enabling the stream on the table

Solution

  1. Step 1: Enable stream on DynamoDB table

    Streams must be enabled first to capture data changes.
  2. Step 2: Add Lambda as a trigger on the enabled stream

    Lambda triggers are linked to the stream to react to events.
  3. Final Answer:

    Enable stream on the table, then add Lambda as a trigger on the stream -> Option A
  4. Quick Check:

    Enable stream first, then add Lambda trigger [OK]
Hint: Enable stream first, then attach Lambda trigger [OK]
Common Mistakes:
  • Trying to add Lambda trigger before enabling stream
  • Not linking Lambda to the stream
  • Enabling stream and Lambda separately without connection
3. Given this Lambda function snippet triggered by a DynamoDB stream event:
def lambda_handler(event, context):
    for record in event['Records']:
        if record['eventName'] == 'INSERT':
            print(record['dynamodb']['NewImage']['id']['S'])

What will this code output when a new item with id '123' is inserted?
medium
A. It prints '123' to the logs
B. It prints the old image of the item
C. It raises a KeyError because 'NewImage' is missing
D. It prints nothing because eventName is 'MODIFY'

Solution

  1. Step 1: Check eventName condition

    The code checks if eventName is 'INSERT', which matches new item insertion.
  2. Step 2: Access NewImage id value

    It accesses 'NewImage' dictionary and prints the 'id' string value '123'.
  3. Final Answer:

    It prints '123' to the logs -> Option A
  4. Quick Check:

    INSERT event prints new id '123' [OK]
Hint: INSERT event uses NewImage to get new item data [OK]
Common Mistakes:
  • Confusing NewImage with OldImage
  • Assuming eventName is MODIFY instead of INSERT
  • Not accessing nested dictionary keys correctly
4. You wrote this Lambda code to process DynamoDB stream events but it throws a runtime error:
def lambda_handler(event, context):
    for record in event['Records']:
        print(record['dynamodb']['NewImage']['id']['S'])

What is the likely cause of the error?
medium
A. The 'id' attribute is always an integer, not a string
B. The event may contain DELETE events without NewImage data
C. The event['Records'] key is missing
D. Lambda functions cannot access DynamoDB streams

Solution

  1. Step 1: Understand stream event types

    DELETE events do not have 'NewImage', only 'OldImage'.
  2. Step 2: Accessing NewImage without check causes error

    Trying to access 'NewImage' on DELETE events causes a KeyError.
  3. Final Answer:

    The event may contain DELETE events without NewImage data -> Option B
  4. Quick Check:

    DELETE events lack NewImage causing runtime error [OK]
Hint: Check event type before accessing NewImage [OK]
Common Mistakes:
  • Assuming all events have NewImage
  • Ignoring DELETE event structure
  • Believing Lambda can't access streams
5. You want your Lambda triggered by DynamoDB streams to process only UPDATE events where the attribute 'status' changed to 'completed'. Which approach is best?
hard
A. Filter events in DynamoDB stream settings to only send UPDATE events with 'completed' status
B. Use a separate Lambda for each event type and status value
C. In Lambda, check if eventName is 'MODIFY' and compare OldImage and NewImage 'status' values
D. Enable stream with only KEYS_ONLY to reduce data size

Solution

  1. Step 1: Understand stream filtering limits

    DynamoDB streams cannot filter by attribute values, only event types.
  2. Step 2: Use Lambda code to check eventName and compare status

    Lambda can check if eventName is 'MODIFY' and compare OldImage and NewImage 'status' to detect change to 'completed'.
  3. Final Answer:

    In Lambda, check if eventName is 'MODIFY' and compare OldImage and NewImage 'status' values -> Option C
  4. Quick Check:

    Filter in Lambda by event and attribute change [OK]
Hint: Filter attribute changes inside Lambda, not in stream settings [OK]
Common Mistakes:
  • Trying to filter by attribute in stream settings
  • Using KEYS_ONLY which lacks attribute data
  • Creating multiple Lambdas unnecessarily
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