如何用Terraform创建多个不同名称的资源和目标文件? [英] How to create multiple resources with different names and target files with Terraform?

查看:0
本文介绍了如何用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
}

完成后,您可以使用key value引用aws_dms_replication_task的单个实例。例如:

aws_dms_replication_task.test["task1"].replication_task_arn

这篇关于如何用Terraform创建多个不同名称的资源和目标文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