I'm trying to set up logging for a Google Storage bucket(already existing) using Terraform, but I'm running into a cyclic dependency issue. Here's my current Terraform configuration:
resource "google_storage_bucket" "user_log_bucket" {
name = google_storage_bucket.user_topic_bucket.name + "-logs"
location = var.location
force_destroy = true
}
resource "google_storage_bucket" "user_topic_bucket"{
name = "user-topic-bucket-${var.env}"
location = var.location
force_destroy = true
uniform_bucket_level_access = true
versioning {
enabled = true
}
logging {
log_bucket = google_storage_bucket.user_log_bucket.name
log_object_prefix = "log"
}
}
The user_topic_bucket is supposed to log to user_log_bucket, but user_log_bucket's name depends on user_topic_bucket, creating a cyclic dependency.
I have tried decoupling the bucket creation and the logging configuration, but I can't find a way to do this within Terraform's current resources and configurations.
How can I resolve this cyclic dependency in Terraform when setting up logging between two Google Storage buckets? Is there a recommended way to separate the creation and logging configuration steps, or is there another approach I should consider?
Any help or examples would be greatly appreciated!
I have tried decoupling the bucket creation and the logging configuration, but I can't find a way to do this within Terraform's current resources and configurations.
Put the name of the bucket in locals
and use it in both resources in the name
argument. Leave reference to another bucket only in the logging
block.
locals {
bucket_name = "user-topic-bucket-${var.env}"
}
resource "google_storage_bucket" "user_log_bucket" {
name = local.bucket_name + "-logs"
location = var.location
force_destroy = true
}
resource "google_storage_bucket" "user_topic_bucket"{
name = local.bucket_name
location = var.location
force_destroy = true
uniform_bucket_level_access = true
versioning {
enabled = true
}
logging {
log_bucket = google_storage_bucket.user_log_bucket.name
log_object_prefix = "log"
}
}
Put the name of the bucket in user_log_bucket
and then reference this name in user_topic_bucket
and use trimsuffix
function to remove -logs
suffix.
resource "google_storage_bucket" "user_log_bucket" {
name = "user-topic-bucket-${var.env}-logs"
location = var.location
force_destroy = true
}
resource "google_storage_bucket" "user_topic_bucket"{
name = trimsuffix(google_storage_bucket.user_log_bucket.name, "-logs")
location = var.location
force_destroy = true
uniform_bucket_level_access = true
versioning {
enabled = true
}
logging {
log_bucket = google_storage_bucket.user_log_bucket.name
log_object_prefix = "log"
}
}