From 2a1cbcea2e4907a5b2a4792d86a7761545449c30 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 precondition validation --- main.tf | 34 ++++++++++++++++++++++++++++++++++ variables.tf | 7 ++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 817857a..d1af45e 100644 --- a/main.tf +++ b/main.tf @@ -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 = <