簡體   English   中英

如何使用 Terraform 創建具有不同名稱和目標文件的多個資源?

[英]How to create multiple resources with different names and target files with Terraform?

比如路徑下有很多JSON個文件

./test1.json
./test2.json
./test3.json
...

我想創建多個具有不同 ID 的任務

resource "aws_dms_replication_task" "test1" {
  replication_task_id       = "test-dms-replication-task-tf-test1"
  table_mappings            = file("${path.module}/test1.json")
  source_endpoint_arn       = aws_dms_endpoint.test-dms-source-endpoint-tf.endpoint_arn
  target_endpoint_arn       = aws_dms_endpoint.test-dms-target-endpoint-tf.endpoint_arn
}

resource "aws_dms_replication_task" "test2" {
  replication_task_id       = "test-dms-replication-task-tf-test2"
  table_mappings            = file("${path.module}/test2.json")
  source_endpoint_arn       = aws_dms_endpoint.test-dms-source-endpoint-tf.endpoint_arn
  target_endpoint_arn       = aws_dms_endpoint.test-dms-target-endpoint-tf.endpoint_arn
}

...

把它們放在一個資源中,有沒有辦法使用for_each?

您可以使用for_each執行此操作。 例如:

variable "rule_files" {
   default = ["test1", "test2", "test3"]
}


resource "aws_dms_replication_task" "test" {

  for_each                  = var.rule_files

  replication_task_id       = "test-dms-replication-task-tf-${each.key}"
  table_mappings            = file("${path.module}/${each.key}.json")
  source_endpoint_arn       = aws_dms_endpoint.test-dms-source-endpoint-tf.endpoint_arn
  target_endpoint_arn       = aws_dms_endpoint.test-dms-target-endpoint-tf.endpoint_arn
}

完成后,您可以使用鍵值引用aws_dms_replication_task的各個實例。 例如:

aws_dms_replication_task.test["task1"].replication_task_arn

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM