-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
autoscaling.tf
119 lines (96 loc) · 5.98 KB
/
autoscaling.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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/${try(aws_dynamodb_table.autoscaled[0].name, aws_dynamodb_table.autoscaled_gsi_ignore[0].name)}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "table_read_policy" {
count = var.create_table && var.autoscaling_enabled && length(var.autoscaling_read) > 0 ? 1 : 0
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.table_read[0].resource_id}"
policy_type = "TargetTrackingScaling"
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
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
scale_in_cooldown = lookup(var.autoscaling_read, "scale_in_cooldown", var.autoscaling_defaults["scale_in_cooldown"])
scale_out_cooldown = lookup(var.autoscaling_read, "scale_out_cooldown", var.autoscaling_defaults["scale_out_cooldown"])
target_value = lookup(var.autoscaling_read, "target_value", var.autoscaling_defaults["target_value"])
}
}
resource "aws_appautoscaling_target" "table_write" {
count = var.create_table && var.autoscaling_enabled && length(var.autoscaling_write) > 0 ? 1 : 0
max_capacity = var.autoscaling_write["max_capacity"]
min_capacity = var.write_capacity
resource_id = "table/${try(aws_dynamodb_table.autoscaled[0].name, aws_dynamodb_table.autoscaled_gsi_ignore[0].name)}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "table_write_policy" {
count = var.create_table && var.autoscaling_enabled && length(var.autoscaling_write) > 0 ? 1 : 0
name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.table_write[0].resource_id}"
policy_type = "TargetTrackingScaling"
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
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}
scale_in_cooldown = lookup(var.autoscaling_write, "scale_in_cooldown", var.autoscaling_defaults["scale_in_cooldown"])
scale_out_cooldown = lookup(var.autoscaling_write, "scale_out_cooldown", var.autoscaling_defaults["scale_out_cooldown"])
target_value = lookup(var.autoscaling_write, "target_value", var.autoscaling_defaults["target_value"])
}
}
resource "aws_appautoscaling_target" "index_read" {
for_each = var.create_table && var.autoscaling_enabled ? var.autoscaling_indexes : {}
max_capacity = each.value["read_max_capacity"]
min_capacity = each.value["read_min_capacity"]
resource_id = "table/${try(aws_dynamodb_table.autoscaled[0].name, aws_dynamodb_table.autoscaled_gsi_ignore[0].name)}/index/${each.key}"
scalable_dimension = "dynamodb:index:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "index_read_policy" {
for_each = var.create_table && var.autoscaling_enabled ? var.autoscaling_indexes : {}
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.index_read[each.key].resource_id}"
policy_type = "TargetTrackingScaling"
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
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
scale_in_cooldown = merge(var.autoscaling_defaults, each.value)["scale_in_cooldown"]
scale_out_cooldown = merge(var.autoscaling_defaults, each.value)["scale_out_cooldown"]
target_value = merge(var.autoscaling_defaults, each.value)["target_value"]
}
}
resource "aws_appautoscaling_target" "index_write" {
for_each = var.create_table && var.autoscaling_enabled ? var.autoscaling_indexes : {}
max_capacity = each.value["write_max_capacity"]
min_capacity = each.value["write_min_capacity"]
resource_id = "table/${try(aws_dynamodb_table.autoscaled[0].name, aws_dynamodb_table.autoscaled_gsi_ignore[0].name)}/index/${each.key}"
scalable_dimension = "dynamodb:index:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "index_write_policy" {
for_each = var.create_table && var.autoscaling_enabled ? var.autoscaling_indexes : {}
name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.index_write[each.key].resource_id}"
policy_type = "TargetTrackingScaling"
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
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}
scale_in_cooldown = merge(var.autoscaling_defaults, each.value)["scale_in_cooldown"]
scale_out_cooldown = merge(var.autoscaling_defaults, each.value)["scale_out_cooldown"]
target_value = merge(var.autoscaling_defaults, each.value)["target_value"]
}
}