diff --git a/README.md b/README.md index 068dd80..b99f28e 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ module "dynamodb_autoscaler" { stage = "dev" name = "cluster" dynamodb_table_name = "cp-dev-cluster-terraform-state-lock" + dynamodb_indexes = [ "first-index", "second-index" ] dynamodb_table_arn = "arn:aws:dynamodb:us-east-1:123456789012:table/cp-dev-cluster-terraform-state-lock" autoscale_write_target = 10 autoscale_read_target = 10 @@ -33,6 +34,7 @@ module "dynamodb_autoscaler" { | `stage` | `` | Stage (_e.g._ `prod`, `dev`, `staging`) | Yes | | `name` | `` | Name (_e.g._ `app` or `cluster`) | Yes | | `dynamodb_table_name` | `` | DynamoDB table name | Yes | +| `dynamodb_indexes` | `[]` | List of DynamoDB indexes | No | | `dynamodb_table_arn` | `` | DynamoDB table ARN | Yes | | `attributes` | `[]` | Additional attributes (_e.g._ `policy` or `role`) | No | | `tags` | `{}` | Additional tags (_e.g._ `map("BusinessUnit","XYZ")` | No | diff --git a/main.tf b/main.tf index d8f5306..7703c2c 100644 --- a/main.tf +++ b/main.tf @@ -86,6 +86,15 @@ resource "aws_appautoscaling_target" "read_target" { service_namespace = "dynamodb" } +resource "aws_appautoscaling_target" "read_target_index" { + count = "${length(var.dynamodb_indexes)}" + max_capacity = "${var.autoscale_max_read_capacity}" + min_capacity = "${var.autoscale_min_read_capacity}" + resource_id = "table/${var.dynamodb_table_name}/index/${element(var.dynamodb_indexes, count.index)}" + scalable_dimension = "dynamodb:index:ReadCapacityUnits" + service_namespace = "dynamodb" +} + resource "aws_appautoscaling_policy" "read_policy" { count = "${var.enabled == "true" ? 1 : 0}" name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.read_target.resource_id}" @@ -112,6 +121,15 @@ resource "aws_appautoscaling_target" "write_target" { service_namespace = "dynamodb" } +resource "aws_appautoscaling_target" "write_target_index" { + count = "${length(var.dynamodb_indexes)}" + max_capacity = "${var.autoscale_max_write_capacity}" + min_capacity = "${var.autoscale_min_write_capacity}" + resource_id = "table/${var.dynamodb_table_name}/index/${element(var.dynamodb_indexes, count.index)}" + scalable_dimension = "dynamodb:index:WriteCapacityUnits" + service_namespace = "dynamodb" +} + resource "aws_appautoscaling_policy" "write_policy" { count = "${var.enabled == "true" ? 1 : 0}" name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.write_target.resource_id}" diff --git a/variables.tf b/variables.tf index 8ed96f3..47350d5 100644 --- a/variables.tf +++ b/variables.tf @@ -41,6 +41,12 @@ variable "dynamodb_table_arn" { description = "DynamoDB table ARN" } +variable "dynamodb_indexes" { + type = "list" + description = "List of DynamoDB indexes" + default = [] +} + variable "autoscale_write_target" { default = 10 description = "The target value for DynamoDB write autoscaling"