Skip to content

Commit 9ae52b6

Browse files
feat: Added variable autoscaling_enabled to control autoscaling (#44)
Co-authored-by: Max Williams <[email protected]>
1 parent 47f9b63 commit 9ae52b6

File tree

6 files changed

+25
-36
lines changed

6 files changed

+25
-36
lines changed

CHANGELOG.md

-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [1.2.0](https://github.com/terraform-aws-modules/terraform-aws-dynamodb-table/compare/v1.1.1...v1.2.0) (2022-01-14)
6-
7-
8-
### Features
9-
10-
* Added autoscaled table resource (may cause unexpected changes) ([#43](https://github.com/terraform-aws-modules/terraform-aws-dynamodb-table/issues/43)) ([c4a8306](https://github.com/terraform-aws-modules/terraform-aws-dynamodb-table/commit/c4a8306643ad30f67b63b3db37fcf7c8dd5b168a))
11-
125
### [1.1.1](https://github.com/terraform-aws-modules/terraform-aws-dynamodb-table/compare/v1.1.0...v1.1.1) (2022-01-10)
136

147

README.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ module "dynamodb_table" {
2929

3030
**Warning: enabling or disabling autoscaling can cause your table to be recreated**
3131

32-
There are two separate Terraform resources used for the DynamoDB table: one is for when any autoscaling settings are used and the other when not. The following scenarios will make Terraform recreate the table:
33-
34-
- Upgrading from an older version of this module with autoscaling settings enabled
35-
- Enabling autoscaling settings when they were previously disabled
36-
- Disabling autoscaling settings when they were previously enabled
37-
38-
In these scenarios you will need to move the old `aws_dynamodb_table` resource that is being `destroyed` to the new resource that is being `created`. For example:
32+
There are two separate Terraform resources used for the DynamoDB table: one is for when any autoscaling is enabled the other when disabled. If your table is already created and then you change the variable `autoscaling_enabled` then your table will be recreated by Terraform. In this case you will need to move the old `aws_dynamodb_table` resource that is being `destroyed` to the new resource that is being `created`. For example:
3933

4034
```
4135
terraform state mv module.dynamodb_table.aws_dynamodb_table.this module.dynamodb_table.aws_dynamodb_table.autoscaled
@@ -86,6 +80,7 @@ No modules.
8680
|------|-------------|------|---------|:--------:|
8781
| <a name="input_attributes"></a> [attributes](#input\_attributes) | List of nested attribute definitions. Only required for hash\_key and range\_key attributes. Each attribute has two properties: name - (Required) The name of the attribute, type - (Required) Attribute type, which must be a scalar type: S, N, or B for (S)tring, (N)umber or (B)inary data | `list(map(string))` | `[]` | no |
8882
| <a name="input_autoscaling_defaults"></a> [autoscaling\_defaults](#input\_autoscaling\_defaults) | A map of default autoscaling settings | `map(string)` | <pre>{<br> "scale_in_cooldown": 0,<br> "scale_out_cooldown": 0,<br> "target_value": 70<br>}</pre> | no |
83+
| <a name="input_autoscaling_enabled"></a> [autoscaling\_enabled](#input\_autoscaling\_enabled) | Whether or not to enable autoscaling. See note in README about this setting | `bool` | `false` | no |
8984
| <a name="input_autoscaling_indexes"></a> [autoscaling\_indexes](#input\_autoscaling\_indexes) | A map of index autoscaling configurations. See example in examples/autoscaling | `map(map(string))` | `{}` | no |
9085
| <a name="input_autoscaling_read"></a> [autoscaling\_read](#input\_autoscaling\_read) | A map of read autoscaling settings. `max_capacity` is the only required key. See example in examples/autoscaling | `map(string)` | `{}` | no |
9186
| <a name="input_autoscaling_write"></a> [autoscaling\_write](#input\_autoscaling\_write) | A map of write autoscaling settings. `max_capacity` is the only required key. See example in examples/autoscaling | `map(string)` | `{}` | no |

autoscaling.tf

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_appautoscaling_target" "table_read" {
2-
count = var.create_table && length(var.autoscaling_read) > 0 ? 1 : 0
2+
count = var.create_table && var.autoscaling_enabled && length(var.autoscaling_read) > 0 ? 1 : 0
33

44
max_capacity = var.autoscaling_read["max_capacity"]
55
min_capacity = var.read_capacity
@@ -9,7 +9,7 @@ resource "aws_appautoscaling_target" "table_read" {
99
}
1010

1111
resource "aws_appautoscaling_policy" "table_read_policy" {
12-
count = var.create_table && length(var.autoscaling_read) > 0 ? 1 : 0
12+
count = var.create_table && var.autoscaling_enabled && length(var.autoscaling_read) > 0 ? 1 : 0
1313

1414
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.table_read[0].resource_id}"
1515
policy_type = "TargetTrackingScaling"
@@ -29,7 +29,7 @@ resource "aws_appautoscaling_policy" "table_read_policy" {
2929
}
3030

3131
resource "aws_appautoscaling_target" "table_write" {
32-
count = var.create_table && length(var.autoscaling_write) > 0 ? 1 : 0
32+
count = var.create_table && var.autoscaling_enabled && length(var.autoscaling_write) > 0 ? 1 : 0
3333

3434
max_capacity = var.autoscaling_write["max_capacity"]
3535
min_capacity = var.write_capacity
@@ -39,7 +39,7 @@ resource "aws_appautoscaling_target" "table_write" {
3939
}
4040

4141
resource "aws_appautoscaling_policy" "table_write_policy" {
42-
count = var.create_table && length(var.autoscaling_write) > 0 ? 1 : 0
42+
count = var.create_table && var.autoscaling_enabled && length(var.autoscaling_write) > 0 ? 1 : 0
4343

4444
name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.table_write[0].resource_id}"
4545
policy_type = "TargetTrackingScaling"
@@ -59,7 +59,7 @@ resource "aws_appautoscaling_policy" "table_write_policy" {
5959
}
6060

6161
resource "aws_appautoscaling_target" "index_read" {
62-
for_each = var.create_table ? var.autoscaling_indexes : {}
62+
for_each = var.create_table && var.autoscaling_enabled ? var.autoscaling_indexes : {}
6363

6464
max_capacity = each.value["read_max_capacity"]
6565
min_capacity = each.value["read_min_capacity"]
@@ -69,7 +69,7 @@ resource "aws_appautoscaling_target" "index_read" {
6969
}
7070

7171
resource "aws_appautoscaling_policy" "index_read_policy" {
72-
for_each = var.create_table ? var.autoscaling_indexes : {}
72+
for_each = var.create_table && var.autoscaling_enabled ? var.autoscaling_indexes : {}
7373

7474
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.index_read[each.key].resource_id}"
7575
policy_type = "TargetTrackingScaling"
@@ -89,7 +89,7 @@ resource "aws_appautoscaling_policy" "index_read_policy" {
8989
}
9090

9191
resource "aws_appautoscaling_target" "index_write" {
92-
for_each = var.create_table ? var.autoscaling_indexes : {}
92+
for_each = var.create_table && var.autoscaling_enabled ? var.autoscaling_indexes : {}
9393

9494
max_capacity = each.value["write_max_capacity"]
9595
min_capacity = each.value["write_min_capacity"]
@@ -99,7 +99,7 @@ resource "aws_appautoscaling_target" "index_write" {
9999
}
100100

101101
resource "aws_appautoscaling_policy" "index_write_policy" {
102-
for_each = var.create_table ? var.autoscaling_indexes : {}
102+
for_each = var.create_table && var.autoscaling_enabled ? var.autoscaling_indexes : {}
103103

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

examples/autoscaling/main.tf

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ resource "random_pet" "this" {
99
module "dynamodb_table" {
1010
source = "../../"
1111

12-
name = "my-table-${random_pet.this.id}"
13-
hash_key = "id"
14-
range_key = "title"
15-
billing_mode = "PROVISIONED"
16-
read_capacity = 5
17-
write_capacity = 5
12+
name = "my-table-${random_pet.this.id}"
13+
hash_key = "id"
14+
range_key = "title"
15+
billing_mode = "PROVISIONED"
16+
read_capacity = 5
17+
write_capacity = 5
18+
autoscaling_enabled = true
1819

1920
autoscaling_read = {
2021
scale_in_cooldown = 50

main.tf

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
locals {
2-
autoscaling_enabled = length(var.autoscaling_read) + length(var.autoscaling_write) + length(var.autoscaling_indexes) > 0 ? true : false
3-
create_normal_table = var.create_table && !local.autoscaling_enabled ? 1 : 0
4-
create_autoscaled_table = var.create_table && local.autoscaling_enabled ? 1 : 0
5-
}
6-
71
resource "aws_dynamodb_table" "this" {
8-
count = local.create_normal_table
2+
count = var.create_table && !var.autoscaling_enabled ? 1 : 0
93

104
name = var.name
115
billing_mode = var.billing_mode
@@ -88,7 +82,7 @@ resource "aws_dynamodb_table" "this" {
8882
}
8983

9084
resource "aws_dynamodb_table" "autoscaled" {
91-
count = local.create_autoscaled_table
85+
count = var.create_table && var.autoscaling_enabled ? 1 : 0
9286

9387
name = var.name
9488
billing_mode = var.billing_mode

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ variable "timeouts" {
122122
}
123123
}
124124

125+
variable "autoscaling_enabled" {
126+
description = "Whether or not to enable autoscaling. See note in README about this setting"
127+
type = bool
128+
default = false
129+
}
130+
125131
variable "autoscaling_defaults" {
126132
description = "A map of default autoscaling settings"
127133
type = map(string)

0 commit comments

Comments
 (0)