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
|
# DynamoDB tables ============================================================
resource "aws_dynamodb_table" "this" {
for_each = var.tables
name = each.value.name
billing_mode = each.value.billing_mode
read_capacity = each.value.billing_mode == "PROVISIONED" ? each.value.read_capacity : null
write_capacity = each.value.billing_mode == "PROVISIONED" ? each.value.write_capacity : null
hash_key = each.value.hash_key
range_key = each.value.range_key
table_class = each.value.table_class
stream_enabled = each.value.stream_enabled
stream_view_type = each.value.stream_enabled ? each.value.stream_view_type : null
server_side_encryption {
enabled = true
kms_key_arn = each.value.kms_key_arn
}
point_in_time_recovery {
enabled = each.value.point_in_time_recovery
}
dynamic "ttl" {
for_each = each.value.ttl_attribute != null ? [each.value.ttl_attribute] : []
content {
attribute_name = ttl.value
enabled = true
}
}
dynamic "attribute" {
for_each = each.value.attributes
content {
name = attribute.value.name
type = attribute.value.type
}
}
dynamic "global_secondary_index" {
for_each = each.value.global_secondary_indexes
content {
name = global_secondary_index.value.name
hash_key = global_secondary_index.value.hash_key
range_key = global_secondary_index.value.range_key
projection_type = global_secondary_index.value.projection_type
non_key_attributes = global_secondary_index.value.projection_type == "INCLUDE" ? global_secondary_index.value.non_key_attributes : null
read_capacity = each.value.billing_mode == "PROVISIONED" ? global_secondary_index.value.read_capacity : null
write_capacity = each.value.billing_mode == "PROVISIONED" ? global_secondary_index.value.write_capacity : null
}
}
dynamic "local_secondary_index" {
for_each = each.value.local_secondary_indexes
content {
name = local_secondary_index.value.name
range_key = local_secondary_index.value.range_key
projection_type = local_secondary_index.value.projection_type
non_key_attributes = local_secondary_index.value.projection_type == "INCLUDE" ? local_secondary_index.value.non_key_attributes : null
}
}
tags = local.tags
}
# Application Auto Scaling for provisioned tables ============================
resource "aws_appautoscaling_target" "read" {
for_each = local.tables_with_autoscaling
max_capacity = each.value.autoscaling.read_max_capacity
min_capacity = each.value.autoscaling.read_min_capacity
resource_id = "table/${aws_dynamodb_table.this[each.key].name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "read" {
for_each = local.tables_with_autoscaling
name = "${each.value.name}-read-scaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.read[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.read[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.read[each.key].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
target_value = each.value.autoscaling.target_utilization
}
}
resource "aws_appautoscaling_target" "write" {
for_each = local.tables_with_autoscaling
max_capacity = each.value.autoscaling.write_max_capacity
min_capacity = each.value.autoscaling.write_min_capacity
resource_id = "table/${aws_dynamodb_table.this[each.key].name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "write" {
for_each = local.tables_with_autoscaling
name = "${each.value.name}-write-scaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.write[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.write[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.write[each.key].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}
target_value = each.value.autoscaling.target_utilization
}
}
|