Skip to content

Commit 14e5974

Browse files
author
John Titus
authored
Merge pull request #11 from rackspace-infrastructure-automation/MPCSUPENG-867
HCL2\Terraform v0.12 conversion
2 parents 0cd3dfb + abff833 commit 14e5974

File tree

7 files changed

+218
-147
lines changed

7 files changed

+218
-147
lines changed

.terraform-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.12.17

README.md

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ This module creates an AWS DynamoDB table.
66

77
```HCL
88
module "basic" {
9-
source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-dynamo/?ref=v0.0.3"
9+
source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-dynamo/?ref=v0.12.0"
10+
11+
environment = "Test"
12+
hash_key = "MyHashKey"
13+
read_capacity_units = 10
14+
name = "myexampletable"
15+
tags = local.tags
16+
write_capacity_units = 5
1017
1118
attributes = [
1219
{
1320
name = "TestHashKey"
1421
type = "S"
1522
},
1623
]
17-
18-
environment = "Test"
19-
hash_key = "MyHashKey"
20-
read_capacity_units = 10
21-
table_name = "myexampletable"
22-
tags = "${local.tags}"
23-
write_capacity_units = 5
2424
}
2525
```
2626

@@ -40,33 +40,50 @@ The input data was `["data1", "data2", "data3"]` and we can see that attributes
4040

4141
The resolution at the moment is to edit the list in your map to match the order in the state file. This should result in a clean plan when no other changes are present.
4242

43-
**References:**
43+
__References:__
4444

