Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support reserved instances #233

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ locals {
is_regional_cluster = var.cluster_type == "regional"
is_serverless = var.engine_mode == "serverless"
ignore_admin_credentials = var.replication_source_identifier != "" || var.snapshot_identifier != null
reserved_instance_engine = split("-", var.engine)[1]
use_reserved_instances = var.use_reserved_instances && !local.is_serverless && contains(["mysql", "postgresql"], local.reserved_instance_engine)
}

data "aws_partition" "current" {
Expand Down Expand Up @@ -88,6 +90,28 @@ resource "aws_security_group_rule" "egress_ipv6" {
security_group_id = join("", aws_security_group.default[*].id)
}

data "aws_rds_reserved_instance_offering" "default" {
count = local.use_reserved_instances
db_instance_class = var.db_cluster_instance_class
duration = var.rds_ri_duration
multi_az = local.is_regional_cluster
offering_type = var.rds_ri_offering_type
product_description = local.reserved_instance_engine
}

# Note: I'm not sure what will happen when the db reservation expires, and this is not easy to test.
# It will either be recreated or will require manual intervention to recreate.
resource "aws_rds_reserved_instance" "default" {
count = local.use_reserved_instances

offering_id = data.aws_rds_reserved_instance_offering.default[0].id
instance_count = local.cluster_instance_count
lifecycle {
# Once created, we want to avoid any case of accidentally re-creating.
prevent_destroy = true
}
}

# The name "primary" is poorly chosen. We actually mean standalone or regional.
# The primary cluster of a global database is actually created with the "secondary" cluster resource below.
resource "aws_rds_cluster" "primary" {
Expand Down
25 changes: 24 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,27 @@ variable "network_type" {
type = string
default = "IPV4"
description = "The network type of the cluster. Valid values: IPV4, DUAL."
}
}

variable "use_reserved_instances" {
description = <<-EOT
WARNING: Observe your plans and applies carefully when using this feature.
It has potential to be very expensive if not used correctly.

Whether to use reserved instances.
EOT
type = bool
default = false
}

variable "rds_ri_offering_type" {
description = "Offering type of reserved DB instances. Valid values are 'No Upfront', 'Partial Upfront', 'All Upfront'."
type = string
default = ""
}

variable "rds_ri_duration" {
description = "The number of years to reserve the instance. Values can be 1 or 3 (or in seconds, 31536000 or 94608000)"
type = number
default = 1
}
Loading