0
0
DynamoDBquery~10 mins

Lambda function with DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lambda function with DynamoDB
Event triggers Lambda
Lambda starts execution
Lambda reads event data
Lambda calls DynamoDB API
DynamoDB processes request
DynamoDB returns response
Lambda processes response
Lambda returns result or error
This flow shows how a Lambda function is triggered, interacts with DynamoDB, and returns a result.
Execution Sample
DynamoDB
exports.handler = async (event) => {
  const AWS = require('aws-sdk');
  const db = new AWS.DynamoDB.DocumentClient();
  const params = { TableName: 'Users', Key: { userId: event.userId } };
  const data = await db.get(params).promise();
  return data.Item;
};
This Lambda function fetches a user item from DynamoDB based on userId from the event.
Execution Table
StepActionInput/ConditionDynamoDB CallResponseOutput
1Lambda triggeredevent.userId = '123'NoNoStart execution
2Create DynamoDB clientAWS SDK loadedNoNoClient ready
3Prepare paramsTableName='Users', Key={userId:'123'}NoNoParams ready
4Call db.get(params).promise()params as aboveGetItem on 'Users' with Key userId='123'Returns item {userId:'123', name:'Alice'}Data received
5Return data.ItemItem existsNoNoReturns {userId:'123', name:'Alice'}
6Lambda execution endsNoNoNoResponse sent
💡 Lambda finishes after returning the DynamoDB item or error if any
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
event.userId'123''123''123''123''123'
dbundefinedAWS.DynamoDB.DocumentClient instanceAWS.DynamoDB.DocumentClient instanceAWS.DynamoDB.DocumentClient instanceAWS.DynamoDB.DocumentClient instance
paramsundefinedundefined{ TableName: 'Users', Key: { userId: '123' } }{ TableName: 'Users', Key: { userId: '123' } }{ TableName: 'Users', Key: { userId: '123' } }
dataundefinedundefinedundefined{ Item: { userId: '123', name: 'Alice' } }{ Item: { userId: '123', name: 'Alice' } }
return valueundefinedundefinedundefinedundefined{ userId: '123', name: 'Alice' }
Key Moments - 3 Insights
Why do we use 'await' before the DynamoDB call?
Because the DynamoDB call returns a promise, 'await' pauses Lambda execution until the data is returned, as shown in step 4 of the execution_table.
What happens if the item with userId '123' does not exist?
The 'data.Item' will be undefined, so the Lambda will return undefined or null. This is implied in step 5 where the item is returned if it exists.
Why do we create a new DynamoDB client inside the Lambda handler?
To interact with DynamoDB, the Lambda needs a client instance, created at step 2, which allows calling DynamoDB APIs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'params' after step 3?
Aundefined
B{ TableName: 'Users', Key: { userId: '123' } }
C{ userId: '123' }
DAWS.DynamoDB.DocumentClient instance
💡 Hint
Check the 'params' variable in variable_tracker after step 3
At which step does the Lambda function receive the data from DynamoDB?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Response' column in execution_table where DynamoDB returns the item
If the event.userId was missing, what would change in the execution?
ALambda would return a default item
BLambda would skip DynamoDB call
CDynamoDB call would fail or return no item
DLambda would create a new user
💡 Hint
Consider how 'params' depends on event.userId in variable_tracker and execution_table
Concept Snapshot
Lambda function with DynamoDB:
- Lambda triggered by event
- Create DynamoDB client inside Lambda
- Prepare params with TableName and Key
- Use await db.get(params).promise() to fetch item
- Return data.Item as Lambda response
- Handle missing items gracefully
Full Transcript
This visual execution shows how a Lambda function interacts with DynamoDB. When the Lambda is triggered, it reads the event data, creates a DynamoDB client, prepares parameters for the query, calls DynamoDB to get an item, waits for the response, and returns the item. Variables like event.userId, params, and data change step-by-step. Key moments include understanding the use of await, handling missing items, and why the client is created inside Lambda. The quiz questions help reinforce these steps by referencing the execution table and variable states.