Skip to content

Commit 22ba0e3

Browse files
committed
upgraded documentation in preparation for module community release
1 parent d20a53e commit 22ba0e3

File tree

5 files changed

+97
-119
lines changed

5 files changed

+97
-119
lines changed

README.md

Lines changed: 44 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
# PostgreSQL RDS | Enterprise Grade | Terraform Module
2+
# PostgreSQL RDS | Create from Snapshot or Create New
33

4-
Provision either a new **enterprise grade** PostgreSQL RDS database or **create a clone of another database from its snapshot**. In this context enterprise grade means
4+
Provision _either_ a new **enterprise grade** PostgreSQL RDS database _or_ **create a clone of another database from its snapshot**. In this context enterprise grade means
55
- a 48 long password chosen from a set of 70 characters
66
- a non predictable master database username string
77
- a high redundancy multi-availability zone database
@@ -10,118 +10,88 @@ Provision either a new **enterprise grade** PostgreSQL RDS database or **create
1010
- robust options for backup (maintenance) windows and retention period
1111
- sensible descriptive resource tags
1212

13-
## From Snapshot or New
1413

15-
This module will conditionally **instantiate from a snapshot** depending on a boolean variable that you provide.
16-
17-
## integration test | Jenkinsfile
18-
19-
This module comes with an **[integraion test](integration/postgres.test-main.tf)** and a Jenkinsfile so you know that it has been validated day in, day out. It doesn't grow stale and stop working like many other Terraform modules.
20-
21-
## Test Drive | Create Two Databases
22-
23-
Why not test drive this PostgreSQL terraform module.
14+
---
2415

25-
```
26-
git clone https://github.com/devops4me/terraform-aws-postgres-rds
27-
cd terraform-aws-postgres-rds/integration
28-
# Export your AWS Credentials and Region
29-
terraform init
30-
terraform deploy
31-
```
3216

33-
## Usage | Creating New and Cloned Databases
17+
## Module Usage | Create New and/or Cloned RDS DBs
3418

35-
This is a small insight
19+
This module will conditionally **instantiate from a snapshot** depending on whether **`var.in_clone_snapshot`** is true or false. If true the RDS ID of the database to clone must be provided. This last available snapshot is chosen as the baseline for creating the cloned database.
3620

3721
```
38-
locals {
39-
ecosystem_name = "canary"
40-
fresh_db_name = "freshdb"
41-
clone_db_name = "clonedb"
42-
}
43-
4422
module fresh_db {
4523
46-
source = "github.com/devops4me/terraform-aws-postgres-rds"
24+
source = "devops4me/postgres-rds/aws"
25+
version = "1.0.1"
4726
4827
in_security_group_id = module.security-group.out_security_group_id
4928
in_db_subnet_ids = module.vpc-network.out_private_subnet_ids
50-
in_database_name = local.fresh_db_name
51-
52-
in_ecosystem_name = local.ecosystem_name
53-
in_tag_timestamp = module.resource-tags.out_tag_timestamp
54-
in_tag_description = module.resource-tags.out_tag_description
29+
in_database_name = "fresh_db"
5530
}
5631
5732
module clone_db {
5833
59-
source = "github.com/devops4me/terraform-aws-postgres-rds"
34+
source = "devops4me/postgres-rds/aws"
35+
version = "1.0.1"
6036
61-
in_security_group_id = module.security-group.out_security_group_id
62-
in_db_subnet_ids = module.vpc-network.out_private_subnet_ids
37+
in_clone_snapshot = true
6338
in_id_of_db_to_clone = var.in_id_of_db_to_clone
64-
in_clone_snapshot = true
6539
66-
in_database_name = local.clone_db_name
67-
68-
in_ecosystem_name = local.ecosystem_name
69-
in_tag_timestamp = module.resource-tags.out_tag_timestamp
70-
in_tag_description = module.resource-tags.out_tag_description
40+
in_security_group_id = module.security-group.out_security_group_id
41+
in_db_subnet_ids = module.vpc-network.out_private_subnet_ids
42+
in_database_name = "cloned_db"
7143
}
7244
```
7345

7446
The important outputs are the **out_database_hostname**, **out_database_username** and the **out_database_password**.
7547

7648
Look at the integration test for the bells and whistles that terraform demands.
7749

50+
7851
---
7952

53+
8054
## Inputs
8155

