Skip to content

Commit 09cb0ea

Browse files
authored
feat: Allow to customizable instance settings (#146)
1 parent a8cb954 commit 09cb0ea

File tree

5 files changed

+148
-6
lines changed

5 files changed

+148
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module "db" {
6262
- [MySQL](examples/mysql): A simple example with VPC and MySQL cluster.
6363
- [Serverless](examples/serverless): Serverless PostgreSQL cluster.
6464
- [Advanced](examples/advanced): A PostgreSQL cluster with enhanced monitoring and autoscaling enabled.
65+
- [Custom Instance Settings](examples/custom_instance_settings): A PostgreSQL cluster with custom instance settings.
6566

6667
## Documentation
6768

@@ -113,6 +114,7 @@ Terraform documentation is generated automatically using [pre-commit hooks](http
113114
| iam\_roles | A List of ARNs for the IAM roles to associate to the RDS Cluster. | `list(string)` | `[]` | no |
114115
| instance\_type | Instance type to use at master instance. If instance\_type\_replica is not set it will use the same type for replica instances | `string` | n/a | yes |
115116
| instance\_type\_replica | Instance type to use at replica instance | `string` | `null` | no |
117+
| instances\_parameters | Customized instance settings. Supported keys: instance\_name, instance\_type, instance\_promotion\_tier, publicly\_accessible | `list(map(string))` | `[]` | no |
116118
| kms\_key\_id | The ARN for the KMS encryption key if one is set to the cluster. | `string` | `""` | no |
117119
| monitoring\_interval | The interval (seconds) between points when Enhanced Monitoring metrics are collected | `number` | `0` | no |
118120
| monitoring\_role\_arn | IAM role for RDS to send enhanced monitoring metrics to CloudWatch | `string` | `""` | no |
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}
4+
5+
######################################
6+
# Data sources to get VPC and subnets
7+
######################################
8+
data "aws_vpc" "default" {
9+
default = true
10+
}
11+
12+
data "aws_subnet_ids" "all" {
13+
vpc_id = data.aws_vpc.default.id
14+
}
15+
16+
#############
17+
# RDS Aurora
18+
#############
19+
module "aurora" {
20+
source = "../../"
21+
name = "aurora-example-postgresql"
22+
engine = "aurora-postgresql"
23+
engine_version = "11.6"
24+
subnets = data.aws_subnet_ids.all.ids
25+
vpc_id = data.aws_vpc.default.id
26+
replica_count = 3
27+
instance_type = "db.r5.large"
28+
apply_immediately = true
29+
skip_final_snapshot = true
30+
db_parameter_group_name = aws_db_parameter_group.aurora_db_postgres11_parameter_group.id
31+
db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.aurora_cluster_postgres11_parameter_group.id
32+
// enabled_cloudwatch_logs_exports = ["audit", "error", "general", "slowquery"]
33+
security_group_description = ""
34+
35+
instances_parameters = [
36+
// List index should be equal to `replica_count`
37+
// Omitted keys replaced by module defaults
38+
{
39+
instance_type = "db.r5.2xlarge"
40+
publicly_accessible = true
41+
},
42+
{
43+
instance_type = "db.r5.2xlarge"
44+
},
45+
{
46+
instance_name = "reporting"
47+
instance_type = "db.r5.large"
48+
instance_promotion_tier = 15
49+
}
50+
]
51+
}
52+
53+
resource "aws_db_parameter_group" "aurora_db_postgres11_parameter_group" {
54+
name = "test-aurora-db-postgres11-parameter-group"
55+
family = "aurora-postgresql11"
56+
description = "test-aurora-db-postgres11-parameter-group"
57+
}
58+
59+
resource "aws_rds_cluster_parameter_group" "aurora_cluster_postgres11_parameter_group" {
60+
name = "test-aurora-postgres11-cluster-parameter-group"
61+
family = "aurora-postgresql11"
62+
description = "test-aurora-postgres11-cluster-parameter-group"
63+
}
64+
65+
############################
66+
# Example of security group
67+
############################
68+
resource "aws_security_group" "app_servers" {
69+
name_prefix = "app-servers-"
70+
description = "For application servers"
71+
vpc_id = data.aws_vpc.default.id
72+
}
73+
74+
resource "aws_security_group_rule" "allow_access" {
75+
type = "ingress"
76+
from_port = module.aurora.this_rds_cluster_port
77+
to_port = module.aurora.this_rds_cluster_port
78+
protocol = "tcp"
79+
source_security_group_id = aws_security_group.app_servers.id
80+
security_group_id = module.aurora.this_security_group_id
81+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// aws_rds_cluster
2+
output "this_rds_cluster_id" {
3+
description = "The ID of the cluster"
4+
value = module.aurora.this_rds_cluster_id
5+
}
6+
7+
output "this_rds_cluster_resource_id" {
8+
description = "The Resource ID of the cluster"
9+
value = module.aurora.this_rds_cluster_resource_id
10+
}
11+
12+
output "this_rds_cluster_endpoint" {
13+
description = "The cluster endpoint"
14+
value = module.aurora.this_rds_cluster_endpoint
15+
}
16+
17+
output "this_rds_cluster_reader_endpoint" {
18+
description = "The cluster reader endpoint"
19+
value = module.aurora.this_rds_cluster_reader_endpoint
20+
}
21+
22+
output "this_rds_cluster_database_name" {
23+
description = "Name for an automatically created database on cluster creation"
24+
value = module.aurora.this_rds_cluster_database_name
25+
}
26+
27+
output "this_rds_cluster_master_password" {
28+
description = "The master password"
29+
value = module.aurora.this_rds_cluster_master_password
30+
sensitive = true
31+
}
32+
33+
output "this_rds_cluster_port" {
34+
description = "The port"
35+
value = module.aurora.this_rds_cluster_port
36+
}
37+
38+
output "this_rds_cluster_master_username" {
39+
description = "The master username"
40+
value = module.aurora.this_rds_cluster_master_username
41+
}
42+
43+
// aws_rds_cluster_instance
44+
output "this_rds_cluster_instance_endpoints" {
45+
description = "A list of all cluster instance endpoints"
46+
value = module.aurora.this_rds_cluster_instance_endpoints
47+
}
48+
49+
// aws_security_group
50+
output "this_security_group_id" {
51+
description = "The security group ID of the cluster"
52+
value = module.aurora.this_security_group_id
53+
}
54+

main.tf

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ resource "aws_rds_cluster" "this" {
6464
enabled_cloudwatch_logs_exports = var.enabled_cloudwatch_logs_exports
6565

6666
dynamic "scaling_configuration" {
67-
for_each = length(keys(var.scaling_configuration)) == 0 ? [] : [
68-
var.scaling_configuration]
67+
for_each = length(keys(var.scaling_configuration)) == 0 ? [] : [var.scaling_configuration]
6968

7069
content {
7170
auto_pause = lookup(scaling_configuration.value, "auto_pause", null)
@@ -82,20 +81,20 @@ resource "aws_rds_cluster" "this" {
8281
resource "aws_rds_cluster_instance" "this" {
8382
count = var.replica_scale_enabled ? var.replica_scale_min : var.replica_count
8483

85-
identifier = "${var.name}-${count.index + 1}"
84+
identifier = length(var.instances_parameters) > count.index ? lookup(var.instances_parameters[count.index], "instance_name", "${var.name}-${count.index + 1}") : "${var.name}-${count.index + 1}"
8685
cluster_identifier = aws_rds_cluster.this.id
8786
engine = var.engine
8887
engine_version = var.engine_version
89-
instance_class = count.index > 0 ? coalesce(var.instance_type_replica, var.instance_type) : var.instance_type
90-
publicly_accessible = var.publicly_accessible
88+
instance_class = length(var.instances_parameters) > count.index ? lookup(var.instances_parameters[count.index], "instance_type", var.instance_type) : count.index > 0 ? coalesce(var.instance_type_replica, var.instance_type) : var.instance_type
89+
publicly_accessible = length(var.instances_parameters) > count.index ? lookup(var.instances_parameters[count.index], "publicly_accessible", var.publicly_accessible) : var.publicly_accessible
9190
db_subnet_group_name = local.db_subnet_group_name
9291
db_parameter_group_name = var.db_parameter_group_name
9392
preferred_maintenance_window = var.preferred_maintenance_window
9493
apply_immediately = var.apply_immediately
9594
monitoring_role_arn = local.rds_enhanced_monitoring_arn
9695
monitoring_interval = var.monitoring_interval
9796
auto_minor_version_upgrade = var.auto_minor_version_upgrade
98-
promotion_tier = count.index + 1
97+
promotion_tier = length(var.instances_parameters) > count.index ? lookup(var.instances_parameters[count.index], "instance_promotion_tier", count.index + 1) : count.index + 1
9998
performance_insights_enabled = var.performance_insights_enabled
10099
performance_insights_kms_key_id = var.performance_insights_kms_key_id
101100
ca_cert_identifier = var.ca_cert_identifier

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,9 @@ variable "ca_cert_identifier" {
344344
type = string
345345
default = "rds-ca-2019"
346346
}
347+
348+
variable "instances_parameters" {
349+
description = "Customized instance settings. Supported keys: instance_name, instance_type, instance_promotion_tier, publicly_accessible"
350+
type = list(map(string))
351+
default = []
352+
}

0 commit comments

Comments
 (0)