首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Terraform创建多个不同名称的资源和目标文件?

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

Stack Overflow用户
提问于 2021-10-12 09:09:05
回答 1查看 225关注 0票数 0

例如,路径中有许多JSON文件

代码语言:javascript
复制
./test1.json
./test2.json
./test3.json
...

我想创建具有不同ids的多个任务

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-12 09:30:09

您可以使用for_each来实现这一点。例如:

代码语言:javascript
复制
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的各个实例。例如:

代码语言:javascript
复制
aws_dms_replication_task.test["task1"].replication_task_arn
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69537846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档