8256
This AWS PostgreSQL database terraform module requires these input variables.
8357
**Important - the database name must start with a letter (not a digit) and can only contain alphanumerics.**
8458

85-
| Imported | Type | Comment |
59+
| Input Variable | Type | Comment |
8660
|:-------- |:---- |:------- |
87-
**in_security_group_id** | String | security group constraining database traffic flows
88-
**in_db_subnet_ids** | List | private subnet IDs in which to create the database
89-
**in_database_name** | String | alphanumeric only name to give the new database
90-
61+
**in_database_name** | String | **alphanumeric only name** to give the new database and it must start with a letter - note that providing a different name causes terraform to create a different database
62+
**in_clone_snapshot** | Boolean | if true the in_id_of_db_to_clone must be provided and will cause the database to be created from the last available snapshot of the specified database
63+
**in_id_of_db_to_clone** | String | this ID must be provided if **`in_clone_snapshot`** is true causing the database to be created from the last available snapshot
64+
**in_security_group_id** | String | the security group that will constrain traffic to and from the database
65+
**in_db_subnet_ids** | List | list of private subnet IDs in at least two availability zones (see example) for housing database instances
9166

92-
## Outputs
9367

94-
| Exported | Type | Comment |
95-
|:------------------------ |:------ |:------- |
96-
**out_database_hostname** | String | The addressable hostname of the database
97-
**out_database_password** | String | password protecting the database account
68+
### Resource Tag Inputs
9869

70+
Most organisations have a mandatory set of tags that must be placed on AWS resources for cost and billing reports. Typically they denote owners and specify whether environments are prod or non-prod.
9971

100-
### Contributing
10172

102-
Bug reports and pull requests are welcome on GitHub at the https://github.com/devops4me/terraform-aws-postgres-rds page. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
73+
Additionally you can denote
10374

104-
License
105-
-------
75+
| Input Variable | Variable Description | Input Example
76+
|:----------------- |:-------------------- |:----- |
77+
**`in_ecosystem`** | the ecosystem (environment) name these resources belong to | **`my-app-test`** or **`kubernetes-cluster`**
78+
**`in_timestamp`** | the timestamp in resource names helps you identify which environment instance resources belong to | **`1911021435`** as **`$(date +%y%m%d%H%M%S)`**
79+
**`in_description`** | a human readable description usually stating who is creating the resource and when and where | "was created by $USER@$HOSTNAME on $(date)."
10680

107-
MIT License
108-
Copyright (c) 2006 - 2014
81+
Try **`echo $(date +%y%m%d%H%M%S)`** to check your timestamp and **`echo "was created by $USER@$HOSTNAME on $(date)."`** to check your description. Here is how you can send these values to terraform.
10982

110-
Permission is hereby granted, free of charge, to any person obtaining
111-
a copy of this software and associated documentation files (the
112-
'Software'), to deal in the Software without restriction, including
113-
without limitation the rights to use, copy, modify, merge, publish,
114-
distribute, sublicense, and/or sell copies of the Software, and to
115-
permit persons to whom the Software is furnished to do so, subject to
116-
the following conditions:
83+
```
84+
$ export TF_VAR_in_timestamp=$(date +%y%m%d%H%M%S)
85+
$ export TF_VAR_in_description="was created by $USER@$HOSTNAME on $(date)."
86+
```
11787

118-
The above copyright notice and this permission notice shall be
119-
included in all copies or substantial portions of the Software.
88+
## Outputs
12089

121-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
122-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
123-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
124-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
125-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
126-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
127-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
90+
| Output Variable | Type | Comment |
91+
|:------------------------ |:------ |:------- |
92+
**out_database_username** | String | The first database username prefixed with user_rw_ followed by randomized characters.
93+
**out_database_password** | String | Robust terraform created 48 character password that includes the allowed special characters
94+
**out_clone_db_hostname** | String | The addressable hostname of the database that has been cloned from a snapshot
95+
**out_clone_db_endpoint** | String | The database endpoint with protool suffix of the database that has been cloned from a snapshot
96+
**out_fresh_db_hostname** | String | The addressable hostname of the newly created (fresh) database
97+
**out_fresh_db_endpoint** | String | The database endpoint with protool suffix of the newly created (fresh) database

