下面的代码给出了" Provisioned range for read "的输出在"附加设置"中,读/写容量为5 - 10。我想把它设为1 - 10。怎么做呢?
module "dynamodb_table" {
source = "terraform-aws-modules/dynamodb-table/aws"
version = "3.1.1"
name = var.dbname
hash_key = var.hash_key
billing_mode = "PROVISIONED"
read_capacity = 5
write_capacity = 1
autoscaling_enabled = true
autoscaling_read = {
scale_in_cooldown = 50
scale_out_cooldown = 40
target_value = 70
min_capacity = 1
max_capacity = 10
}
autoscaling_write = {
scale_in_cooldown = 50
scale_out_cooldown = 40
target_value = 70
min_capacity = 1
max_capacity = 10
}
attributes = [
{
name = "user_id"
type = "S"
}
]
tags = {
Terraform = "true"
Environment = var.environment
}
}
来自模块的代码:
resource "aws_appautoscaling_target" "table_read" {
count = var.create_table && var.autoscaling_enabled && length(var.autoscaling_read) > 0 ? 1 : 0
max_capacity = var.autoscaling_read["max_capacity"]
min_capacity = var.read_capacity
resource_id = "table/${aws_dynamodb_table.autoscaled[0].name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
我认为你真正想要的是这个参考中的代码:
- aws_appautoscaling_target
- aws_appautoscaling_policy
这是我从main.tf
的实现:
resource "aws_dynamodb_table" "table" {
name = var.table_name
billing_mode = "PROVISIONED"
read_capacity = var.read_capacity_target
write_capacity = var.write_capacity_target
hash_key = var.partition_key_name
range_key = var.sort_key_name
ttl = var.ttl
point_in_time_recovery { enabled = true }
server_side_encryption { enabled = true }
lifecycle {ignore_changes = [read_capacity, write_capacity]}
attribute {
name = var.partition_key_name
type = var.partition_key_type
}
attribute {
name = var.sort_key_name
type = var.sort_key_type
}
tags = {
"Environment" = var.environment
}
}
resource "aws_appautoscaling_target" "dynamodb_table_read_target" {
max_capacity = var.read_capacity_maximum
min_capacity = var.read_capacity_minimum
resource_id = "table/${aws_dynamodb_table.table.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "dynamodb_table_read_policy" {
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.dynamodb_table_read_target.resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.dynamodb_table_read_target.resource_id
scalable_dimension = aws_appautoscaling_target.dynamodb_table_read_target.scalable_dimension
service_namespace = aws_appautoscaling_target.dynamodb_table_read_target.service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
target_value = 70.0
}
}
这里是variables.tf
代码:
variable "vpc_id" {
description = "Required. The Virtual Private Network containing all services."
type = string
}
variable "table_name" {
description = "Required. The name of the DDB table. No spaces."
type = string
}
variable "partition_key_name" {
description = "Required. Name of the partition key. No spaces."
type = string
}
variable "partition_key_type" {
description = "Required. Partition key type: 'S' or 'N'"
type = string
}
variable "sort_key_name" {
description = "Required. The name of the Sort key. No spaces."
type = string
}
variable "sort_key_type" {
description = "Required. Sort key type: 'S' or 'N'"
type = string
}
variable "ttl" {
description = "Required. True if TTL or time-to-live is enabled"
type = bool
}
variable "read_capacity_maximum" {
description = "Required. Maximum allowed autoscale range."
type = number
default = 10
}
variable "read_capacity_minimum" {
description = "Required. Minimum allowed autoscale range."
type = number
default = 2
}
variable "read_capacity_target" {
description = "Required. Target within autoscale range."
type = number
default = 5
}
variable "write_capacity_maximum" {
description = "Required. Maximum allowed autoscale range."
type = number
default = 10
}
variable "write_capacity_minimum" {
description = "Required. Minimum allowed autoscale range."
type = string
default = 2
}
variable "write_capacity_target" {
description = "Required. Target within autoscale range."
type = number
default = 5
}
variable "environment" {
description = "The staging type: development, production."
default = "development"
type = string
}
我打赌你的read_capacity是你用于自动缩放的最小值,你的最大值是在你的自动缩放模块中定义的。
试试这个:
module "dynamodb_table" {
source = "terraform-aws-modules/dynamodb-table/aws"
version = "3.1.1"
name = var.dbname
hash_key = var.hash_key
billing_mode = "PROVISIONED"
read_capacity = 1
write_capacity = 1
autoscaling_enabled = true
autoscaling_read = {
scale_in_cooldown = 50
scale_out_cooldown = 40
target_value = 70
max_capacity = 10
}
autoscaling_write = {
scale_in_cooldown = 50
scale_out_cooldown = 40
target_value = 70
max_capacity = 10
}
attributes = [
{
name = "user_id"
type = "S"
}
]
tags = {
Terraform = "true"
Environment = var.environment
}
}
如果失败,尝试如下操作:
resource "aws_appautoscaling_target" "dynamodb-test-table_read_target" {
max_capacity = 10
min_capacity = 1
resource_id = "table/${aws_dynamodb_table.dynamodb-test-table.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}