Skip to content

Commit

Permalink
feat: add schedule scaling possibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mfroembgen committed Jul 28, 2023
1 parent 03b38ee commit f801d6a
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ Users of Terragrunt can achieve similar results by using modules provided in the

## Modules

No modules.
| Name | Source | Version |
|------|--------|---------|
| <a name="module_index_read_schedule"></a> [index\_read\_schedule](#module\_index\_read\_schedule) | ./module | n/a |
| <a name="module_index_write_schedule"></a> [index\_write\_schedule](#module\_index\_write\_schedule) | ./module | n/a |

## Resources

Expand All @@ -80,6 +83,8 @@ No modules.
| [aws_appautoscaling_policy.index_write_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_policy.table_read_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_policy.table_write_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_scheduled_action.table_read_schedule](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_scheduled_action) | resource |
| [aws_appautoscaling_scheduled_action.table_write_schedule](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_scheduled_action) | resource |
| [aws_appautoscaling_target.index_read](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_appautoscaling_target.index_write](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_appautoscaling_target.table_read](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
Expand Down Expand Up @@ -110,6 +115,10 @@ No modules.
| <a name="input_range_key"></a> [range\_key](#input\_range\_key) | The attribute to use as the range (sort) key. Must also be defined as an attribute | `string` | `null` | no |
| <a name="input_read_capacity"></a> [read\_capacity](#input\_read\_capacity) | The number of read units for this table. If the billing\_mode is PROVISIONED, this field should be greater than 0 | `number` | `null` | no |
| <a name="input_replica_regions"></a> [replica\_regions](#input\_replica\_regions) | Region names for creating replicas for a global DynamoDB table. | `any` | `[]` | no |
| <a name="input_schedule_scaling_indexes_read"></a> [schedule\_scaling\_indexes\_read](#input\_schedule\_scaling\_indexes\_read) | A map of index schedule scaling configurations. | <pre>map(list(object({<br> schedule = string<br> min_capacity = number<br> max_capacity = number<br> })))</pre> | `{}` | no |
| <a name="input_schedule_scaling_indexes_write"></a> [schedule\_scaling\_indexes\_write](#input\_schedule\_scaling\_indexes\_write) | A map of index schedule scaling configurations. | <pre>map(list(object({<br> schedule = string<br> min_capacity = number<br> max_capacity = number<br> })))</pre> | `{}` | no |
| <a name="input_schedule_scaling_read"></a> [schedule\_scaling\_read](#input\_schedule\_scaling\_read) | A map of read schedule scaling settings. `max_capacity` is the only required key. | <pre>list(object({<br> schedule = string<br> min_capacity = number<br> max_capacity = number<br> }))</pre> | `[]` | no |
| <a name="input_schedule_scaling_write"></a> [schedule\_scaling\_write](#input\_schedule\_scaling\_write) | A map of write schedule scaling settings. `max_capacity` is the only required key. | <pre>list(object({<br> schedule = string<br> min_capacity = number<br> max_capacity = number<br> }))</pre> | `[]` | no |
| <a name="input_server_side_encryption_enabled"></a> [server\_side\_encryption\_enabled](#input\_server\_side\_encryption\_enabled) | Whether or not to enable encryption at rest using an AWS managed KMS customer master key (CMK) | `bool` | `false` | no |
| <a name="input_server_side_encryption_kms_key_arn"></a> [server\_side\_encryption\_kms\_key\_arn](#input\_server\_side\_encryption\_kms\_key\_arn) | The ARN of the CMK that should be used for the AWS KMS encryption. This attribute should only be specified if the key is different from the default DynamoDB CMK, alias/aws/dynamodb. | `string` | `null` | no |
| <a name="input_stream_enabled"></a> [stream\_enabled](#input\_stream\_enabled) | Indicates whether Streams are to be enabled (true) or disabled (false). | `bool` | `false` | no |
Expand Down
56 changes: 56 additions & 0 deletions examples/autoscaling/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ module "dynamodb_table" {
max_capacity = 10
}

schedule_scaling_read = [
{
schedule = "cron(15 13 ? * * *)"
max_capacity = 1
min_capacity = 1000
},
{
schedule = "cron(15 9 ? * * *)"
max_capacity = 50
min_capacity = 1000
}
]

schedule_scaling_write = [
{
schedule = "cron(15 13 ? * * *)"
max_capacity = 1
min_capacity = 1000
},
{
schedule = "cron(15 9 ? * * *)"
max_capacity = 50
min_capacity = 1000
}
]

autoscaling_indexes = {
TitleIndex = {
read_max_capacity = 30
Expand All @@ -41,6 +67,36 @@ module "dynamodb_table" {
}
}

schedule_scaling_indexes_read = {
TitleIndex = [
{
schedule = "cron(15 13 ? * * *)"
max_capacity = 1
min_capacity = 1000
},
{
schedule = "cron(15 9 ? * * *)"
max_capacity = 50
min_capacity = 1000
}
]
}

schedule_scaling_indexes_write = {
TitleIndex = [
{
schedule = "cron(15 13 ? * * *)"
max_capacity = 1
min_capacity = 1000
},
{
schedule = "cron(15 9 ? * * *)"
max_capacity = 50
min_capacity = 1000
}
]
}

attributes = [
{
name = "id"
Expand Down
16 changes: 16 additions & 0 deletions module/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "aws_appautoscaling_scheduled_action" "index_schedule" {
for_each = { for k, v in var.schedule_scaling_indexes : k => v }

name = "${var.name}-${each.key}"

resource_id = var.resource_id
scalable_dimension = var.scalable_dimension
service_namespace = var.service_namespace

schedule = each.value["schedule"]

scalable_target_action {
max_capacity = each.value["max_capacity"]
min_capacity = each.value["min_capacity"]
}
}
Empty file added module/outputs.tf
Empty file.
28 changes: 28 additions & 0 deletions module/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
variable "schedule_scaling_indexes" {
description = "A map of index schedule scaling configurations"
type = list(object({
schedule = string
min_capacity = number
max_capacity = number
}))
}

variable "service_namespace" {
type = string
description = "Namespace of the AWS service"
}

variable "resource_id" {
type = string
description = "Identifier of the resource associated with the scheduled action"
}

variable "scalable_dimension" {
type = string
description = "Scalable dimension"
}

variable "name" {
description = "Name of the autoscaling action"
type = string
}
10 changes: 10 additions & 0 deletions module/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.59"
}
}
}
59 changes: 59 additions & 0 deletions schedule.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
resource "aws_appautoscaling_scheduled_action" "table_read_schedule" {
for_each = var.create_table && var.autoscaling_enabled && length(var.schedule_scaling_read) > 0 ? { for k, v in var.schedule_scaling_read : k => v } : {}

name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.table_read[0].resource_id}"

resource_id = aws_appautoscaling_target.table_read[0].resource_id
scalable_dimension = aws_appautoscaling_target.table_read[0].scalable_dimension
service_namespace = aws_appautoscaling_target.table_read[0].service_namespace

schedule = each.value["schedule"]

scalable_target_action {
max_capacity = each.value["max_capacity"]
min_capacity = each.value["min_capacity"]
}
}

module "index_read_schedule" {
for_each = var.create_table && length(var.schedule_scaling_indexes_read) > 0 ? var.autoscaling_indexes : {}
source = "./module"

name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.index_read[each.key].resource_id}"

resource_id = aws_appautoscaling_target.index_read[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.index_read[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.index_read[each.key].service_namespace

schedule_scaling_indexes = var.schedule_scaling_indexes_read[each.key]
}

resource "aws_appautoscaling_scheduled_action" "table_write_schedule" {
for_each = var.create_table && var.autoscaling_enabled && length(var.schedule_scaling_write) > 0 ? { for k, v in var.schedule_scaling_write : k => v } : {}

name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.table_write[0].resource_id}"

resource_id = aws_appautoscaling_target.table_write[0].resource_id
scalable_dimension = aws_appautoscaling_target.table_write[0].scalable_dimension
service_namespace = aws_appautoscaling_target.table_write[0].service_namespace

schedule = each.value["schedule"]

scalable_target_action {
max_capacity = each.value["max_capacity"]
min_capacity = each.value["min_capacity"]
}
}

module "index_write_schedule" {
for_each = var.create_table && length(var.schedule_scaling_indexes_write) > 0 ? var.autoscaling_indexes : {}
source = "./module"

name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.index_write[each.key].resource_id}"

resource_id = aws_appautoscaling_target.index_write[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.index_write[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.index_write[each.key].service_namespace

schedule_scaling_indexes = var.schedule_scaling_indexes_write[each.key]
}
40 changes: 40 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,46 @@ variable "autoscaling_indexes" {
default = {}
}

variable "schedule_scaling_read" {
description = "A map of read schedule scaling settings. `max_capacity` is the only required key."
type = list(object({
schedule = string
min_capacity = number
max_capacity = number
}))
default = []
}

variable "schedule_scaling_write" {
description = "A map of write schedule scaling settings. `max_capacity` is the only required key."
type = list(object({
schedule = string
min_capacity = number
max_capacity = number
}))
default = []
}

variable "schedule_scaling_indexes_read" {
description = "A map of index schedule scaling configurations."
type = map(list(object({
schedule = string
min_capacity = number
max_capacity = number
})))
default = {}
}

variable "schedule_scaling_indexes_write" {
description = "A map of index schedule scaling configurations."
type = map(list(object({
schedule = string
min_capacity = number
max_capacity = number
})))
default = {}
}

variable "table_class" {
description = "The storage class of the table. Valid values are STANDARD and STANDARD_INFREQUENT_ACCESS"
type = string
Expand Down
4 changes: 4 additions & 0 deletions wrappers/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ module "wrapper" {
autoscaling_read = try(each.value.autoscaling_read, var.defaults.autoscaling_read, {})
autoscaling_write = try(each.value.autoscaling_write, var.defaults.autoscaling_write, {})
autoscaling_indexes = try(each.value.autoscaling_indexes, var.defaults.autoscaling_indexes, {})
schedule_scaling_read = try(each.value.schedule_scaling_read, var.defaults.schedule_scaling_read, [])
schedule_scaling_write = try(each.value.schedule_scaling_write, var.defaults.schedule_scaling_write, [])
schedule_scaling_indexes_read = try(each.value.schedule_scaling_indexes_read, var.defaults.schedule_scaling_indexes_read, {})
schedule_scaling_indexes_write = try(each.value.schedule_scaling_indexes_write, var.defaults.schedule_scaling_indexes_write, {})
table_class = try(each.value.table_class, var.defaults.table_class, null)
deletion_protection_enabled = try(each.value.deletion_protection_enabled, var.defaults.deletion_protection_enabled, null)
ignore_changes_global_secondary_index = try(each.value.ignore_changes_global_secondary_index, var.defaults.ignore_changes_global_secondary_index, false)
Expand Down

0 comments on commit f801d6a

Please sign in to comment.