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
+2 -2
View File
@@ -1,6 +1,6 @@
import {
for_each = local.bucket_names
to = module.audit_logging_encryption.aws_s3_bucket.this[each.key]
to = module.audit_logging_encryption.aws_s3_bucket.protected[each.key]
id = each.value
}
@@ -41,7 +41,7 @@ import {
}
import {
to = module.audit_logging_encryption.aws_kms_key.vault
to = module.audit_logging_encryption.aws_kms_key.vault_protected[0]
id = "46975f6d-881a-4188-8fdf-b641e1258a85"
}
+2 -1
View File
@@ -24,7 +24,8 @@ module "audit_logging_encryption" {
name_prefix = var.name_prefix
account_id = var.account_id
bucket_names = local.bucket_names
protect_from_destroy = true
bucket_names = local.bucket_names
trail_name = "management-events"
kms_alias_name = "alias/vault-kms"
@@ -1,6 +1,6 @@
resource "aws_cloudtrail" "management_events" {
name = var.trail_name
s3_bucket_name = aws_s3_bucket.this["cloudtrail"].id
s3_bucket_name = local.bucket_ids["cloudtrail"]
include_global_service_events = true
is_multi_region_trail = true
is_organization_trail = false
+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
}
@@ -4,8 +4,29 @@ locals {
flowlogs = coalesce(var.bucket_names.flowlogs, "${var.name_prefix}-flowlogs")
}
bucket_ids = merge(
{ for key, bucket in aws_s3_bucket.protected : key => bucket.id },
{ for key, bucket in aws_s3_bucket.unprotected : key => bucket.id },
)
vault_kms_key_id = var.protect_from_destroy ? aws_kms_key.vault_protected[0].key_id : aws_kms_key.vault_unprotected[0].key_id
trail_arn = "arn:aws:cloudtrail:${var.region}:${var.account_id}:trail/${var.trail_name}"
kms_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 = "*"
}]
})
bucket_policies = {
cloudtrail = jsonencode({
Version = "2012-10-17"
+14
View File
@@ -0,0 +1,14 @@
moved {
from = aws_kms_key.vault
to = aws_kms_key.vault_protected[0]
}
moved {
from = aws_s3_bucket.this["cloudtrail"]
to = aws_s3_bucket.protected["cloudtrail"]
}
moved {
from = aws_s3_bucket.this["flowlogs"]
to = aws_s3_bucket.protected["flowlogs"]
}
+15 -8
View File
@@ -1,5 +1,5 @@
resource "aws_s3_bucket" "this" {
for_each = local.buckets
resource "aws_s3_bucket" "protected" {
for_each = var.protect_from_destroy ? local.buckets : {}
bucket = each.value
force_destroy = false
@@ -9,10 +9,17 @@ resource "aws_s3_bucket" "this" {
}
}
resource "aws_s3_bucket" "unprotected" {
for_each = var.protect_from_destroy ? {} : local.buckets
bucket = each.value
force_destroy = false
}
resource "aws_s3_bucket_versioning" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
bucket = local.bucket_ids[each.key]
versioning_configuration {
status = "Disabled"
@@ -22,7 +29,7 @@ resource "aws_s3_bucket_versioning" "this" {
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
bucket = local.bucket_ids[each.key]
rule {
apply_server_side_encryption_by_default {
@@ -37,7 +44,7 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
resource "aws_s3_bucket_public_access_block" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
bucket = local.bucket_ids[each.key]
block_public_acls = true
block_public_policy = true
@@ -48,7 +55,7 @@ resource "aws_s3_bucket_public_access_block" "this" {
resource "aws_s3_bucket_ownership_controls" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
bucket = local.bucket_ids[each.key]
rule {
object_ownership = "BucketOwnerEnforced"
@@ -58,14 +65,14 @@ resource "aws_s3_bucket_ownership_controls" "this" {
resource "aws_s3_bucket_policy" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
bucket = local.bucket_ids[each.key]
policy = local.bucket_policies[each.key]
}
resource "aws_s3_bucket_lifecycle_configuration" "this" {
for_each = local.lifecycle_rules
bucket = aws_s3_bucket.this[each.key].id
bucket = local.bucket_ids[each.key]
transition_default_minimum_object_size = "all_storage_classes_128K"
rule {
@@ -13,6 +13,12 @@ variable "account_id" {
type = string
}
variable "protect_from_destroy" {
description = "Protect stateful resources from destroy. prod は true 固定。既存 workspace で true→false に反転しない。"
type = bool
default = true
}
variable "bucket_names" {
description = "Optional existing bucket names. Omitted values are derived from name_prefix for isolated tests."
type = object({