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?
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 }; } };
If the item exists, the get method returns the full item under data.Item.
The dynamoDb.get method returns an object with an Item property containing the item if found. If the item exists, it returns the full item data. If not found, Item is undefined, so the function returns null.
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?
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(); };
Expression attribute values must use placeholders starting with a colon : and be keys in an object.
The correct syntax requires the placeholder :a in the update expression and the corresponding key in ExpressionAttributeValues must be a string with the colon. Option A follows this syntax correctly.
You want to insert 25 user records into a DynamoDB table in a single Lambda invocation. Which approach is the most efficient and correct?
DynamoDB supports batch writes with a limit of 25 items per batch.
Using batchWrite with up to 25 items is the most efficient way to insert multiple items in one request. Calling put multiple times is less efficient and may cause throttling.
This Lambda function writes items to DynamoDB but sometimes fails with ProvisionedThroughputExceededException. What is the likely cause?
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 }; } };
ProvisionedThroughputExceededException means the request rate is too high for the provisioned capacity.
This error occurs when the number of write requests exceeds the provisioned write capacity units of the DynamoDB table. The function likely writes too fast or in bursts.
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?
DynamoDB paginates query results and provides LastEvaluatedKey to continue fetching.
To retrieve all items, the Lambda function should check if LastEvaluatedKey exists in the response and use it as ExclusiveStartKey in the next query call, repeating until no key remains.