Skip to content

Commit a0c3a16

Browse files
jamisonhyattaknysh
authored andcommitted
Implement Local Secondary Index Support (#21)
* updated to use local secondary indexes * updated gsi comment to lsi * ran fmt * updated LSI null resource name * updated GSI null resource name * fixed another timstamp in readme, and made sure md and yaml are in sync.
1 parent e016fc3 commit a0c3a16

File tree

7 files changed

+76
-3
lines changed

7 files changed

+76
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ module "dynamodb_table" {
8181
},
8282
]
8383
84+
local_secondary_index_map = [
85+
{
86+
name = "TimestampSortIndex"
87+
range_key = "Timestamp"
88+
projection_type = "INCLUDE"
89+
non_key_attributes = ["HashKey", "RangeKey"]
90+
},
91+
{
92+
name = "HighWaterIndex"
93+
range_key = "Timestamp"
94+
projection_type = "INCLUDE"
95+
non_key_attributes = ["HashKey", "RangeKey"]
96+
}
97+
]
98+
8499
global_secondary_index_map = [
85100
{
86101
name = "DailyAverageIndex"
@@ -135,6 +150,7 @@ Available targets:
135150
| enable_encryption | Enable DynamoDB server-side encryption | string | `true` | no |
136151
| enable_point_in_time_recovery | Enable DynamoDB point in time recovery | string | `true` | no |
137152
| enable_streams | Enable DynamoDB streams | string | `false` | no |
153+
| local_secondary_index_map | Additional local secondary indexes in the form of a list of mapped values | list | `<list>` | no |
138154
| global_secondary_index_map | Additional global secondary indexes in the form of a list of mapped values | list | `<list>` | no |
139155
| hash_key | DynamoDB table Hash Key | string | - | yes |
140156
| name | Name (e.g. `app` or `cluster`) | string | - | yes |

README.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ usage: |-
112112
},
113113
]
114114
115+
local_secondary_index_map = [
116+
{
117+
name = "TimestampSortIndex"
118+
range_key = "Timestamp"
119+
projection_type = "INCLUDE"
120+
non_key_attributes = ["HashKey", "RangeKey"]
121+
},
122+
{
123+
name = "HighWaterIndex"
124+
range_key = "Timestamp"
125+
projection_type = "INCLUDE"
126+
non_key_attributes = ["HashKey", "RangeKey"]
127+
}
128+
]
129+
115130
global_secondary_index_map = [
116131
{
117132
name = "DailyAverageIndex"

docs/terraform.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
| enable_encryption | Enable DynamoDB server-side encryption | string | `true` | no |
1717
| enable_point_in_time_recovery | Enable DynamoDB point in time recovery | string | `true` | no |
1818
| enable_streams | Enable DynamoDB streams | string | `false` | no |
19+
| local_secondary_index_map | local secondary indexes in the form of a list of mapped values | list | `<list>` | no |
1920
| global_secondary_index_map | Additional global secondary indexes in the form of a list of mapped values | list | `<list>` | no |
2021
| hash_key | DynamoDB table Hash Key | string | - | yes |
2122
| name | Name (e.g. `app` or `cluster`) | string | - | yes |
@@ -30,6 +31,7 @@
3031

3132
| Name | Description |
3233
|------|-------------|
34+
| local_secondary_index_names | DynamoDB local index names |
3335
| global_secondary_index_names | DynamoDB secondary index names |
3436
| table_arn | DynamoDB table ARN |
3537
| table_id | DynamoDB table ID |

examples/complete/main.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ module "dynamodb_table" {
2222
name = "HighWater"
2323
type = "N"
2424
},
25+
{
26+
name = "Timestamp"
27+
type = "S"
28+
},
29+
]
30+
31+
local_secondary_index_map = [
32+
{
33+
name = "TimestampSortIndex"
34+
range_key = "Timestamp"
35+
projection_type = "INCLUDE"
36+
non_key_attributes = ["HashKey", "RangeKey"]
37+
},
38+
{
39+
name = "HighWaterIndex"
40+
range_key = "Timestamp"
41+
projection_type = "INCLUDE"
42+
non_key_attributes = ["HashKey", "RangeKey"]
43+
},
2544
]
2645

2746
global_secondary_index_map = [

main.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ locals {
2828
attributes_final = "${slice(local.attributes, local.from_index, length(local.attributes))}"
2929
}
3030

31-
resource "null_resource" "global_secondary_indexe_names" {
31+
resource "null_resource" "global_secondary_index_names" {
3232
count = "${length(var.global_secondary_index_map)}"
3333

3434
# 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`)
@@ -37,6 +37,15 @@ resource "null_resource" "global_secondary_indexe_names" {
3737
triggers = "${map("name", lookup(var.global_secondary_index_map[count.index], "name"))}"
3838
}
3939

40+
resource "null_resource" "local_secondary_index_names" {
41+
count = "${length(var.local_secondary_index_map)}"
42+
43+
# 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`)
44+
# See `examples/complete`
45+
# https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#non_key_attributes-1
46+
triggers = "${map("name", lookup(var.local_secondary_index_map[count.index], "name"))}"
47+
}
48+
4049
resource "aws_dynamodb_table" "default" {
4150
name = "${module.dynamodb_label.id}"
4251
read_capacity = "${var.autoscale_min_read_capacity}"
@@ -60,6 +69,7 @@ resource "aws_dynamodb_table" "default" {
6069

6170
attribute = ["${local.attributes_final}"]
6271
global_secondary_index = ["${var.global_secondary_index_map}"]
72+
local_secondary_index = ["${var.local_secondary_index_map}"]
6373

6474
ttl {
6575
attribute_name = "${var.ttl_attribute}"
@@ -79,7 +89,7 @@ module "dynamodb_autoscaler" {
7989
attributes = "${var.attributes}"
8090
dynamodb_table_name = "${aws_dynamodb_table.default.id}"
8191
dynamodb_table_arn = "${aws_dynamodb_table.default.arn}"
82-
dynamodb_indexes = ["${null_resource.global_secondary_indexe_names.*.triggers.name}"]
92+
dynamodb_indexes = ["${null_resource.global_secondary_index_names.*.triggers.name}"]
8393
autoscale_write_target = "${var.autoscale_write_target}"
8494
autoscale_read_target = "${var.autoscale_read_target}"
8595
autoscale_min_read_capacity = "${var.autoscale_min_read_capacity}"

outputs.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ output "table_arn" {
1414
}
1515

1616
output "global_secondary_index_names" {
17-
value = ["${null_resource.global_secondary_indexe_names.*.triggers.name}"]
17+
value = ["${null_resource.global_secondary_index_names.*.triggers.name}"]
1818
description = "DynamoDB secondary index names"
1919
}
2020

21+
output "local_secondary_index_names" {
22+
value = ["${null_resource.local_secondary_index_names.*.triggers.name}"]
23+
description = "DynamoDB local index names"
24+
}
25+
2126
output "table_stream_arn" {
2227
value = "${aws_dynamodb_table.default.stream_arn}"
2328
description = "DynamoDB table stream ARN"

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,9 @@ variable "global_secondary_index_map" {
119119
default = []
120120
description = "Additional global secondary indexes in the form of a list of mapped values"
121121
}
122+
123+
variable "local_secondary_index_map" {
124+
type = "list"
125+
default = []
126+
description = "Additional local secondary indexes in the form of a list of mapped values"
127+
}

0 commit comments

Comments
 (0)