Skip to content

Commit a735414

Browse files
authored
fix: update option group to fix name vs. name_prefix and conditional creation for postgresql (#302)
1 parent e7ff6bd commit a735414

File tree

11 files changed

+353
-25
lines changed

11 files changed

+353
-25
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,39 @@ module "db" {
114114
}
115115
```
116116

117+
## Option Groups
118+
119+
[Reference](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithOptionGroups.html)
120+
121+
Users have the ability to:
122+
123+
- Create an option group with the name provided:
124+
125+
```hcl
126+
option_group_name = "prod-instance-mysql-8.0"
127+
option_group_use_name_prefix = false
128+
```
129+
130+
- Create an option group using a unique prefix beginning with the name provided:
131+
132+
```hcl
133+
option_group_name = "prod-instance-mysql-8.0"
134+
```
135+
136+
- Pass the name of an option group to use that has been created outside of the module:
137+
138+
```hcl
139+
create_option_group = false
140+
option_group_name = "prod-instance-mysql-8.0" # must already exist in AWS
141+
```
142+
143+
- Skip creating an option group for PostgreSQL entirely as that is not supported
144+
145+
```hcl
146+
engine = "postgres"
147+
option_group_name = "prod-instance-postgresql-11.0" # this will be ignored, no option group created
148+
```
149+
117150
## Examples
118151

119152
- [Complete RDS example for MSSQL](https://github.com/terraform-aws-modules/terraform-aws-rds/tree/master/examples/complete-mssql)
@@ -198,8 +231,9 @@ No resources.
198231
| multi\_az | Specifies if the RDS instance is multi-AZ | `bool` | `false` | no |
199232
| name | The DB name to create. If omitted, no database is created initially | `string` | `""` | no |
200233
| option\_group\_description | The description of the option group | `string` | `""` | no |
201-
| option\_group\_name | Name of the DB option group to associate | `string` | `""` | no |
234+
| option\_group\_name | Name of the option group | `string` | `""` | no |
202235
| option\_group\_timeouts | Define maximum timeout for deletion of `aws_db_option_group` resource | `map(string)` | <pre>{<br> "delete": "15m"<br>}</pre> | no |
236+
| option\_group\_use\_name\_prefix | Determines whether to use `option_group_name` as is or create a unique name beginning with the `option_group_name` as the prefix | `bool` | `true` | no |
203237
| options | A list of Options to apply. | `any` | `[]` | no |
204238
| parameter\_group\_description | Description of the DB parameter group to create | `string` | `""` | no |
205239
| parameter\_group\_name | Name of the DB parameter group to associate or create | `string` | `""` | no |

examples/option-groups/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Example usage of option groups
2+
3+
Configuration in this directory creates various option groups depending on the configuration specified - no other resources are created.
4+
5+
## Usage
6+
7+
To run this example you need to execute:
8+
9+
```bash
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Requirements
19+
20+
| Name | Version |
21+
|------|---------|
22+
| terraform | >= 0.12.26 |
23+
| aws | >= 2.49 |
24+
25+
## Providers
26+
27+
No provider.
28+
29+
## Modules
30+
31+
| Name | Source | Version |
32+
|------|--------|---------|
33+
| byo_mysql | ../../ | |
34+
| default_mysql | ../../ | |
35+
| default_mysql_name | ../../ | |
36+
| default_postgres | ../../ | |
37+
38+
## Resources
39+
40+
No resources.
41+
42+
## Inputs
43+
44+
No input.
45+
46+
## Outputs
47+
48+
| Name | Description |
49+
|------|-------------|
50+
| byo\_mysql\_option\_group\_arn | The ARN of the BYO MySQL option group (should be blank) |
51+
| byo\_mysql\_option\_group\_id | The ID of the BYO MySQL option group (should be blank) |
52+
| default\_mysql\_name\_option\_group\_arn | The ARN of the default MySQL option group using `name` |
53+
| default\_mysql\_name\_option\_group\_id | The ID of the default MySQL option group using `name` |
54+
| default\_mysql\_option\_group\_arn | The ARN of the default MySQL option group |
55+
| default\_mysql\_option\_group\_id | The ID of the default MySQL option group |
56+
| default\_postgres\_option\_group\_arn | The ARN of the default PostgreSQL option group (should be blank) |
57+
| default\_postgres\_option\_group\_id | The ID of the default PostgreSQL option group (should be blank) |
58+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/option-groups/main.tf

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
provider "aws" {
2+
region = local.region
3+
}
4+
5+
locals {
6+
name = "option-groups"
7+
region = "eu-west-1"
8+
tags = {
9+
Owner = "user"
10+
Environment = "dev"
11+
}
12+
}
13+
14+
################################################################################
15+
# Default Postgres option group
16+
# This should NOT create anything since option groups are not supported for PostgreSQL
17+
# https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithOptionGroups.html
18+
################################################################################
19+
20+
module "default_postgres" {
21+
source = "../../"
22+
23+
identifier = local.name
24+
25+
create_db_instance = false
26+
parameter_group_name = "foo"
27+
create_db_parameter_group = false
28+
db_subnet_group_name = "foo"
29+
create_db_subnet_group = false
30+
31+
# All available versions: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts
32+
engine = "postgres"
33+
engine_version = "11.10"
34+
family = "postgres11" # DB parameter group
35+
major_engine_version = "11" # DB option group
36+
instance_class = "db.t3.large"
37+
38+
allocated_storage = 20
39+
40+
username = "option_groups_postgresql"
41+
password = "YourPwdShouldBeLongAndSecure!"
42+
port = 5432
43+
44+
maintenance_window = "Mon:00:00-Mon:03:00"
45+
backup_window = "03:00-06:00"
46+
47+
tags = local.tags
48+
}
49+
50+
################################################################################
51+
# Default MySQL Option Group w/ name prefix used
52+
# This should create a MySQL option group
53+
# https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithOptionGroups.html
54+
################################################################################
55+
56+
module "default_mysql" {
57+
source = "../../"
58+
59+
identifier = local.name
60+
61+
option_group_name = "${local.name}-default-mysql"
62+
63+
create_db_instance = false
64+
parameter_group_name = "foo"
65+
create_db_parameter_group = false
66+
db_subnet_group_name = "foo"
67+
create_db_subnet_group = false
68+
69+
# All available versions: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts
70+
engine = "mysql"
71+
engine_version = "8.0.20"
72+
family = "mysql8.0" # DB parameter group
73+
major_engine_version = "8.0" # DB option group
74+
instance_class = "db.t3.large"
75+
76+
allocated_storage = 20
77+
78+
username = "option_groups_mysql"
79+
password = "YourPwdShouldBeLongAndSecure!"
80+
port = 3306
81+
82+
maintenance_window = "Mon:00:00-Mon:03:00"
83+
backup_window = "03:00-06:00"
84+
85+
tags = local.tags
86+
}
87+
88+
################################################################################
89+
# Default MySQL option group w/o name prefix
90+
# This should create a MySQL option group
91+
# https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithOptionGroups.html
92+
################################################################################
93+
94+
module "default_mysql_name" {
95+
source = "../../"
96+
97+
identifier = local.name
98+
99+
option_group_name = "${local.name}-default-mysql"
100+
option_group_use_name_prefix = false
101+
102+
create_db_instance = false
103+
parameter_group_name = "foo"
104+
create_db_parameter_group = false
105+
db_subnet_group_name = "foo"
106+
create_db_subnet_group = false
107+
108+
# All available versions: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts
109+
engine = "mysql"
110+
engine_version = "8.0.20"
111+
family = "mysql8.0" # DB parameter group
112+
major_engine_version = "8.0" # DB option group
113+
instance_class = "db.t3.large"
114+
115+
allocated_storage = 20
116+
117+
username = "option_groups_mysql"
118+
password = "YourPwdShouldBeLongAndSecure!"
119+
port = 3306
120+
121+
maintenance_window = "Mon:00:00-Mon:03:00"
122+
backup_window = "03:00-06:00"
123+
124+
tags = local.tags
125+
}
126+
127+
################################################################################
128+
# BYO MySQL Option Group
129+
# This should NOT create a MySQL option group - the user is specifying an option group to use
130+
# https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithOptionGroups.html
131+
################################################################################
132+
133+
module "byo_mysql" {
134+
source = "../../"
135+
136+
identifier = local.name
137+
138+
create_db_option_group = false
139+
option_group_name = "bringMyOwnOptionGroupName"
140+
141+
create_db_instance = false
142+
parameter_group_name = "foo"
143+
create_db_parameter_group = false
144+
db_subnet_group_name = "foo"
145+
create_db_subnet_group = false
146+
147+
# All available versions: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts
148+
engine = "mysql"
149+
engine_version = "8.0.20"
150+
family = "mysql8.0" # DB parameter group
151+
major_engine_version = "8.0" # DB option group
152+
instance_class = "db.t3.large"
153+
154+
allocated_storage = 20
155+
156+
username = "option_groups_mysql"
157+
password = "YourPwdShouldBeLongAndSecure!"
158+
port = 3306
159+
160+
maintenance_window = "Mon:00:00-Mon:03:00"
161+
backup_window = "03:00-06:00"
162+
163+
tags = local.tags
164+
}

examples/option-groups/outputs.tf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Default PostgreSQL
2+
output "default_postgres_option_group_id" {
3+
description = "The ID of the default PostgreSQL option group (should be blank)"
4+
value = module.default_postgres.this_db_option_group_id
5+
}
6+
7+
output "default_postgres_option_group_arn" {
8+
description = "The ARN of the default PostgreSQL option group (should be blank)"
9+
value = module.default_postgres.this_db_option_group_arn
10+
}
11+
12+
# Default MySQL
13+
output "default_mysql_option_group_id" {
14+
description = "The ID of the default MySQL option group"
15+
value = module.default_mysql.this_db_option_group_id
16+
}
17+
18+
output "default_mysql_option_group_arn" {
19+
description = "The ARN of the default MySQL option group"
20+
value = module.default_mysql.this_db_option_group_arn
21+
}
22+
23+
# Default MySQL name
24+
output "default_mysql_name_option_group_id" {
25+
description = "The ID of the default MySQL option group using `name`"
26+
value = module.default_mysql_name.this_db_option_group_id
27+
}
28+
29+
output "default_mysql_name_option_group_arn" {
30+
description = "The ARN of the default MySQL option group using `name`"
31+
value = module.default_mysql_name.this_db_option_group_arn
32+
}
33+
34+
# BYO MySQL
35+
output "byo_mysql_option_group_id" {
36+
description = "The ID of the BYO MySQL option group (should be blank)"
37+
value = module.byo_mysql.this_db_option_group_id
38+
}
39+
40+
output "byo_mysql_option_group_arn" {
41+
description = "The ARN of the BYO MySQL option group (should be blank)"
42+
value = module.byo_mysql.this_db_option_group_arn
43+
}
44+

examples/option-groups/variables.tf

Whitespace-only changes.

examples/option-groups/versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 0.12.26"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 2.49"
8+
}
9+
}
10+
}