4545
[global secondary index always recreated #3828](https://github.com/terraform-providers/terraform-provider-aws/issues/3828)
4646

4747
[DynamoDB Non-Key Attributes Ordering #3807](https://github.com/terraform-providers/terraform-provider-aws/issues/3807)
4848

49+
## Terraform 0.12 upgrade
50+
51+
Several changes were required while adding terraform 0.12 compatibility. The following changes should be
52+
made when upgrading from a previous release to version 0.12.0 or higher.
53+
54+
### Module variables
55+
56+
The following module variables were updated to better meet current Rackspace style guides:
57+
58+
- `table_name` -> `name`
59+
60+
## Providers
61+
62+
| Name | Version |
63+
|------|---------|
64+
| aws | >= 2.1.0 |
65+
4966
## Inputs
5067

5168
| Name | Description | Type | Default | Required |
52-
|------|-------------|:----:|:-----:|:-----:|
53-
| attributes | List of nested attribute definitions. Only required for hash_key's (always) and range_key's (if used) attributes. Attributes have name and type. Type must be a scalar type: S, N, or B for (S)tring, (N)umber or (B)inary data. i.e. [{ name=<hash_key> type=<data_type>}]. Terraform documentation: [A note about attributes](https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#a-note-about-attributes) | list | n/a | yes |
54-
| enable\_pay\_per\_request | Controls how you are charged for read and write throughput and how you manage capacity. If True, DynamoDB charges you for the data reads and writes your application performs on your tables. You do not need to specify how much read and write throughput you expect your application to perform because DynamoDB instantly accommodates your workloads as they ramp up or down. [On-Demand Pricing](https://aws.amazon.com/dynamodb/pricing/on-demand/) If False, you specify the number of `read_capacity_units` and `write_capacity_units` per second that you expect your workload to require. [Provisioned Pricing](https://aws.amazon.com/dynamodb/pricing/provisioned/) | string | `"false"` | no |
55-
| enable\_ttl | Enable time to live on record. | string | `"false"` | no |
56-
| environment | Application environment for which this resource is being created. Preferred values are Development, Integration, PreProduction, Production, QA, Staging, or Test. | string | `"Development"` | no |
57-
| global\_secondary\_index\_maps | A list of maps for each [global secondary index (GSI)](https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#global_secondary_index-1). Please see [examples](./examples) for usage. | list | `<list>` | no |
58-
| hash\_key | **Forces new resource!** Must contain only alphanumberic characters, dash (-), underscore (_) or dot (.). Needs to be defined by type in attributes. | string | n/a | yes |
59-
| local\_secondary\_index\_maps | A list of maps for each [local secondary index (LSI)](https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#local_secondary_index-1). Please see [examples](./examples) for usage. | list | `<list>` | no |
60-
| point\_in\_time\_recovery | Enable point in time recovery for the table. | string | `"false"` | no |
61-
| range\_key | **Forces new resource!** RangeType PrimaryKey Name. If used, it will need to be defined by type in attributes | string | `""` | no |
62-
| read\_capacity\_units | Provisioned read throughput. Should be between 5 and 10000. Ignored if `enable_pay_per_request` is set to `true`. | string | `"5"` | no |
63-
| stream\_enabled | Enable the stream setting on the table. | string | `"false"` | no |
64-
| stream\_view\_type | If using `stream_enabled, you can specify a valid DynamoDB StreamViewType; must be one of: `KEYS_ONLY`, `NEW_IMAGE`. `OLD_IMAGE`, `NEW_AND_OLD_IMAGES` | string | `""` | no |
65-
| table\_encryption\_cmk | You may choose to use an [AWS Managed CMK](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#aws-managed-cmk) by setting this to `true`. Otherwise, server side table encryption defaults to an [AWS Owned CMK](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#aws-owned-cmk). | string | `"false"` | no |
66-
| table\_name | The name of the table, this needs to be unique within a region. | string | n/a | yes |
67-
| tags | Custom tags to apply to all resources. | map | `<map>` | no |
68-
| ttl\_attribute | The name of the table attribute to store the TTL timestamp in | string | `""` | no |
69-
| write\_capacity\_units | Provisioned write throughput. Should be between 5 and 10000. Ignored if `enable_pay_per_request` is set to `true`. | string | `"10"` | no |
69+
|------|-------------|------|---------|:-----:|
70+
| attributes | List of nested attribute definitions. Only required for hash\_key's (always) and range\_key's (if used) attributes. Attributes have name and type. Type must be a scalar type: S, N, or B for (S)tring, (N)umber or (B)inary data. i.e. [{ name=<hash\_key> type=<data\_type>}]. Terraform documentation: [A note about attributes](https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#a-note-about-attributes) | `list(map(string))` | n/a | yes |
71+
| enable\_pay\_per\_request | Controls how you are charged for read and write throughput and how you manage capacity. If True, DynamoDB charges you for the data reads and writes your application performs on your tables. You do not need to specify how much read and write throughput you expect your application to perform because DynamoDB instantly accommodates your workloads as they ramp up or down. [On-Demand Pricing](https://aws.amazon.com/dynamodb/pricing/on-demand/) If False, you specify the number of `read_capacity_units` and `write_capacity_units` per second that you expect your workload to require. [Provisioned Pricing](https://aws.amazon.com/dynamodb/pricing/provisioned/) | `bool` | `false` | no |
72+
| enable\_ttl | Enable time to live on record. | `bool` | `false` | no |
73+
| environment | Application environment for which this resource is being created. Preferred values are Development, Integration, PreProduction, Production, QA, Staging, or Test. | `string` | `"Development"` | no |
74+
| global\_secondary\_index\_maps | A list of maps for each [global secondary index (GSI)](https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#global_secondary_index-1). Please see [examples](./examples) for usage. | `any` | `[]` | no |
75+
| hash\_key | \*\*Forces new resource!\*\* Must contain only alphanumberic characters, dash (-), underscore (\_) or dot (.). Needs to be defined by type in attributes. | `string` | n/a | yes |
76+
| local\_secondary\_index\_maps | A list of maps for each [local secondary index (LSI)](https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html#local_secondary_index-1). Please see [examples](./examples) for usage. | `any` | `[]` | no |
77+
| name | The name of the table, this needs to be unique within a region. | `string` | n/a | yes |
78+
| point\_in\_time\_recovery | Enable point in time recovery for the table. | `bool` | `false` | no |
79+
| range\_key | \*\*Forces new resource!\*\* RangeType PrimaryKey Name. If used, it will need to be defined by type in attributes | `string` | `""` | no |
80+
| read\_capacity\_units | Provisioned read throughput. Should be between 5 and 10000. Ignored if `enable_pay_per_request` is set to `true`. | `number` | `5` | no |
81+
| stream\_enabled | Enable the stream setting on the table. | `bool` | `false` | no |
82+
| stream\_view\_type | If using `stream_enabled, you can specify a valid DynamoDB StreamViewType; must be one of: `KEYS\_ONLY`, `NEW\_IMAGE`. `OLD\_IMAGE`, `NEW\_AND\_OLD\_IMAGES | `string` | `""` | no |
83+
| table\_encryption\_cmk | You may choose to use an [AWS Managed CMK](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#aws-managed-cmk) by setting this to `true`. Otherwise, server side table encryption defaults to an [AWS Owned CMK](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#aws-owned-cmk). | `bool` | `false` | no |
84+
| tags | Custom tags to apply to all resources. | `map(string)` | `{}` | no |
85+
| ttl\_attribute | The name of the table attribute to store the TTL timestamp in | `string` | `""` | no |
86+
| write\_capacity\_units | Provisioned write throughput. Should be between 5 and 10000. Ignored if `enable_pay_per_request` is set to `true`. | `number` | `10` | no |
7087

7188
## Outputs
7289

examples/example.tf

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}
4+
15
provider "aws" {
2-
version = "~> 1.2"
6+
version = "~> 2.2"
37
region = "us-west-2"
48
}
59

610
module "dynamo_table_provisioned" {
7-
source = "git@github.com/rackspace-infrastructure-automation/aws-terraform-dynamo//?ref=v0.0.6"
11+
source = "git@github.com/rackspace-infrastructure-automation/aws-terraform-dynamo//?ref=v0.12.0"
812

9-
table_name = "<TableName>"
13+
enable_pay_per_request = false
1014
hash_key = "<HashKeyName>"
15+
name = "<TableName>"
16+
point_in_time_recovery = true
1117
range_key = "<RangeKey>"
12-
enable_pay_per_request = false
1318
read_capacity_units = 5
19+
table_encryption = true
1420
write_capacity_units = 10
1521

1622
attributes = [
@@ -23,19 +29,17 @@ module "dynamo_table_provisioned" {
2329
type = "N"
2430
},
2531
]
26-
27-
table_encryption = true
28-
29-
point_in_time_recovery = true
3032
}
3133

3234
module "dynamo_table_pay_per_requst" {
33-
source = "git@github.com/rackspace-infrastructure-automation/aws-terraform-dynamo//?ref=v0.0.6"
35+
source = "git@github.com/rackspace-infrastructure-automation/aws-terraform-dynamo//?ref=v0.12.0"
3436

35-
table_name = "<TableName>"
37+
enable_pay_per_request = true
3638
hash_key = "<HashKeyName>"
39+
name = "<TableName>"
40+
point_in_time_recovery = true
3741
range_key = "<RangeKey>"
38-
enable_pay_per_request = true
42+
table_encryption = true
3943

4044
attributes = [
4145
{
@@ -47,8 +51,5 @@ module "dynamo_table_pay_per_requst" {
4751
type = "N"
4852
},
4953
]
50-
51-
table_encryption = true
52-
53-
point_in_time_recovery = true
5454
}
55+

main.tf

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
*
88
* ```HCL
99
* module "basic" {
10-
* source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-dynamo/?ref=v0.0.3"
10+
* source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-dynamo/?ref=v0.12.0"
11+
*
12+
* environment = "Test"
13+
* hash_key = "MyHashKey"
14+
* read_capacity_units = 10
15+
* name = "myexampletable"
16+
* tags = local.tags
17+
* write_capacity_units = 5
1118
*
1219
* attributes = [
1320
* {
1421
* name = "TestHashKey"
1522
* type = "S"
1623
* },
1724
* ]
18-
*
19-
* environment = "Test"
20-
* hash_key = "MyHashKey"
21-
* read_capacity_units = 10
22-
* table_name = "myexampletable"
23-
* tags = "${local.tags}"
24-
* write_capacity_units = 5
2525
* }
2626
* ```
2727
*
@@ -41,50 +41,97 @@
4141
*
4242
* The resolution at the moment is to edit the list in your map to match the order in the state file. This should result in a clean plan when no other changes are present.
4343
*
44-
* **References:**
44+
* __References:__
4545
*
4646
* [global secondary index always recreated #3828](https://github.com/terraform-providers/terraform-provider-aws/issues/3828)
4747
*
4848
* [DynamoDB Non-Key Attributes Ordering #3807](https://github.com/terraform-providers/terraform-provider-aws/issues/3807)
49+
*
50+
* ## Terraform 0.12 upgrade
51+
*
52+
* Several changes were required while adding terraform 0.12 compatibility. The following changes should be
53+
* made when upgrading from a previous release to version 0.12.0 or higher.
54+
*
55+
* ### Module variables
56+
*
57+
* The following module variables were updated to better meet current Rackspace style guides:
58+
*
59+
* - `table_name` -> `name`
60+
*
4961
*/
5062

63+
terraform {
64+
required_version = ">= 0.12"
65+
66+
required_providers {
67+
aws = ">= 2.1.0"
68+
}
69+
}
70+
5171
locals {
52-
tags {
53-
Environment = "${var.environment}"
54-
Name = "${var.table_name}"
72+
tags = {
73+
Environment = var.environment
74+
Name = var.name
5575
ServiceProvider = "Rackspace"
5676
}
5777
}
5878

5979
resource "aws_dynamodb_table" "table" {
60-
billing_mode = "${var.enable_pay_per_request ? "PAY_PER_REQUEST" : "PROVISIONED" }"
61-
hash_key = "${var.hash_key}"
62-
name = "${var.table_name}"
63-
range_key = "${var.range_key}"
64-
read_capacity = "${var.enable_pay_per_request ? 0 : var.read_capacity_units}"
65-
stream_enabled = "${var.stream_enabled}"
66-
stream_view_type = "${var.stream_enabled ? var.stream_view_type : "" }"
67-
write_capacity = "${var.enable_pay_per_request ? 0 : var.write_capacity_units}"
80+
billing_mode = var.enable_pay_per_request ? "PAY_PER_REQUEST" : "PROVISIONED"
81+
hash_key = var.hash_key
82+
name = var.name
83+
range_key = var.range_key
84+
read_capacity = var.enable_pay_per_request ? 0 : var.read_capacity_units
85+
stream_enabled = var.stream_enabled
86+
stream_view_type = var.stream_enabled ? var.stream_view_type : ""
87+
tags = merge(local.tags, var.tags)
88+
write_capacity = var.enable_pay_per_request ? 0 : var.write_capacity_units
89+
90+
dynamic "attribute" {
91+
for_each = var.attributes
92+
93+
content {
94+
name = attribute.value.name
95+
type = attribute.value.type
96+
}
97+
}
6898

69-
attribute = "${var.attributes}"
70-
global_secondary_index = "${var.global_secondary_index_maps}"
71-
local_secondary_index = "${var.local_secondary_index_maps}"
99+
dynamic "global_secondary_index" {
100+
for_each = var.global_secondary_index_maps
101+
102+
content {
103+
hash_key = global_secondary_index.value.hash_key
104+
name = global_secondary_index.value.name
105+
non_key_attributes = lookup(global_secondary_index.value, "non_key_attributes", null)
106+
projection_type = global_secondary_index.value.projection_type
107+
range_key = lookup(global_secondary_index.value, "range_key", null)
108+
read_capacity = lookup(global_secondary_index.value, "read_capacity", null)
109+
write_capacity = lookup(global_secondary_index.value, "write_capacity", null)
110+
}
111+
}
112+
113+
dynamic "local_secondary_index" {
114+
for_each = var.local_secondary_index_maps
115+
116+
content {
117+
name = local_secondary_index.value.name
118+
non_key_attributes = lookup(local_secondary_index.value, "non_key_attributes", null)
119+
projection_type = local_secondary_index.value.projection_type
120+
range_key = local_secondary_index.value.range_key
121+
}
122+
}
72123

73124
point_in_time_recovery {
74-
enabled = "${var.point_in_time_recovery}"
125+
enabled = var.point_in_time_recovery
75126
}
76127

77128
server_side_encryption {
78-
enabled = "${var.table_encryption_cmk}"
129+
enabled = var.table_encryption_cmk
79130
}
80131

81132
ttl {
82-
enabled = "${var.enable_ttl}"
83-
attribute_name = "${var.ttl_attribute}"
133+
enabled = var.enable_ttl
134+
attribute_name = var.ttl_attribute
84135
}
85-
86-
tags = "${merge(
87-
local.tags,
88-
var.tags
89-
)}"
90136
}
137+

outputs.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
output "stream_arn" {
22
description = "ARN for the stream if `stream_enabled` was set to `true`, otherwise returns a string of \"null\"."
3-
value = "${var.stream_enabled == "true" ? aws_dynamodb_table.table.stream_arn : "null" }"
3+
value = var.stream_enabled == "true" ? aws_dynamodb_table.table.stream_arn : "null"
44
}
55

66
output "table_arn" {
77
description = "Table ARN"
8-
value = "${aws_dynamodb_table.table.arn}"
8+
value = aws_dynamodb_table.table.arn
99
}
1010

1111
output "table_name" {
1212
description = "Table Name"
13-
value = "${aws_dynamodb_table.table.id}"
13+
value = aws_dynamodb_table.table.id
1414
}
15+

0 commit comments

Comments
 (0)