简体   繁体   English

如何使用 Terraform 创建具有不同名称和目标文件的多个资源?

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

For example, there are many JSON files in the path比如路径下有很多JSON个文件

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

I want to create multiple tasks with different ids我想创建多个具有不同 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
}

...

Put them into one resource, is there a way to use for_each?把它们放在一个资源中,有没有办法使用for_each?

You can do this with for_each .您可以使用for_each执行此操作。 For example:例如:

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
}

Once this is done, you can refer to individual instances of aws_dms_replication_task using key value.完成后,您可以使用键值引用aws_dms_replication_task的各个实例。 For example:例如:

aws_dms_replication_task.test["task1"].replication_task_arn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用特定的命名约定创建多个目标组 terraform - How to create multiple target groups with specific naming conventions terraform Terraform 按顺序创建资源 - Terraform Create Resources In Order Terraform 使用 for_each 和 jsondecode 创建多个资源 - Terraform create multiple resources using for_each and jsondecode 如何使用 Terraform 中的循环来部署 2 个不同的 azure 资源 - how to use Loops in Terraform to deploy 2 different azure resources terraform 模块中的多个资源 - 如何从主模块引用 - Multiple resources in a terraform module - how to refer from main module 如何引用terraform中的GCP资源? - how to refer GCP resources in terraform? 如何使用 terraform 创建多个不同的策略 - How to create several different policies with terraform 如何解析Terraform“已经存在”。 应用 terraform 时多个资源出错? - How to resolve Terraform "already exists." error for multiple resources while terraform apply? Terraform --创建多个托管分区并为每个分区分配不同的记录 - Terraform --Create multiple hosted zoned and assign different records for each zones Terraform - 在不同资源之间传递变量 - Terraform - passing vars between different resources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM