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
+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 {