-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscaling.tf
230 lines (186 loc) · 10.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
locals {
autoscaling_scheduled_indexes_read = flatten([
for k, v in var.autoscaling_scheduled_indexes_read : [
for i in range(length(v)) : {
key = k
index = i
schedule = v[i].schedule
start_time = try(v[i].start_time, null)
end_time = try(v[i].end_time, null)
timezone = try(v[i].timezone, null)
min_capacity = v[i].min_capacity
max_capacity = v[i].max_capacity
}
]
])
autoscaling_scheduled_indexes_write = flatten([
for k, v in var.autoscaling_scheduled_indexes_write : [
for i in range(length(v)) : {
key = k
index = i
schedule = v[i].schedule
start_time = try(v[i].start_time, null)
end_time = try(v[i].end_time, null)
timezone = try(v[i].timezone, null)
min_capacity = v[i].min_capacity
max_capacity = v[i].max_capacity
}
]
])
}
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"]
}
}
resource "aws_appautoscaling_scheduled_action" "table_read_schedule" {
for_each = { for k, v in var.autoscaling_scheduled_read : k => v if var.create_table && var.autoscaling_enabled && length(var.autoscaling_scheduled_read) > 0 }
name = "DynamoDBReadCapacityUtilization-${replace(aws_appautoscaling_target.table_read[0].resource_id, "/", "-")}-${each.key}"
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"]
start_time = each.value["start_time"]
end_time = each.value["end_time"]
timezone = each.value["timezone"]
scalable_target_action {
max_capacity = each.value["max_capacity"]
min_capacity = each.value["min_capacity"]
}
}
resource "aws_appautoscaling_scheduled_action" "index_read_schedule" {
for_each = { for i in local.autoscaling_scheduled_indexes_read : "${i.key}-${i.index}" => i if var.create_table && var.autoscaling_enabled }
name = "DynamoDBReadCapacityUtilization-${replace(aws_appautoscaling_target.index_read[each.value["key"]].resource_id, "/", "-")}"
resource_id = aws_appautoscaling_target.index_read[each.value["key"]].resource_id
scalable_dimension = aws_appautoscaling_target.index_read[each.value["key"]].scalable_dimension
service_namespace = aws_appautoscaling_target.index_read[each.value["key"]].service_namespace
schedule = each.value["schedule"]
start_time = each.value["start_time"]
end_time = each.value["end_time"]
timezone = each.value["timezone"]
scalable_target_action {
max_capacity = each.value["max_capacity"]
min_capacity = each.value["min_capacity"]
}
}
resource "aws_appautoscaling_scheduled_action" "table_write_schedule" {
for_each = { for k, v in var.autoscaling_scheduled_write : k => v if var.create_table && var.autoscaling_enabled && length(var.autoscaling_scheduled_write) > 0 }
name = "DynamoDBWriteCapacityUtilization-${replace(aws_appautoscaling_target.table_write[0].resource_id, "/", "-")}-${each.key}"
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"]
start_time = each.value["start_time"]
end_time = each.value["end_time"]
timezone = each.value["timezone"]
scalable_target_action {
max_capacity = each.value["max_capacity"]
min_capacity = each.value["min_capacity"]
}
}
resource "aws_appautoscaling_scheduled_action" "index_write_schedule" {
for_each = { for i in local.autoscaling_scheduled_indexes_write : "${i.key}-${i.index}" => i if var.create_table && var.autoscaling_enabled }
name = "DynamoDBWriteCapacityUtilization-${replace(aws_appautoscaling_target.index_write[each.value["key"]].resource_id, "/", "-")}"
resource_id = aws_appautoscaling_target.index_write[each.value["key"]].resource_id
scalable_dimension = aws_appautoscaling_target.index_write[each.value["key"]].scalable_dimension
service_namespace = aws_appautoscaling_target.index_write[each.value["key"]].service_namespace
schedule = each.value["schedule"]
start_time = each.value["start_time"]
end_time = each.value["end_time"]
timezone = each.value["timezone"]
scalable_target_action {
max_capacity = each.value["max_capacity"]
min_capacity = each.value["min_capacity"]
}
}