terraform get json file path

626 Views Asked by At

I'm using terraform to manage ibm cloud resources. I'm creating apis for api gate way.

However, resource requires a json file and its path. I have to modify json file for all different environments and regions.

resource "ibm_api_gateway_endpoint" "endpoint"{
    service_instance_crn = ibm_resource_instance.apigateway.id
   open_api_doc_name    = "${path.module}/${var.environment}-api-definitions.yaml"
}

It works this way with a yml (or json) file. If i use jsonencode or tempfile for open_api_doc i got an error that states open_api_doc_name requires a json,yml file. Is there a way to convert jsonencode to a file path?

1

There are 1 best solutions below

1
On

open_api_doc_name attribute in ibm_api_gateway_endpoint resource doesn't accept file as a string.. It accepts only file path/location If you want to use jsonencode, may be you can make use of local_file resource to create a file and use that file in endpoint resource.. some thing like..

resource "local_file" "test" {
    content     = jsonencode({"key"="value"})
    filename = "${path.module}/test.json"
}
resource "ibm_api_gateway_endpoint" "file_endpoint" {
  service_instance_crn = ibm_resource_instance.apigateway.id
  name                 = var.endpoint_name
  managed              = true
  open_api_doc_name    = local_file.test.filename
  type                 = var.action_type
}