Consider this AWS SDK for JavaScript code querying a DynamoDB table named Users for items where age is greater than 25.
const { DynamoDBClient, QueryCommand } = require("@aws-sdk/client-dynamodb");
const client = new DynamoDBClient({ region: "us-east-1" });
const params = {
TableName: "Users",
KeyConditionExpression: "#age > :ageVal",
ExpressionAttributeNames: { "#age": "age" },
ExpressionAttributeValues: { ":ageVal": { N: "25" } }
};
async function run() {
try {
const data = await client.send(new QueryCommand(params));
console.log(data.Items.length);
} catch (err) {
console.error(err);
}
}
run();What will happen when this code runs?
const { DynamoDBClient, QueryCommand } = require("@aws-sdk/client-dynamodb");
const client = new DynamoDBClient({ region: "us-east-1" });
const params = {
TableName: "Users",
KeyConditionExpression: "#age > :ageVal",
ExpressionAttributeNames: { "#age": "age" },
ExpressionAttributeValues: { ":ageVal": { N: "25" } }
};
async function run() {
try {
const data = await client.send(new QueryCommand(params));
console.log(data.Items.length);
} catch (err) {
console.error(err);
}
}
run();Remember that Query requires the partition key in the KeyConditionExpression.
In DynamoDB, Query operations require the partition key to be specified in the KeyConditionExpression. Using a non-key attribute like age causes a ValidationException. To filter by non-key attributes, you must use a Scan with a filter expression or design your table with appropriate keys.
You want to create a DynamoDB client in Node.js using AWS SDK v3 that automatically uses credentials set in environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Which code snippet correctly achieves this?
The AWS SDK v3 automatically loads credentials from environment variables if no explicit credentials are provided.
By default, the AWS SDK for JavaScript v3 uses the default credential provider chain, which includes environment variables. Explicitly passing credentials is optional unless you want to override them. Option A relies on this default behavior and is the simplest correct approach.
You are designing a web application that uses DynamoDB as its database. You want to ensure high availability and low latency globally. Which architecture choice below best supports this goal?
Think about automatic multi-region replication and data consistency.
DynamoDB Global Tables provide fully managed multi-region, multi-master replication. This allows your app to read and write data locally in multiple regions, improving availability and latency. Other options either lack replication or cause latency by accessing remote tables.
You want to create an IAM policy for a Lambda function that only allows reading items from the DynamoDB table Orders. Which policy below follows the principle of least privilege?
Consider which actions are needed to read items and the resource scope.
Option D grants only the read actions (GetItem, Query, Scan) on the specific table Orders. Option D is too broad, Option D excludes Scan which is a read action, and Option D allows access to all tables, not just Orders.
Your Node.js app using AWS SDK for JavaScript calls DynamoDB frequently and sometimes receives ProvisionedThroughputExceededException errors. What is the best practice to handle these errors?
Think about how to reduce retry collisions and avoid overwhelming the service.
Exponential backoff with jitter is a recommended pattern to handle throttling errors. It gradually increases wait time between retries and adds randomness to avoid retry storms. Immediate retries or manual throughput increases are not scalable or efficient.