main.tf

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
locals {
2-
db_subnet_group_name = var.db_subnet_group_name != "" ? var.db_subnet_group_name : module.db_subnet_group.this_db_subnet_group_id
32
enable_create_db_subnet_group = var.db_subnet_group_name == "" ? var.create_db_subnet_group : false
3+
db_subnet_group_name = coalesce(var.db_subnet_group_name, module.db_subnet_group.this_db_subnet_group_id)
44

5-
parameter_group_name_id = var.parameter_group_name != "" ? var.parameter_group_name : module.db_parameter_group.this_db_parameter_group_id
5+
parameter_group_name_id = coalesce(var.parameter_group_name, module.db_parameter_group.this_db_parameter_group_id)
66

7-
option_group_name = var.option_group_name != "" ? var.option_group_name : module.db_option_group.this_db_option_group_id
8-
enable_create_db_option_group = var.create_db_option_group ? true : var.option_group_name == "" && var.engine != "postgres"
7+
enable_create_db_option_group = var.create_db_option_group && var.engine != "postgres"
8+
option_group = var.engine != "postgres" ? coalesce(module.db_option_group.this_db_option_group_id, var.option_group_name) : null
99
}
1010

1111
module "db_subnet_group" {
@@ -35,12 +35,15 @@ module "db_parameter_group" {
3535
tags = var.tags
3636
}
3737

38+
# "${var.identifier}-${var.engine}-${var.major_engine_version}"
39+
3840
module "db_option_group" {
3941
source = "./modules/db_option_group"
4042

41-
create = local.enable_create_db_option_group
42-
identifier = var.identifier
43-
name_prefix = "${var.identifier}-"
43+
create = local.enable_create_db_option_group
44+
45+
name = var.option_group_name
46+
use_name_prefix = var.option_group_use_name_prefix
4447
option_group_description = var.option_group_description
4548
engine_name = var.engine
4649
major_engine_version = var.major_engine_version
@@ -81,7 +84,7 @@ module "db_instance" {
8184
vpc_security_group_ids = var.vpc_security_group_ids
8285
db_subnet_group_name = local.db_subnet_group_name
8386
parameter_group_name = local.parameter_group_name_id
84-
option_group_name = local.option_group_name
87+
option_group_name = local.option_group
8588

8689
availability_zone = var.availability_zone
8790
multi_az = var.multi_az

modules/db_option_group/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ No Modules.
3030
|------|-------------|------|---------|:--------:|
3131
| create | Whether to create this resource or not? | `bool` | `true` | no |
3232
| engine\_name | Specifies the name of the engine that this option group should be associated with | `string` | n/a | yes |
33-
| identifier | The identifier of the resource | `string` | n/a | yes |
3433
| major\_engine\_version | Specifies the major version of the engine that this option group should be associated with | `string` | n/a | yes |
35-
| name\_prefix | Creates a unique name beginning with the specified prefix | `string` | n/a | yes |
34+
| name | The name of the option group | `string` | n/a | yes |
3635
| option\_group\_description | The description of the option group | `string` | `""` | no |
3736
| options | A list of Options to apply | `any` | `[]` | no |
3837
| tags | A mapping of tags to assign to the resource | `map(string)` | `{}` | no |
3938
| timeouts | Define maximum timeout for deletion of `aws_db_option_group` resource | `map(string)` | <pre>{<br> "delete": "15m"<br>}</pre> | no |
39+
| use\_name\_prefix | Determines whether to use `name` as is or create a unique name beginning with `name` as the specified prefix | `bool` | `true` | no |
4040

4141
## Outputs
4242

0 commit comments

Comments
 (0)