-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
131 lines (116 loc) · 4.24 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
terraform {
required_version = ">= 1.0.8"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.0.0"
}
}
}
locals {
all_attribs = concat(
[
{
name = var.hash_key
type = var.hash_key_type
}
],
var.range_key != "" && var.range_key != null ?
[
{
name = var.range_key
type = var.range_key_type
}
] : [],
var.attributes
)
}
resource "aws_dynamodb_table" "table" {
name = var.table_name
billing_mode = var.billing_mode
read_capacity = var.billing_mode == "PROVISIONED" ? var.min_read_capacity : null
write_capacity = var.billing_mode == "PROVISIONED" ? var.min_write_capacity : null
hash_key = var.hash_key
range_key = var.range_key
stream_enabled = var.stream_enabled
stream_view_type = var.stream_enabled ? var.stream_view_type : null
dynamic "attribute" {
for_each = local.all_attribs
content {
name = attribute.value.name
type = attribute.value.type
}
}
dynamic "ttl" {
for_each = var.ttl_enabled ? [1] : []
content {
attribute_name = var.ttl_attribute
enabled = true
}
}
dynamic "global_secondary_index" {
for_each = var.global_secondary_indices
content {
hash_key = global_secondary_index.value.hash_key
name = global_secondary_index.value.name
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)
non_key_attributes = lookup(global_secondary_index.value, "non_key_attributes", null)
}
}
point_in_time_recovery {
enabled = var.enable_point_in_time_recovery
}
lifecycle {
ignore_changes = [
read_capacity,
write_capacity
]
}
}
resource "aws_appautoscaling_target" "read_capacity_autoscaling_target" {
count = var.billing_mode == "PROVISIONED" ? 1 : 0
min_capacity = var.min_read_capacity
max_capacity = var.max_read_capacity
resource_id = "table/${aws_dynamodb_table.table.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "read_capacity_autoscaling_policy" {
count = var.billing_mode == "PROVISIONED" ? 1 : 0
name = "${aws_dynamodb_table.table.name}-read-capacity-autoscaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.read_capacity_autoscaling_target[0].resource_id
scalable_dimension = aws_appautoscaling_target.read_capacity_autoscaling_target[0].scalable_dimension
service_namespace = aws_appautoscaling_target.read_capacity_autoscaling_target[0].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
target_value = 70
}
}
resource "aws_appautoscaling_target" "write_capacity_autoscaling_target" {
count = var.billing_mode == "PROVISIONED" ? 1 : 0
min_capacity = var.min_write_capacity
max_capacity = var.max_write_capacity
resource_id = "table/${aws_dynamodb_table.table.name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "write_capacity_autoscaling_policy" {
count = var.billing_mode == "PROVISIONED" ? 1 : 0
name = "${aws_dynamodb_table.table.name}-write-capacity-autoscaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.write_capacity_autoscaling_target[0].resource_id
scalable_dimension = aws_appautoscaling_target.write_capacity_autoscaling_target[0].scalable_dimension
service_namespace = aws_appautoscaling_target.write_capacity_autoscaling_target[0].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}
target_value = 70
}
}