-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.tf
168 lines (157 loc) · 7.2 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
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
resource "aws_dynamodb_table" "table" {
dynamic "attribute" {
for_each = var.attribute
content {
name = attribute.value.name
type = attribute.value.type
}
}
billing_mode = var.billing_mode
deletion_protection_enabled = var.deletion_protection_enabled
dynamic "global_secondary_index" {
for_each = var.global_secondary_index
content {
hash_key = global_secondary_index.value.hash_key
name = global_secondary_index.value.name
non_key_attributes = lookup(global_secondary_index.value, "non_key_attributes", null)
projection_type = global_secondary_index.value.projection_type
range_key = lookup(global_secondary_index.value, "range_key", null)
read_capacity = lookup(global_secondary_index.value, "read_capacity", null)
write_capacity = lookup(global_secondary_index.value, "write_capacity", null)
}
}
hash_key = var.hash_key
lifecycle {
ignore_changes = [
read_capacity,
write_capacity,
ttl
]
prevent_destroy = true
}
dynamic "local_secondary_index" {
for_each = var.local_secondary_index
content {
name = local_secondary_index.value.name
non_key_attributes = lookup(local_secondary_index.value, "non_key_attributes", null)
projection_type = local_secondary_index.value.projection_type
range_key = local_secondary_index.value.range_key
}
}
name = var.name
point_in_time_recovery {
enabled = var.point_in_time_recovery
}
range_key = var.range_key
read_capacity = var.billing_mode == "PROVISIONED" ? var.read_capacity["min"] : null
server_side_encryption {
enabled = true
}
stream_enabled = length(var.stream_view_type) > 0 ? true : false
stream_view_type = var.stream_view_type
tags = local.tags
ttl {
attribute_name = var.ttl_attribute_name
enabled = length(var.ttl_attribute_name) > 0 ? true : false
}
write_capacity = var.billing_mode == "PROVISIONED" ? var.write_capacity["min"] : null
dynamic "replica" {
for_each = var.replica
content {
region_name = replica.value.region_name
kms_key_arn = lookup(replica.value, "kms_key_arn", null)
}
}
}
resource "aws_appautoscaling_target" "table_read" {
count = var.billing_mode == "PROVISIONED" ? 1 : 0
max_capacity = var.read_capacity["max"]
min_capacity = var.read_capacity["min"]
resource_id = "table/${aws_dynamodb_table.table.name}"
role_arn = var.autoscaling_service_role_arn
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "table_read" {
count = var.billing_mode == "PROVISIONED" ? 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"
}
target_value = 70
}
}
resource "aws_appautoscaling_target" "table_write" {
count = var.billing_mode == "PROVISIONED" ? 1 : 0
max_capacity = var.write_capacity["max"]
min_capacity = var.write_capacity["min"]
resource_id = "table/${aws_dynamodb_table.table.name}"
role_arn = var.autoscaling_service_role_arn
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "table_write" {
count = var.billing_mode == "PROVISIONED" ? 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"
}
target_value = 70
}
}
resource "aws_appautoscaling_target" "global_secondary_index_read" {
count = var.billing_mode == "PROVISIONED" ? local.global_secondary_indexes_count : 0
max_capacity = var.read_capacity["max"]
min_capacity = var.read_capacity["min"]
resource_id = "table/${aws_dynamodb_table.table.name}/index/${var.global_secondary_index[count.index]["name"]}"
role_arn = var.autoscaling_service_role_arn
scalable_dimension = "dynamodb:index:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "global_secondary_index_read" {
count = var.billing_mode == "PROVISIONED" ? local.global_secondary_indexes_count : 0
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.global_secondary_index_read[count.index].resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.global_secondary_index_read[count.index].resource_id
scalable_dimension = aws_appautoscaling_target.global_secondary_index_read[count.index].scalable_dimension
service_namespace = aws_appautoscaling_target.global_secondary_index_read[count.index].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
target_value = 70
}
}
resource "aws_appautoscaling_target" "global_secondary_index_write" {
count = var.billing_mode == "PROVISIONED" ? local.global_secondary_indexes_count : 0
max_capacity = var.write_capacity["max"]
min_capacity = var.write_capacity["min"]
resource_id = "table/${aws_dynamodb_table.table.name}/index/${var.global_secondary_index[count.index]["name"]}"
role_arn = var.autoscaling_service_role_arn
scalable_dimension = "dynamodb:index:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "global_secondary_index_write" {
count = var.billing_mode == "PROVISIONED" ? local.global_secondary_indexes_count : 0
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.global_secondary_index_write[count.index].resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.global_secondary_index_write[count.index].resource_id
scalable_dimension = aws_appautoscaling_target.global_secondary_index_write[count.index].scalable_dimension
service_namespace = aws_appautoscaling_target.global_secondary_index_write[count.index].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}
target_value = 70
}
}