Skip to content

Commit

Permalink
feat: Add terraform precondition validation
Browse files Browse the repository at this point in the history
  • Loading branch information
timckt committed Sep 26, 2024
1 parent 7b368fc commit 2a1cbce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
34 changes: 34 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,40 @@ resource "aws_db_instance" "rds" {
}

tags = merge(local.default_tags, local.tag_for_auto_shutdown)

lifecycle {
precondition {
condition = var.storage_type != "io2" || (
contains(["sqlserver-ee", "sqlserver-se", "sqlserver-ex", "sqlserver-web"], var.db_engine) ? var.db_allocated_storage >= 20 : var.db_allocated_storage >= 100
)
error_message = "When 'storage_type' is 'io2', 'db_allocated_storage' must be at least 100 GiB unless using SQL which must be at least 20 GiB."
}

precondition {
condition = var.storage_type != "io2" || (var.db_iops != null ? var.db_iops >= 1000 : false)
error_message = "When 'storage_type' is 'io2', 'db_iops' must be specified and at least 1000."
}

precondition {
condition = var.storage_type != "gp3" || var.db_allocated_storage >= 20
error_message = "When 'storage_type' is 'gp3', 'db_allocated_storage' must be at least 20 GiB."
}

precondition {
condition = var.storage_type != "gp3" || contains(["sqlserver-ee", "sqlserver-se", "sqlserver-ex", "sqlserver-web"], var.db_engine) || (
contains(["oracle-ee", "oracle-se", "oracle-se1", "oracle-se2"], var.db_engine) ? (
var.db_allocated_storage < 200 || (var.db_iops != null ? var.db_iops >= 12000 : false)
) : (
var.db_allocated_storage < 400 || (var.db_iops != null ? var.db_iops >= 12000 : false)
)
)
error_message = <<EOF
When 'storage_type' is 'gp3':
- For Oracle engines, if 'db_allocated_storage' is at least 200 GiB, 'db_iops' must be specified and at least 12,000.
- For other engines (excluding SQL Server), if 'db_allocated_storage' is at least 400 GiB, 'db_iops' must be specified and at least 12,000.
EOF
}
}
}

##########################
Expand Down
7 changes: 6 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ 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" {
Expand Down

0 comments on commit 2a1cbce

Please sign in to comment.