forked from cloudposse/terraform-aws-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
101 lines (86 loc) · 4.33 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
module "dynamodb_label" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.2.1"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
delimiter = "${var.delimiter}"
attributes = "${var.attributes}"
tags = "${var.tags}"
}
locals {
attributes = [
{
name = "${var.range_key}"
type = "${var.range_key_type}"
},
{
name = "${var.hash_key}"
type = "${var.hash_key_type}"
},
"${var.dynamodb_attributes}",
]
# Use the `slice` pattern (instead of `conditional`) to remove the first map from the list if no `range_key` is provided
# Terraform does not support conditionals with `lists` and `maps`: aws_dynamodb_table.default: conditional operator cannot be used with list values
from_index = "${length(var.range_key) > 0 ? 0 : 1}"
attributes_final = "${slice(local.attributes, local.from_index, length(local.attributes))}"
}
resource "null_resource" "global_secondary_index_names" {
count = "${(var.enabled == "true" ? 1 : 0 ) * length(var.global_secondary_index_map)}"
# Convert the multi-item `global_secondary_index_map` into a simple `map` with just one item `name` since `triggers` does not support `lists` in `maps` (which are used in `non_key_attributes`)
# See `examples/complete`
# https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#non_key_attributes-1
triggers = "${map("name", lookup(var.global_secondary_index_map[count.index], "name"))}"
}
resource "null_resource" "local_secondary_index_names" {
count = "${(var.enabled == "true" ? 1 : 0 ) * length(var.local_secondary_index_map)}"
# Convert the multi-item `local_secondary_index_map` into a simple `map` with just one item `name` since `triggers` does not support `lists` in `maps` (which are used in `non_key_attributes`)
# See `examples/complete`
# https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#non_key_attributes-1
triggers = "${map("name", lookup(var.local_secondary_index_map[count.index], "name"))}"
}
resource "aws_dynamodb_table" "default" {
count = "${var.enabled == "true" ? 1 : 0 }"
name = "${var.simple_name == "true" ? var.name : module.dynamodb_label.id}"
billing_mode = "${var.billing_mode}"
read_capacity = "${var.autoscale_min_read_capacity}"
write_capacity = "${var.autoscale_min_write_capacity}"
hash_key = "${var.hash_key}"
range_key = "${var.range_key}"
stream_enabled = "${var.enable_streams}"
stream_view_type = "${var.enable_streams == "true" ? var.stream_view_type : ""}"
server_side_encryption {
enabled = "${var.enable_encryption}"
}
point_in_time_recovery {
enabled = "${var.enable_point_in_time_recovery}"
}
lifecycle {
ignore_changes = ["read_capacity", "write_capacity"]
}
attribute = ["${local.attributes_final}"]
global_secondary_index = ["${var.global_secondary_index_map}"]
local_secondary_index = ["${var.local_secondary_index_map}"]
ttl {
attribute_name = "${var.ttl_attribute}"
enabled = true
}
tags = "${module.dynamodb_label.tags}"
}
module "dynamodb_autoscaler" {
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb-autoscaler.git?ref=tags/0.2.5"
enabled = "${var.enabled == "true" && var.enable_autoscaler == "true" && var.billing_mode == "PROVISIONED"}"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
delimiter = "${var.delimiter}"
attributes = "${var.attributes}"
dynamodb_table_name = "${element(concat(aws_dynamodb_table.default.*.id, list("")), 0)}"
dynamodb_table_arn = "${element(concat(aws_dynamodb_table.default.*.arn, list("")), 0)}"
dynamodb_indexes = ["${null_resource.global_secondary_index_names.*.triggers.name}"]
autoscale_write_target = "${var.autoscale_write_target}"
autoscale_read_target = "${var.autoscale_read_target}"
autoscale_min_read_capacity = "${var.autoscale_min_read_capacity}"
autoscale_max_read_capacity = "${var.autoscale_max_read_capacity}"
autoscale_min_write_capacity = "${var.autoscale_min_write_capacity}"
autoscale_max_write_capacity = "${var.autoscale_max_write_capacity}"
}