How do I dynamically scale AWS DynamoDB with AppAutoScaling?
In this guide, we will demonstrate how to configure AWS DynamoDB with automatic scaling using Pulumi. We will create a DynamoDB table and set up Application Auto Scaling to automatically adjust the read and write capacity based on the workload.
Key Points
- Define a DynamoDB table with the necessary attributes and key schema.
- Configure Application Auto Scaling for the DynamoDB table’s read and write capacity.
- Set up scaling policies to manage the table’s capacity based on CloudWatch metrics.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Define the DynamoDB table
const dynamoTable = new aws.dynamodb.Table("myTable", {
attributes: [
{ name: "Id", type: "S" },
],
hashKey: "Id",
readCapacity: 5,
writeCapacity: 5,
streamEnabled: true,
streamViewType: "NEW_AND_OLD_IMAGES",
});
// Define the role for Application Auto Scaling
const role = new aws.iam.Role("autoScalingRole", {
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({
Service: "application-autoscaling.amazonaws.com",
}),
});
// Attach the necessary policies to the role
const policyAttachment = new aws.iam.RolePolicyAttachment("autoScalingPolicy", {
role: role.name,
policyArn: "arn:aws:iam::aws:policy/service-role/AmazonDynamoDBFullAccess",
});
// Create a scalable target for the DynamoDB table's read capacity
const readScalableTarget = new aws.appautoscaling.Target("readScalableTarget", {
maxCapacity: 20,
minCapacity: 5,
resourceId: pulumi.interpolate`table/${dynamoTable.name}`,
scalableDimension: "dynamodb:table:ReadCapacityUnits",
serviceNamespace: "dynamodb",
roleArn: role.arn,
});
// Create a scalable target for the DynamoDB table's write capacity
const writeScalableTarget = new aws.appautoscaling.Target("writeScalableTarget", {
maxCapacity: 20,
minCapacity: 5,
resourceId: pulumi.interpolate`table/${dynamoTable.name}`,
scalableDimension: "dynamodb:table:WriteCapacityUnits",
serviceNamespace: "dynamodb",
roleArn: role.arn,
});
// Define a scaling policy for read capacity
const readScalingPolicy = new aws.appautoscaling.Policy("readScalingPolicy", {
policyType: "TargetTrackingScaling",
resourceId: readScalableTarget.resourceId,
scalableDimension: readScalableTarget.scalableDimension,
serviceNamespace: readScalableTarget.serviceNamespace,
targetTrackingScalingPolicyConfiguration: {
targetValue: 70.0,
predefinedMetricSpecification: {
predefinedMetricType: "DynamoDBReadCapacityUtilization",
},
scaleInCooldown: 60,
scaleOutCooldown: 60,
},
});
// Define a scaling policy for write capacity
const writeScalingPolicy = new aws.appautoscaling.Policy("writeScalingPolicy", {
policyType: "TargetTrackingScaling",
resourceId: writeScalableTarget.resourceId,
scalableDimension: writeScalableTarget.scalableDimension,
serviceNamespace: writeScalableTarget.serviceNamespace,
targetTrackingScalingPolicyConfiguration: {
targetValue: 70.0,
predefinedMetricSpecification: {
predefinedMetricType: "DynamoDBWriteCapacityUtilization",
},
scaleInCooldown: 60,
scaleOutCooldown: 60,
},
});
// Export the DynamoDB table name and ARN
export const tableName = dynamoTable.name;
export const tableArn = dynamoTable.arn;
Summary
In this guide, we created a DynamoDB table and configured Application Auto Scaling to dynamically adjust the read and write capacity based on the demand. We defined scalable targets and scaling policies to ensure the table’s capacity is managed efficiently. This setup helps maintain optimal performance and cost-effectiveness for your DynamoDB tables.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.