From 96f359cf04e216cda5324cc8431b91b0589f6398 Mon Sep 17 00:00:00 2001 From: Tim Cheung <152907271+timckt@users.noreply.github.com> Date: Tue, 24 Sep 2024 13:11:53 +0100 Subject: [PATCH] feat: Add terraform variable validation --- variables.tf | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/variables.tf b/variables.tf index 885d5bb..290be76 100644 --- a/variables.tf +++ b/variables.tf @@ -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" { @@ -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" {