Skip to content

Commit

Permalink
feat: Add terraform variable validation
Browse files Browse the repository at this point in the history
  • Loading branch information
timckt committed Sep 24, 2024
1 parent 7b368fc commit 96f359c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ variable "db_allocated_storage" {
description = "The allocated storage in gibibytes"
default = "20" # Minimum 'gp3' storage size is 20 GiB for Amazon RDS.
type = number

validation {
condition = (
var.storage_type == "io2" ? (
contains(["sqlserver-ee", "sqlserver-se", "sqlserver-ex", "sqlserver-web"], var.db_engine)
? var.allocated_storage >= 20
: var.allocated_storage >= 100
)
: var.storage_type == "gp3" ? var.allocated_storage >= 20
: true
)
error_message = "Invalid 'allocated_storage':
- When 'storage_type' is 'io2' and 'engine' is SQL Server, it must be at least 20 GiB.
- When 'storage_type' is 'io2' and 'engine' is not SQL Server, it must be at least 100 GiB.
- When 'storage_type' is 'gp3', it must be at least 20 GiB."
}
}

variable "db_max_allocated_storage" {
Expand Down Expand Up @@ -63,15 +79,25 @@ variable "storage_type" {
description = "One of 'standard' (magnetic), 'gp2' (general purpose SSD), 'gp3' (new generation of general purpose SSD), 'io1' (provisioned IOPS SSD), or 'io2' (new generation of provisioned IOPS SSD). If you specify 'io2', you must also include a value for the 'iops' parameter and the `allocated_storage` must be at least 100 GiB (except for SQL Server which the minimum is 20 GiB)."
type = string
default = "gp3"

validation {
condition = contains(["standard", "gp2", "gp3", "io1", "io2"], var.storage_type)
error_message = "Invalid 'storage_type'. Must be one of 'standard', 'gp2', 'gp3', 'io1', or 'io2'."
}
}

# For larger database sizes, you need to adjust the 'iops' value accordingly.
# For larger database sizes, you need to adjust the 'db_iops' value accordingly.
# The valid ranges for IOPS depend on the DB engine and storage size.
# Please refer to https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Storage.html for detailed information:
variable "db_iops" {
description = "The amount of provisioned IOPS."
type = number
default = null # Default to null to omit 'iops' unless explicitly specified, preventing unintended changes

validation {
condition = (var.storage_type == "io2") ? var.db_iops != null : true
error_message = "When 'storage_type' is 'io2', 'db_iops' must be specified."
}
}

variable "db_name" {
Expand Down

0 comments on commit 96f359c

Please sign in to comment.