Skip to content

Commit 86ff7b0

Browse files
committed
refactor: Enhance vault lock configuration validation
- Updated the condition for enabling the vault lock configuration to include a check for the 'enabled' variable. - Moved the validation logic for min_retention_days and max_retention_days into the variable definition for 'locked' to ensure proper validation when vault locking is enabled.
1 parent f657214 commit 86ff7b0

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

main.tf

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@ resource "aws_backup_vault" "ab_vault" {
1010

1111
# AWS Backup vault lock configuration
1212
resource "aws_backup_vault_lock_configuration" "ab_vault_lock_configuration" {
13-
count = var.locked && var.vault_name != null ? 1 : 0
13+
count = var.enabled && var.vault_name != null && var.locked ? 1 : 0
1414

1515
backup_vault_name = aws_backup_vault.ab_vault[0].name
16-
changeable_for_days = var.changeable_for_days
17-
max_retention_days = var.max_retention_days
1816
min_retention_days = var.min_retention_days
19-
20-
lifecycle {
21-
precondition {
22-
condition = var.min_retention_days != null && var.max_retention_days != null && var.min_retention_days <= var.max_retention_days
23-
error_message = "For vault lock configuration, min_retention_days and max_retention_days must be provided and min_retention_days must be less than or equal to max_retention_days."
24-
}
25-
}
17+
max_retention_days = var.max_retention_days
18+
changeable_for_days = var.changeable_for_days
2619
}
2720

2821
# AWS Backup plan

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ variable "locked" {
4242
description = "Change to true to add a lock configuration for the backup vault"
4343
type = bool
4444
default = false
45+
46+
validation {
47+
condition = !var.locked || (var.min_retention_days != null && var.max_retention_days != null && var.min_retention_days <= var.max_retention_days)
48+
error_message = "When vault locking is enabled (locked = true), min_retention_days and max_retention_days must be provided and min_retention_days must be less than or equal to max_retention_days."
49+
}
4550
}
4651

4752
variable "changeable_for_days" {

0 commit comments

Comments
 (0)