Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
BATIAI-1679: Updating the dependent resources to have deterministic n…
Browse files Browse the repository at this point in the history
…ames at create-time (#11)
  • Loading branch information
bushong1 authored Jul 14, 2023
1 parent 87efffb commit 3f18e1a
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ locals {
}

resource "aws_s3_bucket_ownership_controls" "landing_zone_buckets" {
for_each = aws_s3_bucket.landing_zone_buckets
bucket = each.value.id
## Iterate over the list from var's to avoid some chicken/egg problems
for_each = toset(var.s3_bucket_names)
## Refer to the id from the bucket resource to retain the dependency
bucket = aws_s3_bucket.landing_zone_buckets[each.value].id
rule {
object_ownership = "BucketOwnerEnforced"
}
}

resource "aws_s3_bucket_public_access_block" "landing_zone_buckets" {
for_each = aws_s3_bucket.landing_zone_buckets
bucket = each.value.id
## Iterate over the list from var's to avoid some chicken/egg problems
for_each = toset(var.s3_bucket_names)
## Refer to the id from the bucket resource to retain the dependency
bucket = aws_s3_bucket.landing_zone_buckets[each.value].id

block_public_acls = true
block_public_policy = true
Expand All @@ -40,16 +44,20 @@ resource "aws_s3_bucket_public_access_block" "landing_zone_buckets" {
}

resource "aws_s3_bucket_versioning" "bucket_versioning" {
for_each = var.versioning_enabled ? aws_s3_bucket.landing_zone_buckets : {}
bucket = each.value.id
## Iterate over the list from var's to avoid some chicken/egg problems
for_each = var.versioning_enabled ? toset(var.s3_bucket_names) : []
## Refer to the id from the bucket resource to retain the dependency
bucket = aws_s3_bucket.landing_zone_buckets[each.value].id
versioning_configuration {
status = "Enabled"
}
}

resource "aws_s3_bucket_policy" "bucket" {
for_each = aws_s3_bucket.landing_zone_buckets
bucket = each.value.id
## Iterate over the list from var's to avoid some chicken/egg problems
for_each = toset(var.s3_bucket_names)
## Refer to the id from the bucket resource to retain the dependency
bucket = aws_s3_bucket.landing_zone_buckets[each.value].id

policy = jsonencode({
Version = "2012-10-17"
Expand All @@ -61,8 +69,8 @@ resource "aws_s3_bucket_policy" "bucket" {
Principal = "*"
Action = "s3:*"
Resource = [
"${each.value.arn}/*",
"${each.value.arn}",
"${aws_s3_bucket.landing_zone_buckets[each.value].arn}/*",
"${aws_s3_bucket.landing_zone_buckets[each.value].arn}",
]
Condition = {
Bool = {
Expand All @@ -76,8 +84,8 @@ resource "aws_s3_bucket_policy" "bucket" {
Principal = "*"
Action = "s3:*"
Resource = [
"${each.value.arn}/*",
"${each.value.arn}",
"${aws_s3_bucket.landing_zone_buckets[each.value].arn}/*",
"${aws_s3_bucket.landing_zone_buckets[each.value].arn}",
]
Condition = {
NumericLessThan = {
Expand All @@ -95,7 +103,7 @@ resource "aws_s3_bucket_policy" "bucket" {
}
Action = ["s3:ReplicateObject", "s3:ReplicateDelete", "s3:ReplicateTags"]
Resource = [
"${each.value.arn}/*",
"${aws_s3_bucket.landing_zone_buckets[each.value].arn}/*",
]
},
{
Expand All @@ -106,7 +114,7 @@ resource "aws_s3_bucket_policy" "bucket" {
}
Action = ["s3:GetReplicationConfiguration", "s3:ListBucket"]
Resource = [
"${each.value.arn}",
"${aws_s3_bucket.landing_zone_buckets[each.value].arn}",
]
}
]
Expand All @@ -115,8 +123,10 @@ resource "aws_s3_bucket_policy" "bucket" {
}

resource "aws_s3_bucket_server_side_encryption_configuration" "bucket" {
for_each = aws_s3_bucket.landing_zone_buckets
bucket = each.value.id
## Iterate over the list from var's to avoid some chicken/egg problems
for_each = toset(var.s3_bucket_names)
## Refer to the id from the bucket resource to retain the dependency
bucket = aws_s3_bucket.landing_zone_buckets[each.value].id

rule {
apply_server_side_encryption_by_default {
Expand All @@ -128,9 +138,10 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "bucket" {

# Lifecycle configuration for the dev buckets to remove all objects older than var.lifecycle_expiration_days.
resource "aws_s3_bucket_lifecycle_configuration" "lifecycle_expiration_days" {
for_each = var.lifecycle_expiration_days > 0 ? aws_s3_bucket.landing_zone_buckets : {}

bucket = each.value.id
## Iterate over the list from var's to avoid some chicken/egg problems
for_each = var.lifecycle_expiration_days > 0 ? toset(var.s3_bucket_names) : []
## Refer to the id from the bucket resource to retain the dependency
bucket = aws_s3_bucket.landing_zone_buckets[each.value].id

dynamic "rule" {
for_each = var.lifecycle_expiration_days > 0 ? [1] : []
Expand All @@ -146,4 +157,4 @@ resource "aws_s3_bucket_lifecycle_configuration" "lifecycle_expiration_days" {
}
}
}
}
}

0 comments on commit 3f18e1a

Please sign in to comment.