Bird
Raised Fist0
DynamoDBquery~20 mins

Lambda function with DynamoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DynamoDB Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this Lambda function when querying DynamoDB?

Consider a Lambda function written in Node.js that queries a DynamoDB table named Users with a partition key userId. The function queries for userId = '123'. What will the function return if the item exists?

DynamoDB
import AWS from 'aws-sdk';
const dynamoDb = new AWS.DynamoDB.DocumentClient();

export const handler = async (event) => {
  const params = {
    TableName: 'Users',
    Key: { userId: '123' }
  };
  try {
    const data = await dynamoDb.get(params).promise();
    return data.Item || null;
  } catch (error) {
    return { error: error.message };
  }
};
A{"userId":"123","name":"Alice","age":30}
Bnull
C{"error":"ResourceNotFoundException"}
D{"userId":"123"}
Attempts:
2 left
💡 Hint

If the item exists, the get method returns the full item under data.Item.

📝 Syntax
intermediate
2:00remaining
Which option correctly updates an item in DynamoDB using Lambda?

You want to update the age attribute of a user with userId = '456' in DynamoDB using a Lambda function. Which code snippet is syntactically correct?

DynamoDB
import AWS from 'aws-sdk';
const dynamoDb = new AWS.DynamoDB.DocumentClient();

export const handler = async () => {
  const params = {
    TableName: 'Users',
    Key: { userId: '456' },
    UpdateExpression: 'set age = :a',
    ExpressionAttributeValues: { ':a': 35 },
    ReturnValues: 'UPDATED_NEW'
  };
  return await dynamoDb.update(params).promise();
};
AUpdateExpression: 'set age = :a', ExpressionAttributeValues: { ':a': 35 }
BUpdateExpression: 'set age = a', ExpressionAttributeValues: { 'a': 35 }
CUpdateExpression: 'set age = :a', ExpressionAttributeValues: { a: 35 }
DUpdateExpression: 'set age = :a', ExpressionAttributeValues: { ':a' => 35 }
Attempts:
2 left
💡 Hint

Expression attribute values must use placeholders starting with a colon : and be keys in an object.

❓ optimization
advanced
2:00remaining
How to optimize a Lambda function to batch write multiple items to DynamoDB?

You want to insert 25 user records into a DynamoDB table in a single Lambda invocation. Which approach is the most efficient and correct?

ACall <code>dynamoDb.put</code> 25 times in parallel without waiting for promises.
BCall <code>dynamoDb.put</code> 25 times sequentially inside a loop.
CUse <code>dynamoDb.update</code> with 25 update expressions in one call.
DUse <code>dynamoDb.batchWrite</code> with up to 25 <code>PutRequest</code> items in one call.
Attempts:
2 left
💡 Hint

DynamoDB supports batch writes with a limit of 25 items per batch.

🔧 Debug
advanced
2:00remaining
Why does this Lambda function fail with a ProvisionedThroughputExceededException?

This Lambda function writes items to DynamoDB but sometimes fails with ProvisionedThroughputExceededException. What is the likely cause?

DynamoDB
import AWS from 'aws-sdk';
const dynamoDb = new AWS.DynamoDB.DocumentClient();

export const handler = async (event) => {
  const params = {
    TableName: 'Users',
    Item: event.item
  };
  try {
    await dynamoDb.put(params).promise();
    return { status: 'success' };
  } catch (error) {
    return { error: error.code };
  }
};
AThe AWS SDK is not configured with correct credentials.
BThe Item parameter is missing a required attribute.
CThe function is writing too many items too quickly, exceeding the table's write capacity.
DThe table name 'Users' is misspelled.
Attempts:
2 left
💡 Hint

ProvisionedThroughputExceededException means the request rate is too high for the provisioned capacity.

🧠 Conceptual
expert
2:00remaining
What is the best practice for handling DynamoDB pagination in a Lambda function?

You query a DynamoDB table with a large dataset that returns paginated results. How should your Lambda function handle pagination to retrieve all items efficiently?

ASet <code>Limit</code> to a very high number to get all items in one query call.
BUse the <code>LastEvaluatedKey</code> from the query response to iteratively request the next page until no more keys remain.
CIgnore pagination and process only the first page of results.
DUse <code>Scan</code> instead of <code>Query</code> to avoid pagination.
Attempts:
2 left
💡 Hint

DynamoDB paginates query results and provides LastEvaluatedKey to continue fetching.

Practice

(1/5)
1. What is the main purpose of using a Lambda function with DynamoDB in AWS?
easy
A. To run code automatically when DynamoDB data changes
B. To manually update DynamoDB tables from the AWS console
C. To create new DynamoDB tables without coding
D. To backup DynamoDB data to S3 manually

Solution

  1. Step 1: Understand Lambda triggers

    Lambda functions can be triggered by events, such as changes in DynamoDB tables.
  2. Step 2: Identify the purpose of automation

    This allows automatic code execution when data changes, without manual intervention.
  3. Final Answer:

    To run code automatically when DynamoDB data changes -> Option A
  4. Quick Check:

    Lambda triggers = automatic code run [OK]
Hint: Lambda runs code on events like DynamoDB changes [OK]
Common Mistakes:
  • Thinking Lambda manually updates tables
  • Confusing Lambda with backup tools
  • Assuming Lambda creates tables automatically
2. Which of the following is the correct way to initialize AWS DynamoDB DocumentClient in a Node.js Lambda function?
easy
A. const docClient = new AWS.DynamoDBClient();
B. const docClient = new AWS.DynamoDB.DocumentClient();
C. const docClient = AWS.DynamoDB.DocumentClient();
D. const docClient = AWS.DynamoDB.Document();

Solution

  1. Step 1: Recall correct DocumentClient syntax

    The AWS SDK requires using 'new' keyword to create a DocumentClient instance.
  2. Step 2: Match correct class name and usage

    The correct class is AWS.DynamoDB.DocumentClient, not DynamoDBClient or Document.
  3. Final Answer:

    const docClient = new AWS.DynamoDB.DocumentClient(); -> Option B
  4. Quick Check:

    Use 'new' with DocumentClient class [OK]
Hint: Use 'new AWS.DynamoDB.DocumentClient()' to initialize [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using wrong class name like DynamoDBClient
  • Calling Document() instead of DocumentClient
3. Given this Lambda function snippet, what will be the output if the item with id '123' exists in the DynamoDB table?
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event) => {
  const params = {
    TableName: 'Users',
    Key: { id: '123' }
  };
  const data = await docClient.get(params).promise();
  return data.Item;
};
medium
A. An error because 'get' method is incorrect
B. A list of all items in the Users table
C. An empty object {}
D. The item object with id '123' from the Users table

Solution

  1. Step 1: Understand docClient.get usage

    The get method retrieves a single item by key from the specified table.
  2. Step 2: Analyze the return value

    If the item with id '123' exists, data.Item will contain that item object.
  3. Final Answer:

    The item object with id '123' from the Users table -> Option D
  4. Quick Check:

    docClient.get returns item if found [OK]
Hint: docClient.get returns single item by key [OK]
Common Mistakes:
  • Expecting a list instead of single item
  • Thinking get method is invalid
  • Assuming empty object if item exists
4. Identify the error in this Lambda function code that writes an item to DynamoDB:
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event) => {
  const params = {
    TableName: 'Products',
    Item: {
      productId: '001',
      name: 'Book'
    }
  };
  await docClient.putItem(params).promise();
  return 'Success';
};
medium
A. Using 'putItem' instead of 'put' method on DocumentClient
B. Missing 'Key' property in params
C. TableName should be lowercase
D. Missing callback function in putItem

Solution

  1. Step 1: Check DocumentClient method names

    DocumentClient uses 'put' method, not 'putItem'.
  2. Step 2: Confirm correct method usage

    Using 'putItem' causes a runtime error because it does not exist on DocumentClient.
  3. Final Answer:

    Using 'putItem' instead of 'put' method on DocumentClient -> Option A
  4. Quick Check:

    Use 'put' to write items with DocumentClient [OK]
Hint: Use 'put', not 'putItem', with DocumentClient [OK]
Common Mistakes:
  • Confusing low-level DynamoDB client with DocumentClient
  • Assuming 'putItem' exists in DocumentClient
  • Ignoring method naming conventions
5. You want a Lambda function to update a user's email in DynamoDB only if the user exists. Which approach correctly ensures this conditional update?
hard
A. Use docClient.delete before updating
B. Use docClient.put without any condition
C. Use docClient.update with ConditionExpression 'attribute_exists(id)'
D. Use docClient.get and then docClient.put unconditionally

Solution

  1. Step 1: Understand conditional update in DynamoDB

    To update only if an item exists, use ConditionExpression with 'attribute_exists' on the key.
  2. Step 2: Apply ConditionExpression in update method

    docClient.update supports ConditionExpression to prevent overwriting non-existing items.
  3. Step 3: Evaluate other options

    Using put without condition overwrites or creates new items; delete before update is inefficient; get then put is not atomic.
  4. Final Answer:

    Use docClient.update with ConditionExpression 'attribute_exists(id)' -> Option C
  5. Quick Check:

    Conditional update = update + attribute_exists [OK]
Hint: Use ConditionExpression 'attribute_exists' for safe updates [OK]
Common Mistakes:
  • Using put which overwrites without condition
  • Deleting item before update unnecessarily
  • Not using atomic conditional update
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