Make destroy protection environment aware

This commit is contained in:
2026-11-14 14:05:19 +09:00
parent 94a5c2bf55
commit a1453034c3
8 changed files with 84 additions and 28 deletions
+23 -16
View File
@@ -1,23 +1,12 @@
resource "aws_kms_key" "vault" {
resource "aws_kms_key" "vault_protected" {
count = var.protect_from_destroy ? 1 : 0
description = "KMS for Vault"
key_usage = "ENCRYPT_DECRYPT"
customer_master_key_spec = "SYMMETRIC_DEFAULT"
enable_key_rotation = false
multi_region = false
policy = jsonencode({
Version = "2012-10-17"
Id = "key-consolepolicy-3"
Statement = [{
Sid = "Enable IAM User Permissions"
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::${var.account_id}:root"
}
Action = "kms:*"
Resource = "*"
}]
})
policy = local.kms_policy
lifecycle {
prevent_destroy = true
@@ -28,7 +17,25 @@ resource "aws_kms_key" "vault" {
}
}
resource "aws_kms_key" "vault_unprotected" {
count = var.protect_from_destroy ? 0 : 1
description = "KMS for Vault"
key_usage = "ENCRYPT_DECRYPT"
customer_master_key_spec = "SYMMETRIC_DEFAULT"
enable_key_rotation = false
multi_region = false
policy = local.kms_policy
lifecycle {
ignore_changes = [
bypass_policy_lockout_safety_check,
deletion_window_in_days,
]
}
}
resource "aws_kms_alias" "vault" {
name = var.kms_alias_name
target_key_id = aws_kms_key.vault.key_id
target_key_id = local.vault_kms_key_id
}