0

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.

sudhir tataraju
  • 1,159
  • 1
  • 14
  • 30
  • Where do you want to reference the function_id, i.e., in which of the arguments of the `aws_appsync_resolver`? – Marko E Oct 26 '21 at 07:52
  • "not working! " - is not specific. What exactly is happening? Any error msgs? – Marcin Oct 26 '21 at 08:03

2 Answers2

1

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]     
     }
  }
Marcin
  • 215,873
  • 14
  • 235
  • 294
0

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

sudhir tataraju
  • 1,159
  • 1
  • 14
  • 30