Skip to content

Commit bb9e9a4

Browse files
Jamie-BitFlightaknysh
authored andcommitted
Support additional attributes and secondary indexes (#6)
* Added the ability to add additional attributes, and secondary indexes * Added formatting * Cleaned up readme file formatting * switched to terraform-terraform-label for modern sleekness
1 parent 45cb3dc commit bb9e9a4

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,53 @@ module "dynamodb_table" {
2525
}
2626
```
2727

28+
## Advanced Usage - with additional attributes and indexes
29+
30+
```hcl
31+
module "dynamodb_table" {
32+
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb.git?ref=master"
33+
namespace = "cp"
34+
stage = "dev"
35+
name = "cluster"
36+
hash_key = "HashKey"
37+
range_key = "RangeKey"
38+
autoscale_write_target = 10
39+
autoscale_read_target = 10
40+
autoscale_min_read_capacity = 5
41+
autoscale_max_read_capacity = 20
42+
autoscale_min_write_capacity = 5
43+
autoscale_max_write_capacity = 20
44+
enable_autoscaler = "true"
45+
46+
dynamodb_attributes = [
47+
{
48+
name = "DailyAverage"
49+
type = "N"
50+
},
51+
{
52+
name = "HighWater"
53+
type = "N"
54+
}
55+
]
56+
57+
global_secondary_index_map = [
58+
{
59+
name = "DailyAverageIndex"
60+
hash_key = "DailyAverage"
61+
write_capacity = 10
62+
read_capacity = 10
63+
projection_type = "KEYS_ONLY"
64+
},
65+
{
66+
name = "HighWaterIndex"
67+
hash_key = "HighWater"
68+
write_capacity = 10
69+
read_capacity = 10
70+
projection_type = "KEYS_ONLY"
71+
}
72+
]
73+
}
74+
```
2875

2976
## Variables
3077

@@ -47,7 +94,19 @@ module "dynamodb_table" {
4794
| `autoscale_min_write_capacity` | `5` | DynamoDB autoscaling min write capacity | No |
4895
| `autoscale_max_write_capacity` | `20` | DynamoDB autoscaling max write capacity | No |
4996
| `enable_autoscaler` | `true` | Flag to enable/disable DynamoDB autoscaling | No |
97+
| `dynamodb_attributes` | `[]` | List of maps, that describe extra DynamoDB attributes | No |
98+
| `global_secondary_index_map` | `[]` | List of maps, that describes additional secondary index properties | No |
99+
100+
101+
## A note about DynamoDB attributes
102+
Only define attributes on the table object that are going to be used as:
103+
104+
* Table hash key or range key
105+
* LSI or GSI hash key or range key
106+
107+
The DynamoDB API expects attribute structure (name and type) to be passed along when creating or updating GSI/LSIs or creating the initial table. In these cases it expects the Hash / Range keys to be provided; because these get re-used in numerous places (i.e the table's range key could be a part of one or more GSIs), they are stored on the table object to prevent duplication and increase consistency. If you add attributes here that are not used in these scenarios it can cause an infinite loop in planning.
50108

109+
Additional details see [dynamodb_table](https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html)
51110

52111
## Outputs
53112

main.tf

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module "dynamodb_label" {
2-
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3"
2+
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.1.2"
33
namespace = "${var.namespace}"
44
stage = "${var.stage}"
55
name = "${var.name}"
@@ -8,6 +8,19 @@ module "dynamodb_label" {
88
tags = "${var.tags}"
99
}
1010

11+
locals {
12+
attributes = [{
13+
name = "${var.hash_key}"
14+
type = "S"
15+
},
16+
{
17+
name = "${var.range_key}"
18+
type = "S"
19+
},
20+
"${var.dynamodb_attributes}",
21+
]
22+
}
23+
1124
resource "aws_dynamodb_table" "default" {
1225
name = "${module.dynamodb_label.id}"
1326
read_capacity = "${var.autoscale_min_read_capacity}"
@@ -23,15 +36,8 @@ resource "aws_dynamodb_table" "default" {
2336
ignore_changes = ["read_capacity", "write_capacity"]
2437
}
2538

26-
attribute {
27-
name = "${var.hash_key}"
28-
type = "S"
29-
}
30-
31-
attribute {
32-
name = "${var.range_key}"
33-
type = "S"
34-
}
39+
attribute = ["${local.attributes}"]
40+
global_secondary_index = ["${var.global_secondary_index_map}"]
3541

3642
ttl {
3743
attribute_name = "${var.ttl_attribute}"

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,15 @@ variable "enable_autoscaler" {
8888
default = "true"
8989
description = "Flag to enable/disable DynamoDB autoscaling"
9090
}
91+
92+
variable "dynamodb_attributes" {
93+
type = "list"
94+
default = []
95+
description = "Additional dynamodb attributes in the form of a list of mapped values"
96+
}
97+
98+
variable "global_secondary_index_map" {
99+
type = "list"
100+
default = []
101+
description = "Additional global secondary indexes in the form of a list of mapped values"
102+
}

0 commit comments

Comments
 (0)