Skip to content

Commit d474d24

Browse files
authored
Make range_key optional (#17)
1 parent 4bf4dd7 commit d474d24

File tree

7 files changed

+38
-28
lines changed

7 files changed

+38
-28
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export README_DEPS ?= docs/targets.md docs/terraform.md
77

88
## Lint terraform code
99
lint:
10-
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate
10+
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ It's 100% Open Source and licensed under the [APACHE2](LICENSE).
3535
```hcl
3636
module "dynamodb_table" {
3737
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb.git?ref=master"
38-
namespace = "cp"
38+
namespace = "eg"
3939
stage = "dev"
4040
name = "cluster"
4141
hash_key = "HashKey"
@@ -55,7 +55,7 @@ module "dynamodb_table" {
5555
```hcl
5656
module "dynamodb_table" {
5757
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb.git?ref=master"
58-
namespace = "cp"
58+
namespace = "eg"
5959
stage = "dev"
6060
name = "cluster"
6161
hash_key = "HashKey"
@@ -107,8 +107,9 @@ module "dynamodb_table" {
107107
```
108108
Available targets:
109109
110-
help This help screen
110+
help Help screen
111111
help/all Display help for all targets
112+
help/short This help short screen
112113
lint Lint terraform code
113114
114115
```
@@ -133,11 +134,11 @@ Available targets:
133134
| global_secondary_index_map | Additional global secondary indexes in the form of a list of mapped values | list | `<list>` | no |
134135
| hash_key | DynamoDB table Hash Key | string | - | yes |
135136
| name | Name (e.g. `app` or `cluster`) | string | - | yes |
136-
| namespace | Namespace (e.g. `cp` or `cloudposse`) | string | - | yes |
137-
| range_key | DynamoDB table Range Key | string | - | yes |
137+
| namespace | Namespace (e.g. `eg` or `cp`) | string | - | yes |
138+
| range_key | DynamoDB table Range Key | string | `` | no |
138139
| stage | Stage (e.g. `prod`, `dev`, `staging`, `infra`) | string | - | yes |
139140
| stream_view_type | When an item in the table is modified, what information is written to the stream | string | `` | no |
140-
| tags | Additional tags (e.g. map('BusinessUnit`,`XYZ`) | map | `<map>` | no |
141+
| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`) | map | `<map>` | no |
141142
| ttl_attribute | DynamoDB table TTL attribute | string | `Expires` | no |
142143

143144
## Outputs

README.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ related:
3737
url: "https://github.com/cloudposse/terraform-aws-tfstate-backend"
3838
- name: "terraform-aws-elasticache-redis"
3939
description: "Terraform module to provision an ElastiCache Redis Cluster"
40-
url: "https://github.com/cloudposse/terraform-aws-elasticache-redis"
40+
url: "https://github.com/cloudposse/terraform-aws-elasticache-redis"
4141
- name: "terraform-aws-elasticache-memcached"
4242
description: "Terraform Module for ElastiCache Memcached Cluster"
43-
url: "https://github.com/cloudposse/terraform-aws-elasticache-memcached"
43+
url: "https://github.com/cloudposse/terraform-aws-elasticache-memcached"
4444
- name: "terraform-aws-elasticache-cloudwatch-sns-alarms"
4545
description: "Terraform module that configures CloudWatch SNS alerts for ElastiCache"
46-
url: "https://github.com/cloudposse/terraform-aws-elasticache-cloudwatch-sns-alarms"
46+
url: "https://github.com/cloudposse/terraform-aws-elasticache-cloudwatch-sns-alarms"
4747
- name: "terraform-aws-rds-cluster"
4848
description: "Terraform module to provision an RDS Aurora cluster for MySQL or Postgres"
49-
url: "https://github.com/cloudposse/terraform-aws-rds-cluster"
49+
url: "https://github.com/cloudposse/terraform-aws-rds-cluster"
5050
- name: "terraform-aws-rds"
5151
description: "Terraform module to provision AWS RDS instances"
52-
url: "https://github.com/cloudposse/terraform-aws-rds"
52+
url: "https://github.com/cloudposse/terraform-aws-rds"
5353

5454
# Short description of this project
5555
description: |-
@@ -66,7 +66,7 @@ usage: |-
6666
```hcl
6767
module "dynamodb_table" {
6868
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb.git?ref=master"
69-
namespace = "cp"
69+
namespace = "eg"
7070
stage = "dev"
7171
name = "cluster"
7272
hash_key = "HashKey"
@@ -86,7 +86,7 @@ usage: |-
8686
```hcl
8787
module "dynamodb_table" {
8888
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb.git?ref=master"
89-
namespace = "cp"
89+
namespace = "eg"
9090
stage = "dev"
9191
name = "cluster"
9292
hash_key = "HashKey"
@@ -142,4 +142,4 @@ contributors:
142142
- name: "Jamie Nelson"
143143
github: "Jamie-BitFlight"
144144
- name: "Daren Desjardins"
145-
github: "darend"
145+
github: "darend"

docs/targets.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
```
33
Available targets:
44
5-
help This help screen
5+
help Help screen
66
help/all Display help for all targets
7+
help/short This help short screen
78
lint Lint terraform code
89
910
```

docs/terraform.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
| global_secondary_index_map | Additional global secondary indexes in the form of a list of mapped values | list | `<list>` | no |
2020
| hash_key | DynamoDB table Hash Key | string | - | yes |
2121
| name | Name (e.g. `app` or `cluster`) | string | - | yes |
22-
| namespace | Namespace (e.g. `cp` or `cloudposse`) | string | - | yes |
23-
| range_key | DynamoDB table Range Key | string | - | yes |
22+
| namespace | Namespace (e.g. `eg` or `cp`) | string | - | yes |
23+
| range_key | DynamoDB table Range Key | string | `` | no |
2424
| stage | Stage (e.g. `prod`, `dev`, `staging`, `infra`) | string | - | yes |
2525
| stream_view_type | When an item in the table is modified, what information is written to the stream | string | `` | no |
26-
| tags | Additional tags (e.g. map('BusinessUnit`,`XYZ`) | map | `<map>` | no |
26+
| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`) | map | `<map>` | no |
2727
| ttl_attribute | DynamoDB table TTL attribute | string | `Expires` | no |
2828

2929
## Outputs

main.tf

Lines changed: 14 additions & 7 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-terraform-label.git?ref=tags/0.1.2"
2+
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.1.6"
33
namespace = "${var.namespace}"
44
stage = "${var.stage}"
55
name = "${var.name}"
@@ -9,16 +9,23 @@ module "dynamodb_label" {
99
}
1010

1111
locals {
12-
attributes = [{
13-
name = "${var.hash_key}"
14-
type = "S"
15-
},
12+
attributes = [
1613
{
1714
name = "${var.range_key}"
1815
type = "S"
1916
},
17+
{
18+
name = "${var.hash_key}"
19+
type = "S"
20+
},
2021
"${var.dynamodb_attributes}",
2122
]
23+
24+
# Use the `slice` pattern (instead of `conditional`) to remove the first map from the list if no `range_key` is provided
25+
# Terraform does not support conditionals with `lists` and `maps`: aws_dynamodb_table.default: conditional operator cannot be used with list values
26+
from_index = "${length(var.range_key) > 0 ? 0 : 1}"
27+
28+
attributes_final = "${slice(local.attributes, local.from_index, length(local.attributes))}"
2229
}
2330

2431
resource "null_resource" "global_secondary_indexes" {
@@ -47,7 +54,7 @@ resource "aws_dynamodb_table" "default" {
4754
ignore_changes = ["read_capacity", "write_capacity"]
4855
}
4956

50-
attribute = ["${local.attributes}"]
57+
attribute = ["${local.attributes_final}"]
5158
global_secondary_index = ["${var.global_secondary_index_map}"]
5259

5360
ttl {
@@ -59,7 +66,7 @@ resource "aws_dynamodb_table" "default" {
5966
}
6067

6168
module "dynamodb_autoscaler" {
62-
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb-autoscaler.git?ref=tags/0.2.1"
69+
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb-autoscaler.git?ref=tags/0.2.4"
6370
enabled = "${var.enable_autoscaler}"
6471
namespace = "${var.namespace}"
6572
stage = "${var.stage}"

variables.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
variable "namespace" {
22
type = "string"
3-
description = "Namespace (e.g. `cp` or `cloudposse`)"
3+
description = "Namespace (e.g. `eg` or `cp`)"
44
}
55

66
variable "stage" {
@@ -28,7 +28,7 @@ variable "attributes" {
2828
variable "tags" {
2929
type = "map"
3030
default = {}
31-
description = "Additional tags (e.g. map('BusinessUnit`,`XYZ`)"
31+
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)"
3232
}
3333

3434
variable "autoscale_write_target" {
@@ -92,6 +92,7 @@ variable "hash_key" {
9292

9393
variable "range_key" {
9494
type = "string"
95+
default = ""
9596
description = "DynamoDB table Range Key"
9697
}
9798

0 commit comments

Comments
 (0)