|
| 1 | +locals { |
| 2 | + autoscaling_enabled = length(var.autoscaling_read) + length(var.autoscaling_write) + length(var.autoscaling_indexes) > 0 ? true : false |
| 3 | + create_normal_table = var.create_table && !local.autoscaling_enabled ? 1 : 0 |
| 4 | + create_autoscaled_table = var.create_table && local.autoscaling_enabled ? 1 : 0 |
| 5 | +} |
| 6 | + |
1 | 7 | resource "aws_dynamodb_table" "this" {
|
2 |
| - count = var.create_table ? 1 : 0 |
| 8 | + count = local.create_normal_table |
3 | 9 |
|
4 | 10 | name = var.name
|
5 | 11 | billing_mode = var.billing_mode
|
@@ -80,3 +86,90 @@ resource "aws_dynamodb_table" "this" {
|
80 | 86 | update = lookup(var.timeouts, "update", null)
|
81 | 87 | }
|
82 | 88 | }
|
| 89 | + |
| 90 | +resource "aws_dynamodb_table" "autoscaled" { |
| 91 | + count = local.create_autoscaled_table |
| 92 | + |
| 93 | + name = var.name |
| 94 | + billing_mode = var.billing_mode |
| 95 | + hash_key = var.hash_key |
| 96 | + range_key = var.range_key |
| 97 | + read_capacity = var.read_capacity |
| 98 | + write_capacity = var.write_capacity |
| 99 | + stream_enabled = var.stream_enabled |
| 100 | + stream_view_type = var.stream_view_type |
| 101 | + |
| 102 | + ttl { |
| 103 | + enabled = var.ttl_enabled |
| 104 | + attribute_name = var.ttl_attribute_name |
| 105 | + } |
| 106 | + |
| 107 | + point_in_time_recovery { |
| 108 | + enabled = var.point_in_time_recovery_enabled |
| 109 | + } |
| 110 | + |
| 111 | + dynamic "attribute" { |
| 112 | + for_each = var.attributes |
| 113 | + |
| 114 | + content { |
| 115 | + name = attribute.value.name |
| 116 | + type = attribute.value.type |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + dynamic "local_secondary_index" { |
| 121 | + for_each = var.local_secondary_indexes |
| 122 | + |
| 123 | + content { |
| 124 | + name = local_secondary_index.value.name |
| 125 | + range_key = local_secondary_index.value.range_key |
| 126 | + projection_type = local_secondary_index.value.projection_type |
| 127 | + non_key_attributes = lookup(local_secondary_index.value, "non_key_attributes", null) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + dynamic "global_secondary_index" { |
| 132 | + for_each = var.global_secondary_indexes |
| 133 | + |
| 134 | + content { |
| 135 | + name = global_secondary_index.value.name |
| 136 | + hash_key = global_secondary_index.value.hash_key |
| 137 | + projection_type = global_secondary_index.value.projection_type |
| 138 | + range_key = lookup(global_secondary_index.value, "range_key", null) |
| 139 | + read_capacity = lookup(global_secondary_index.value, "read_capacity", null) |
| 140 | + write_capacity = lookup(global_secondary_index.value, "write_capacity", null) |
| 141 | + non_key_attributes = lookup(global_secondary_index.value, "non_key_attributes", null) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + dynamic "replica" { |
| 146 | + for_each = var.replica_regions |
| 147 | + |
| 148 | + content { |
| 149 | + region_name = replica.value.region_name |
| 150 | + kms_key_arn = lookup(replica.value, "kms_key_arn", null) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + server_side_encryption { |
| 155 | + enabled = var.server_side_encryption_enabled |
| 156 | + kms_key_arn = var.server_side_encryption_kms_key_arn |
| 157 | + } |
| 158 | + |
| 159 | + tags = merge( |
| 160 | + var.tags, |
| 161 | + { |
| 162 | + "Name" = format("%s", var.name) |
| 163 | + }, |
| 164 | + ) |
| 165 | + |
| 166 | + timeouts { |
| 167 | + create = lookup(var.timeouts, "create", null) |
| 168 | + delete = lookup(var.timeouts, "delete", null) |
| 169 | + update = lookup(var.timeouts, "update", null) |
| 170 | + } |
| 171 | + |
| 172 | + lifecycle { |
| 173 | + ignore_changes = [read_capacity, write_capacity] |
| 174 | + } |
| 175 | +} |
0 commit comments