【问题标题】:In Terraform how to reference ID's of one resource in another resource?在 Terraform 中,如何在另一个资源中引用一个资源的 ID?
【发布时间】:2021-12-11 14:28:19
【问题描述】:

下面是我在从aws_appsync_function.appsync_functions 捕获function_id 并引用创建aws_appsync_resolver 的代码

#---- Create AppSync Functions -----
resource "aws_appsync_function" "appsync_functions" {
  for_each                  = var.appsync_function
  name                      = each.value.name
  api_id                    = aws_appsync_graphql_api.appsync.id
  data_source               = each.value.data_source
  request_mapping_template  = file(each.value.request_template_path)
  response_mapping_template = file(each.value.response_template_path)
  description               = each.value.description
  }


#---- Create AppSync Resolvers -----
resource "aws_appsync_resolver" "appsync_pipeline_resolver" {
  type              = "Query"
  api_id            = aws_appsync_graphql_api.appsync.id
  field             = var.appsync_resolver.trailheadItemById.name
  request_template  = file(var.appsync_resolver.trailheadItemById.request_template_path)
  response_template = file(var.appsync_resolver.trailheadItemById.response_template_path)
  kind              = "PIPELINE"
  for_each                  = var.appsync_function
  pipeline_config {
    functions = aws_appsync_function.appsync_functions[each.key].name==var.appsync_resolver.trailheadItemById.name ? aws_appsync_function.appsync_functions["trailheadItemById"].function_id : ""
  }
}

上面的代码捕获了我放置在 pipeline_config 下的所有 function_id 和条件,但无法正常工作!我可以获得语法方面的帮助来完成这项工作吗?

谢谢。

【问题讨论】:

  • 你想在哪里引用function_id,即aws_appsync_resolver的哪个参数?
  • “不工作!” - 不具体。到底发生了什么?有任何错误消息吗?

标签: amazon-web-services terraform terraform-provider-aws aws-appsync aws-appsync-resolver


【解决方案1】:

functions 是一个列表,而不是string。应该是:

  pipeline_config {
    functions = [aws_appsync_function.appsync_functions[each.key].name==var.appsync_resolver.trailheadItemById.name ? aws_appsync_function.appsync_functions["trailheadItemById"].function_id : ""]
  }

但可能必须使用动态块使其成为可选:

  dynamic "pipeline_config" {
     for_each = aws_appsync_function.appsync_functions[each.key].name==var.appsync_resolver.trailheadItemById.name ? [1]: []
     content {
        functions = [aws_appsync_function.appsync_functions["trailheadItemById"].function_id]     
     }
  }

【讨论】:

  • 没有用,它在那里创建了 5 次解析器作为 5 个函数对象
【解决方案2】:

这个成功了……!!万岁

我正在做的错误是提及

for_each = var.appsync_function in resource "aws_appsync_resolver"

#---- Create AppSync Functions -----
resource "aws_appsync_function" "sfdc_appsync_functions" {
  for_each                  = var.appsync_function
  name                      = each.value.name
  api_id                    = aws_appsync_graphql_api.sfdc_appsync.id
  data_source               = each.value.data_source
  request_mapping_template  = file(each.value.request_template_path)
  response_mapping_template = file(each.value.response_template_path)
  description               = each.value.description
  depends_on = [aws_appsync_graphql_api.sfdc_appsync, aws_appsync_datasource.sfdc_appsync_datasource]
  }

#---- Create AppSync Resolvers -----
# PIPELINE type resolver
resource "aws_appsync_resolver" "sfdc_appsync_pipeline_resolver" {
  for_each          = var.appsync_resolver
  type              = "Query"
  api_id            = aws_appsync_graphql_api.sfdc_appsync.id
  field             = each.value.name
  request_template  = file(var.appsync_resolver.trailheadItemById.request_template_path)
  response_template = file(var.appsync_resolver.trailheadItemById.response_template_path)
  kind              = "PIPELINE"
  pipeline_config {
        functions = [aws_appsync_function.sfdc_appsync_functions["GetContentById"].function_id]
  }
  depends_on = [aws_appsync_graphql_api.sfdc_appsync, aws_appsync_datasource.sfdc_appsync_datasource, aws_appsync_function.sfdc_appsync_functions]
}

cc:@Marcin

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    • 2019-10-02
    • 2021-04-01
    相关资源
    最近更新 更多