例如,路径
中有许多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