example/postgres.example-main.tf

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ module fresh_db {
5252
in_db_subnet_ids = module.vpc-network.out_private_subnet_ids
5353
in_database_name = local.fresh_db_name
5454

55-
in_ecosystem_name = local.ecosystem_name
56-
in_tag_timestamp = module.resource-tags.out_tag_timestamp
57-
in_tag_description = module.resource-tags.out_tag_description
55+
in_ecosystem = local.ecosystem_name
56+
in_timestamp = module.resource-tags.out_tag_timestamp
57+
in_description = module.resource-tags.out_tag_description
5858
}
5959

6060

@@ -69,9 +69,9 @@ module clone_db {
6969

7070
in_database_name = local.clone_db_name
7171

72-
in_ecosystem_name = local.ecosystem_name
73-
in_tag_timestamp = module.resource-tags.out_tag_timestamp
74-
in_tag_description = module.resource-tags.out_tag_description
72+
in_ecosystem = local.ecosystem_name
73+
in_timestamp = module.resource-tags.out_tag_timestamp
74+
in_description = module.resource-tags.out_tag_description
7575
}
7676

7777

@@ -82,9 +82,9 @@ module vpc-network {
8282
in_num_public_subnets = 3
8383
in_num_private_subnets = 3
8484

85-
in_ecosystem_name = local.ecosystem_name
86-
in_tag_timestamp = module.resource-tags.out_tag_timestamp
87-
in_tag_description = module.resource-tags.out_tag_description
85+
in_ecosystem = local.ecosystem_name
86+
in_timestamp = module.resource-tags.out_tag_timestamp
87+
in_description = module.resource-tags.out_tag_description
8888
}
8989

9090

@@ -94,9 +94,9 @@ module security-group {
9494
in_ingress = [ "postgres" ]
9595
in_vpc_id = module.vpc-network.out_vpc_id
9696

97-
in_ecosystem_name = local.ecosystem_name
98-
in_tag_timestamp = module.resource-tags.out_tag_timestamp
99-
in_tag_description = module.resource-tags.out_tag_description
97+
in_ecosystem = local.ecosystem_name
98+
in_timestamp = module.resource-tags.out_tag_timestamp
99+
in_description = module.resource-tags.out_tag_description
100100
}
101101

102102

rds.postgres-main.tf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ locals {
2525
resource aws_db_instance fresh {
2626

2727
count = var.in_clone_snapshot ? 0 : 1
28-
identifier = "${ var.in_database_name }-fresh-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
28+
identifier = "${ var.in_database_name }-fresh-${ var.in_ecosystem }-${ var.in_timestamp }"
2929

3030
name = var.in_database_name
3131
username = local.db_username
@@ -72,7 +72,7 @@ resource aws_db_instance clone {
7272
count = var.in_clone_snapshot ? 1 : 0
7373

7474
snapshot_identifier = data.aws_db_snapshot.from[0].id
75-
identifier = "${ var.in_database_name }-clone-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
75+
identifier = "${ var.in_database_name }-clone-${ var.in_ecosystem }-${ var.in_timestamp }"
7676

7777
name = var.in_database_name
7878
port = 5432
@@ -119,8 +119,8 @@ data aws_db_snapshot from {
119119
*/
120120
resource aws_db_subnet_group me {
121121

122-
name_prefix = "db-${ var.in_ecosystem_name }"
123-
description = "RDS postgres subnet group for the ${ var.in_ecosystem_name } database."
122+
name_prefix = "db-${ var.in_ecosystem }"
123+
description = "RDS postgres subnet group for the ${ var.in_ecosystem } database."
124124
subnet_ids = var.in_db_subnet_ids
125125
tags = merge( local.subnet_group_tags, var.in_mandated_tags )
126126
}
@@ -169,21 +169,21 @@ resource random_string username_suffix {
169169
locals {
170170

171171
subnet_group_tags = {
172-
Name = "db-subnet-group-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
173-
Desc = "This RDS postgres database subnet group for ${ var.in_ecosystem_name } ${ var.in_tag_description }"
172+
Name = "db-subnet-group-${ var.in_ecosystem }-${ var.in_timestamp }"
173+
Desc = "This RDS postgres database subnet group for ${ var.in_ecosystem } ${ var.in_description }"
174174
}
175175

176176
database_tags = {
177177
Name = var.in_database_name
178-
Id = "${ var.in_database_name }-${ var.in_ecosystem_name }-${ var.in_tag_timestamp }"
178+
Id = "${ var.in_database_name }-${ var.in_ecosystem }-${ var.in_timestamp }"
179179
}
180180

181181
cloned_database_tags = {
182-
Desc = "This PostgreSQL database named ${ var.in_database_name } was cloned from snapshot ${ data.aws_db_snapshot.from[0].id } and ${ var.in_tag_description }"
182+
Desc = "This PostgreSQL database named ${ var.in_database_name } was cloned from snapshot ${ data.aws_db_snapshot.from[0].id } and ${ var.in_description }"
183183
}
184184

185185
fresh_database_tags = {
186-
Desc = "This brand new PostgreSQL database named ${ var.in_database_name } ${ var.in_tag_description }"
186+
Desc = "This brand new PostgreSQL database named ${ var.in_database_name } ${ var.in_description }"
187187
}
188188

189189
}

rds.postgres-outputs.tf

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11

2-
/*
3-
| --
4-
| -- ---------------------------------- --
5-
| -- Created or Cloned Database Outputs --
6-
| -- ---------------------------------- --
7-
| --
8-
| -- These are the postgres database output variables.
9-
| -- The database username and password are not involved here
10-
| -- because they are consumed ( as opposed to produced ).
11-
| --
12-
*/
13-
output out_fresh_db_hostname { value = length( aws_db_instance.fresh ) == 0 ? "n/a" : aws_db_instance.fresh[0].address }
14-
output out_fresh_db_endpoint { value = length( aws_db_instance.fresh ) == 0 ? "n/a" : aws_db_instance.fresh[0].endpoint }
2+
output out_database_username {
3+
value = local.db_username
4+
}
155

16-
output out_clone_db_hostname { value = length( aws_db_instance.clone ) == 0 ? "n/a" : aws_db_instance.clone[0].address }
17-
output out_clone_db_endpoint { value = length( aws_db_instance.clone ) == 0 ? "n/a" : aws_db_instance.clone[0].endpoint }
6+
output out_database_password {
7+
value = random_string.dbpassword.result
8+
}
9+
10+
output out_fresh_db_hostname {
11+
value = length( aws_db_instance.fresh ) == 0 ? "n/a" : aws_db_instance.fresh[0].address
12+
}
13+
14+
output out_fresh_db_endpoint {
15+
value = length( aws_db_instance.fresh ) == 0 ? "n/a" : aws_db_instance.fresh[0].endpoint
16+
}
17+
18+
output out_clone_db_hostname {
19+
value = length( aws_db_instance.clone ) == 0 ? "n/a" : aws_db_instance.clone[0].address
20+
}
21+
22+
output out_clone_db_endpoint {
23+
value = length( aws_db_instance.clone ) == 0 ? "n/a" : aws_db_instance.clone[0].endpoint
24+
}
1825

19-
output out_database_username { value = local.db_username }
20-
output out_database_password { value = random_string.dbpassword.result }

rds.postgres-variables.tf

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ variable in_security_group_id {
33
description = "The ID of the security group protecting the database."
44
}
55

6+
67
variable in_db_subnet_ids {
78
type = "list"
89
description = "The subnet ids in which the multi-availability zone db resides."
910
}
1011

12+
1113
variable in_database_name {
12-
description = "The database name remembering that a different name creates another database."
14+
description = "Alphanumeric only database name starting with a letter and a max length of 32."
1315
}
1416

17+
1518
variable in_clone_snapshot {
1619
description = "The newly Created database will be a clone from a snapshot if set to true."
1720
default = false
@@ -35,30 +38,30 @@ variable in_mandated_tags {
3538

3639

3740
### ################# ###
38-
### in_ecosystem_name ###
41+
### in_ecosystem ###
3942
### ################# ###
4043

41-
variable in_ecosystem_name {
44+
variable in_ecosystem {
4245

4346
description = "Creational stamp binding all infrastructure components created on behalf of this ecosystem instance."
4447
}
4548

4649

4750
### ################ ###
48-
### in_tag_timestamp ###
51+
### in_timestamp ###
4952
### ################ ###
5053

51-
variable in_tag_timestamp {
54+
variable in_timestamp {
5255

5356
description = "A timestamp for resource tags in the format ymmdd-hhmm like 80911-1435"
5457
}
5558

5659

5760
### ################## ###
58-
### in_tag_description ###
61+
### in_description ###
5962
### ################## ###
6063

61-
variable in_tag_description {
64+
variable in_description {
6265

6366
description = "Ubiquitous note detailing who, when, where and why for every infrastructure component."
6467
}

0 commit comments

Comments
 (0)