簡體   English   中英

在 Terraform 中如何引用另一個資源中一個資源的 ID?

[英]In Terraform how to reference ID's of one resource in another resource?

下面是我在從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 和條件不起作用! 我可以獲得語法方面的幫助以完成這項工作嗎?

謝謝你。

函數是一個列表,而不是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]     
     }
  }

這個有效... !! 歡呼

我做的錯誤是提到

for_each = var.appsync_functionresource "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]
}

抄送:@Marcin

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM