Home > OS >  In Terraform how to reference ID's of one resource in another resource?
In Terraform how to reference ID's of one resource in another resource?

Time:10-28

Below is the code I have where am stuck in capturing function_id from aws_appsync_function.appsync_functions and reference to create 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 : ""
  }
}

Above code capturing all the function_id's and conditions I placed under pipeline_config not working! Can I get help with syntax to get this work?

Thankyou.

CodePudding user response:

functions is a list, not string. It should be:

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

But probably have to use dynamic blocks to make it optional:

  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]     
     }
  }

CodePudding user response:

This one worked... !! Hurray

Mistake I was doing is mentioning

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

  • Related