Complete the code to create a DynamoDB client in AWS Lambda.
const AWS = require('aws-sdk'); const dynamoDB = new AWS.DynamoDB.[1]();
The DocumentClient is used in Lambda to interact easily with DynamoDB tables.
Complete the code to define a Lambda function that reads an item from DynamoDB.
exports.handler = async (event) => {
const params = {
TableName: 'Users',
Key: { 'UserId': event.[1] }
};
const data = await dynamoDB.get(params).promise();
return data.Item;
};The event object usually contains the user ID as userId to fetch the item.
Fix the error in the Lambda function that writes data to DynamoDB.
exports.handler = async (event) => {
const params = {
TableName: 'Orders',
Item: event.[1]
};
await dynamoDB.put(params).promise();
return { statusCode: 200 };
};The put method requires the key Item with capital 'I' to specify the data.
Fill both blanks to create a Lambda function that deletes an item from DynamoDB using the correct key and method.
exports.handler = async (event) => {
const params = {
TableName: 'Products',
[1]: { 'ProductId': event.productId }
};
await dynamoDB.[2](params).promise();
return { statusCode: 200 };
};The delete method deletes an item using the Key property to specify which item.
Fill the blanks to update an attribute in DynamoDB using Lambda.
exports.handler = async (event) => {
const params = {
TableName: 'Employees',
Key: { 'EmployeeId': event.employeeId },
UpdateExpression: 'set [1] = :val',
ExpressionAttributeValues: { ':val': event.newValue },
ReturnValues: '{{BLANK_3}}'
};
const result = await dynamoDB.update(params).promise();
return result.Attributes;
};To update the 'Salary' attribute, return 'UPDATED_NEW' to get updated attributes.