[英]GCP Log-export pub/sub with multiple sink filters with Terraform
我正在嘗試創建一個 terraform 模塊,該模塊將過濾器列表作為參數並基於它創建接收器。 我只想使用一個發布/訂閱主題。 我曾嘗試使用for_each
根據過濾器列表創建接收器,但我最終得到了Cycle-error
。 這是一個過濾器解決方案的 terraform 網站鏈接。
這是我嘗試過的:
module "log_export" {
for_each = toset(var.log_error_filters) // Terraform list ["filter1", "filter2"]
source = "terraform-google-modules/log-export/google"
destination_uri = "${module.destination.destination_uri}"
filter = each.key
log_sink_name = "pubsub_example_logsink"
parent_resource_id = var.project_id
parent_resource_type = "project"
unique_writer_identity = true
}
module "destination" {
for_each = module.log_export
source = "terraform-google-modules/log-export/google//modules/pubsub"
project_id = var.project_id
topic_name = "sample-topic"
log_sink_writer_identity = "${module.log_export[each.key].writer_identity}"
create_subscriber = true
}
在模塊“destination”中, module.log_export
是一個列表,因此需要循環它,因為log_sink_writer_identity
-field 需要是一個字符串。
有任何想法嗎?
解決此問題的一種方法是使用多行字符串作為 Terraform 變量,其中包含用OR
分隔的過濾器。 這是完整的解決方案:
module "log_export" {
source = "terraform-google-modules/log-export/google"
destination_uri = "${module.destination.destination_uri}"
filter = var.log_error_filters
log_sink_name = "pubsub_example_logsink"
parent_resource_id = var.project_id
parent_resource_type = "project"
unique_writer_identity = true
}
module "destination" {
source = "terraform-google-modules/log-export/google//modules/pubsub"
project_id = var.project_id
topic_name = "sample-topic"
log_sink_writer_identity = "${module.log_export.writer_identity}"
create_subscriber = true
}
其中var.log_error_filters
是這樣構建的:
log_error_filters = <<EOF
severity >= ERROR OR
filter2 here
EOF
您可以使用另一個 for_each 引用在另一個模塊中創建的對象列表。 writer_identity(以及任何其他屬性)是引用列表中的值。
module "destination" {
for_each = module.log_export
source = "terraform-google-modules/log-
export/google//modules/pubsub"
project_id = var.project_id
topic_name = "sample-topic"
log_sink_writer_identity = each.value.writer_identity
create_subscriber = true
}
for_each 的語法可能需要對您進行一些調整,因為引用通常是for_each = <resource>.<name>
即for_each = google_logging_project_sink.my_logging_sinks
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.