Skip to content

Commit

Permalink
Add parameter_group_name_prefix_enabled variable. Update examples a…
Browse files Browse the repository at this point in the history
…nd tests (#179)

* Add `parameter_group_name_prefix_enabled` variable

* Add `parameter_group_name_prefix_enabled` variable

* Add `parameter_group_name_prefix_enabled` variable

* Add `parameter_group_name_prefix_enabled` variable

* Add `parameter_group_name_prefix_enabled` variable

* Add `parameter_group_name_prefix_enabled` variable

* Add `parameter_group_name_prefix_enabled` variable

* Add `parameter_group_name_prefix_enabled` variable
  • Loading branch information
aknysh authored Oct 6, 2023
1 parent 36fc284 commit 1f487bb
Show file tree
Hide file tree
Showing 20 changed files with 679 additions and 262 deletions.
7 changes: 4 additions & 3 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"extends": [
"config:base",
":preserveSemverRanges"
":preserveSemverRanges",
":rebaseStalePrs"
],
"baseBranches": ["main", "master", "/^release\\/v\\d{1,2}$/"],
"baseBranches": ["main"],
"labels": ["auto-update"],
"dependencyDashboardAutoclose": true,
"enabledManagers": ["terraform"],
"terraform": {
"ignorePaths": ["**/context.tf", "examples/**"]
"ignorePaths": ["**/context.tf"]
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2017-2022 Cloud Posse, LLC
Copyright 2017-2023 Cloud Posse, LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ We highly recommend that in your code you pin the version to the exact version y
using so that your infrastructure remains stable, and update versions in a
systematic way so that they do not catch you by surprise.

Also, because of a bug in the Terraform registry ([hashicorp/terraform#21417](https://github.com/hashicorp/terraform/issues/21417)),
the registry shows many of our inputs as required when in fact they are optional.
The table below correctly indicates which inputs are required.



For a complete example, see [examples/complete](examples/complete).
Expand Down Expand Up @@ -465,6 +461,7 @@ Available targets:
| <a name="input_maintenance_window"></a> [maintenance\_window](#input\_maintenance\_window) | Weekly time range during which system maintenance can occur, in UTC | `string` | `"wed:03:00-wed:04:00"` | no |
| <a name="input_name"></a> [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.<br>This is the only ID element not also included as a `tag`.<br>The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no |
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
| <a name="input_parameter_group_name_prefix_enabled"></a> [parameter\_group\_name\_prefix\_enabled](#input\_parameter\_group\_name\_prefix\_enabled) | Set to `true` to use `name_prefix` to name the cluster and database parameter groups. Set to `false` to use `name` instead | `bool` | `true` | no |
| <a name="input_performance_insights_enabled"></a> [performance\_insights\_enabled](#input\_performance\_insights\_enabled) | Whether to enable Performance Insights | `bool` | `false` | no |
| <a name="input_performance_insights_kms_key_id"></a> [performance\_insights\_kms\_key\_id](#input\_performance\_insights\_kms\_key\_id) | The ARN for the KMS key to encrypt Performance Insights data. When specifying `performance_insights_kms_key_id`, `performance_insights_enabled` needs to be set to true | `string` | `""` | no |
| <a name="input_performance_insights_retention_period"></a> [performance\_insights\_retention\_period](#input\_performance\_insights\_retention\_period) | Amount of time in days to retain Performance Insights data. Either 7 (7 days) or 731 (2 years) | `number` | `null` | no |
Expand Down
1 change: 1 addition & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
| <a name="input_maintenance_window"></a> [maintenance\_window](#input\_maintenance\_window) | Weekly time range during which system maintenance can occur, in UTC | `string` | `"wed:03:00-wed:04:00"` | no |
| <a name="input_name"></a> [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.<br>This is the only ID element not also included as a `tag`.<br>The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no |
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
| <a name="input_parameter_group_name_prefix_enabled"></a> [parameter\_group\_name\_prefix\_enabled](#input\_parameter\_group\_name\_prefix\_enabled) | Set to `true` to use `name_prefix` to name the cluster and database parameter groups. Set to `false` to use `name` instead | `bool` | `true` | no |
| <a name="input_performance_insights_enabled"></a> [performance\_insights\_enabled](#input\_performance\_insights\_enabled) | Whether to enable Performance Insights | `bool` | `false` | no |
| <a name="input_performance_insights_kms_key_id"></a> [performance\_insights\_kms\_key\_id](#input\_performance\_insights\_kms\_key\_id) | The ARN for the KMS key to encrypt Performance Insights data. When specifying `performance_insights_kms_key_id`, `performance_insights_enabled` needs to be set to true | `string` | `""` | no |
| <a name="input_performance_insights_retention_period"></a> [performance\_insights\_retention\_period](#input\_performance\_insights\_retention\_period) | Amount of time in days to retain Performance Insights data. Either 7 (7 days) or 731 (2 years) | `number` | `null` | no |
Expand Down
13 changes: 7 additions & 6 deletions examples/complete/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ stage = "test"

name = "rds-cluster"

instance_type = "db.t3.small"
# https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.DBInstanceClass.html#Concepts.DBInstanceClass.SupportAurora
instance_type = "db.t3.medium"

cluster_family = "aurora-mysql5.7"
cluster_family = "aurora-mysql8.0"

engine = "aurora-mysql"

engine_mode = "provisioned"

cluster_size = 1

deletion_protection = false

autoscaling_enabled = false

engine = "aurora-mysql"

engine_mode = "provisioned"

db_name = "test_db"

admin_user = "admin"
Expand Down
16 changes: 10 additions & 6 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ provider "aws" {
}

module "vpc" {
source = "cloudposse/vpc/aws"
version = "2.1.0"
source = "cloudposse/vpc/aws"
version = "2.1.0"

ipv4_primary_cidr_block = "172.16.0.0/16"
context = module.this.context

context = module.this.context
}

module "subnets" {
source = "cloudposse/dynamic-subnets/aws"
version = "2.4.1"
source = "cloudposse/dynamic-subnets/aws"
version = "2.4.1"

availability_zones = var.availability_zones
vpc_id = module.vpc.vpc_id
igw_id = [module.vpc.igw_id]
ipv4_cidr_block = [module.vpc.vpc_cidr_block]
nat_gateway_enabled = false
nat_instance_enabled = false
context = module.this.context

context = module.this.context
}

module "rds_cluster" {
Expand Down
3 changes: 2 additions & 1 deletion examples/complete/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ variable "region" {
}

variable "availability_zones" {
type = list(string)
type = list(string)
description = "Availability zones"
}

variable "instance_type" {
Expand Down
26 changes: 10 additions & 16 deletions examples/postgres/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ stage = "test"

name = "rds-cluster"

instance_type = "db.m5d.large"
# https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.DBInstanceClass.html#Concepts.DBInstanceClass.SupportAurora
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster_instance
instance_type = "db.t3.large"

cluster_family = "postgres14"
cluster_family = "aurora-postgresql15"

cluster_size = 1
engine_version = "15.3"

deletion_protection = false
engine = "aurora-postgresql"

autoscaling_enabled = false
engine_mode = "provisioned"

engine = "postgres"
cluster_size = 1

engine_mode = "provisioned"
deletion_protection = false

engine_version = "14.7"
autoscaling_enabled = false

db_name = "test_db"

Expand All @@ -33,11 +35,3 @@ admin_password = "admin_password"
enhanced_monitoring_role_enabled = true

rds_monitoring_interval = 30

allocated_storage = 100

storage_type = "io1"

iops = 1000

db_cluster_instance_class = "db.m5d.large"
6 changes: 3 additions & 3 deletions examples/postgres/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ variable "allocated_storage" {

variable "db_cluster_instance_class" {
type = string
default = "db.m5d.large"
description = "This setting is required to create a Multi-AZ DB cluste"
}
default = null
description = "This setting is required to create a Multi-AZ RDS cluster (not Aurora)"
}
11 changes: 8 additions & 3 deletions examples/serverlessv2_postgres/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
enabled = true

region = "us-east-2"

availability_zones = ["us-east-2a", "us-east-2b"]
Expand All @@ -10,13 +8,20 @@ stage = "test"

name = "rds-cluster"

engine = "aurora-postgresql"

cluster_family = "aurora-postgresql13"

engine_version = "13.6"

instance_type = "db.serverless"

cluster_size = 1

deletion_protection = false

autoscaling_enabled = false


db_name = "test_db"

admin_user = "postgres"
Expand Down
9 changes: 4 additions & 5 deletions examples/serverlessv2_postgres/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ module "subnets" {
module "rds_cluster_aurora_serverlessv2_postgres_13" {
source = "../../"

engine = "aurora-postgresql"
cluster_family = "aurora-postgresql13"
engine_version = "13.6"
instance_type = var.instance_type
engine = var.engine
cluster_family = var.cluster_family
engine_version = var.engine_version
cluster_size = var.cluster_size
admin_user = var.admin_user
admin_password = var.admin_password
db_name = var.db_name
instance_type = "db.serverless"
vpc_id = module.vpc.vpc_id
subnets = module.subnets.private_subnet_ids
security_groups = [module.vpc.vpc_default_security_group_id]
Expand All @@ -45,6 +45,5 @@ module "rds_cluster_aurora_serverlessv2_postgres_13" {
min_capacity = var.min_capacity
}


context = module.this.context
}
24 changes: 23 additions & 1 deletion examples/serverlessv2_postgres/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@ variable "region" {
}

variable "availability_zones" {
type = list(string)
type = list(string)
description = "Availability zones"
}

variable "cluster_family" {
type = string
description = "The family of the DB cluster parameter group"
}

variable "engine" {
type = string
description = "The name of the database engine to be used for this DB cluster. Valid values: `aurora`, `aurora-mysql`, `aurora-postgresql`"
}

variable "engine_version" {
type = string
default = ""
description = "The version of the database engine to use. See `aws rds describe-db-engine-versions` "
}

variable "instance_type" {
type = string
description = "Instance type to use"
}

variable "cluster_size" {
Expand Down
14 changes: 10 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ resource "aws_db_subnet_group" "default" {
}

resource "aws_rds_cluster_parameter_group" "default" {
count = local.enabled ? 1 : 0
name_prefix = "${module.this.id}${module.this.delimiter}"
count = local.enabled ? 1 : 0

name_prefix = var.parameter_group_name_prefix_enabled ? "${module.this.id}${module.this.delimiter}" : null
name = !var.parameter_group_name_prefix_enabled ? module.this.id : null

description = "DB cluster parameter group"
family = var.cluster_family

Expand All @@ -314,8 +317,11 @@ resource "aws_rds_cluster_parameter_group" "default" {
}

resource "aws_db_parameter_group" "default" {
count = local.enabled ? 1 : 0
name_prefix = "${module.this.id}${module.this.delimiter}"
count = local.enabled ? 1 : 0

name_prefix = var.parameter_group_name_prefix_enabled ? "${module.this.id}${module.this.delimiter}" : null
name = !var.parameter_group_name_prefix_enabled ? module.this.id : null

description = "DB instance parameter group"
family = var.cluster_family

Expand Down
16 changes: 7 additions & 9 deletions test/src/examples_complete_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

import (
"os"
"regexp"
"strings"
"testing"

Expand All @@ -11,11 +11,6 @@ import (
"github.com/stretchr/testify/assert"
)

func cleanup(t *testing.T, terraformOptions *terraform.Options, tempTestFolder string) {
terraform.Destroy(t, terraformOptions)
os.RemoveAll(tempTestFolder)
}

// Test the Terraform module in examples/complete using Terratest.
func TestExamplesComplete(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -91,7 +86,7 @@ func TestExamplesCompleteDisabled(t *testing.T) {
VarFiles: varFiles,
Vars: map[string]interface{}{
"attributes": attributes,
"enabled": "false",
"enabled": false,
},
}

Expand All @@ -101,6 +96,9 @@ func TestExamplesCompleteDisabled(t *testing.T) {
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
results := terraform.InitAndApply(t, terraformOptions)

// Should complete successfully without creating or changing any resources
assert.Contains(t, results, "Resources: 0 added, 0 changed, 0 destroyed.")
// Should complete successfully without creating or changing any resources.
// Extract the "Resources:" section of the output to make the error message more readable.
re := regexp.MustCompile(`Resources: [^.]+\.`)
match := re.FindString(results)
assert.Equal(t, "Resources: 0 added, 0 changed, 0 destroyed.", match, "Re-applying the same configuration should not change any resources")
}
12 changes: 8 additions & 4 deletions test/src/examples_postgres_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"regexp"
"strings"
"testing"

Expand All @@ -10,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)

// Test the Terraform module in examples/complete using Terratest.
// Test the Terraform module in examples/postgres using Terratest.
func TestExamplesPostgres(t *testing.T) {
t.Parallel()
randID := strings.ToLower(random.UniqueId())
Expand Down Expand Up @@ -85,7 +86,7 @@ func TestExamplesPostgresDisabled(t *testing.T) {
VarFiles: varFiles,
Vars: map[string]interface{}{
"attributes": attributes,
"enabled": "false",
"enabled": false,
},
}

Expand All @@ -95,6 +96,9 @@ func TestExamplesPostgresDisabled(t *testing.T) {
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
results := terraform.InitAndApply(t, terraformOptions)

// Should complete successfully without creating or changing any resources
assert.Contains(t, results, "Resources: 0 added, 0 changed, 0 destroyed.")
// Should complete successfully without creating or changing any resources.
// Extract the "Resources:" section of the output to make the error message more readable.
re := regexp.MustCompile(`Resources: [^.]+\.`)
match := re.FindString(results)
assert.Equal(t, "Resources: 0 added, 0 changed, 0 destroyed.", match, "Re-applying the same configuration should not change any resources")
}
Loading

0 comments on commit 1f487bb

Please sign in to comment.