例如,路径中有许多JSON文件
./test1.json
./test2.json
./test3.json
...我想创建具有不同ids的多个任务
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?
发布于 2021-10-12 09:30:09
您可以使用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_arnhttps://stackoverflow.com/questions/69537846
复制相似问题