This repository has been archived by the owner on Jul 14, 2023. It is now read-only.
forked from cloudposse/terraform-aws-dynamodb-autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
109 lines (88 loc) · 4.78 KB
/
main.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
resource "aws_appautoscaling_target" "read_target" {
count = module.this.enabled ? 1 : 0
max_capacity = var.autoscale_max_read_capacity
min_capacity = var.autoscale_min_read_capacity
resource_id = "table/${var.dynamodb_table_name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_target" "read_target_index" {
for_each = module.this.enabled ? toset(var.dynamodb_indexes) : toset([])
max_capacity = coalesce(var.autoscale_max_read_capacity_index, var.autoscale_max_read_capacity)
min_capacity = coalesce(var.autoscale_min_read_capacity_index, var.autoscale_min_read_capacity)
resource_id = "table/${var.dynamodb_table_name}/index/${each.key}"
scalable_dimension = "dynamodb:index:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "read_policy" {
count = module.this.enabled ? 1 : 0
name = "DynamoDBReadCapacityUtilization:${join("", aws_appautoscaling_target.read_target[*].id)}"
policy_type = "TargetTrackingScaling"
resource_id = join("", aws_appautoscaling_target.read_target[*].resource_id)
scalable_dimension = join("", aws_appautoscaling_target.read_target[*].scalable_dimension)
service_namespace = join("", aws_appautoscaling_target.read_target[*].service_namespace)
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
target_value = var.autoscale_read_target
}
}
resource "aws_appautoscaling_policy" "read_policy_index" {
for_each = module.this.enabled ? toset(var.dynamodb_indexes) : toset([])
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.read_target_index[each.key].id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.read_target_index[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.read_target_index[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.read_target_index[each.key].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
target_value = coalesce(var.autoscale_read_target_index, var.autoscale_read_target)
}
}
resource "aws_appautoscaling_target" "write_target" {
count = module.this.enabled ? 1 : 0
max_capacity = var.autoscale_max_write_capacity
min_capacity = var.autoscale_min_write_capacity
resource_id = "table/${var.dynamodb_table_name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_target" "write_target_index" {
for_each = module.this.enabled ? toset(var.dynamodb_indexes) : toset([])
max_capacity = coalesce(var.autoscale_max_write_capacity_index, var.autoscale_max_write_capacity)
min_capacity = coalesce(var.autoscale_min_write_capacity_index, var.autoscale_min_write_capacity)
resource_id = "table/${var.dynamodb_table_name}/index/${each.key}"
scalable_dimension = "dynamodb:index:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "write_policy" {
count = module.this.enabled ? 1 : 0
name = "DynamoDBWriteCapacityUtilization:${join("", aws_appautoscaling_target.write_target[*].id)}"
policy_type = "TargetTrackingScaling"
resource_id = join("", aws_appautoscaling_target.write_target[*].resource_id)
scalable_dimension = join("", aws_appautoscaling_target.write_target[*].scalable_dimension)
service_namespace = join("", aws_appautoscaling_target.write_target[*].service_namespace)
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}
target_value = var.autoscale_write_target
}
}
resource "aws_appautoscaling_policy" "write_policy_index" {
for_each = module.this.enabled ? toset(var.dynamodb_indexes) : toset([])
name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.write_target_index[each.key].id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.write_target_index[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.write_target_index[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.write_target_index[each.key].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}
target_value = coalesce(var.autoscale_write_target_index, var.autoscale_write_target)
}
}