jy45v
jy45v

Reputation: 21

Terraform complex vars in root module / mapped to tfvars

I have the following module created, i wanted to know how to call this from a root module and use tfvars to define the object list

module/schedular/main.tf

locals {
  job_name = { for job_name in var.job_name : job_name["name"] => job_name }
}

resource "gooc_cloud_scheler_job" "schedular" {
  for_each    = local.job_name
  name        = each.key
  description = each.value["description"]
  schedule    = each.value["schedule"]

  dynamic "pubsub_target" {
    for_each = each.value["pubsub_target"] != null ? [each.value["pubsub_target"]] : []
    content {
      topic_name = pubsub_target.value["topic_name"]
      data       = base64encode(pubsub_target.value["data"])
    }
  }
}

module/schedular/variables.tf

variable "job_name" {
  default     = []
  type = list(object({
    name        = string,
    description = string,
    schedule    = string,
    pubsub_target = object({
      topic_name = string,
      data       = string,
    }),
  }))
}

module/schedular/terraform.tfvars

job_name = [
{
  description = "testing"
  name        = "testjob1"
  schedule    = "*/2 * * * *"
  pubsub_target = {
    data       = "test"
    topic_name = "projects/xxxxxx/topics/testing"
  }
    
}

The above works great, I then call this from the root module

rootmodule - main.tf

module "mycronjobs" {
  source = "/xxxx/xxx/ccc-scheduler"
  job_name = [
    {
      name = "testing"
      description = "testing"
      schedule = "*/5 * * * *"
      pubsub_target = {
        data = "testing1"
        topic_name = "projects/xxxxxxxxx/topics/testing"
      }
    },
    {
      name = "testing45"
      description = "testing45"
      schedule = "*/9 * * * *"
      pubsub_target = {
        data = "testing156"
        topic_name = "projects/xxxxxx/topics/testing22222"
      }
    }
  ]
}

This works, but i want to be able to define the object in the root module's tfvars file can anyone tell me what i need to add to the root modules main.tf to enable this to work with the root modules tfvars file . Thanks

Upvotes: 1

Views: 557

Answers (1)

mario
mario

Reputation: 46

As pointed out by the documentation terraform will automatically load the variables it finds in the .tfvars files.

So in your case, you would just have to:

  • create a new variable in the root module (you can just define it as a list if you already have type reinforcement in the module)
variable "job_name" {
  default     = []
  type = list()
}
  • create a terraform.tfvars file in the root module
jobname = [
    {
      name = "testing"
      description = "testing"
      schedule = "*/5 * * * *"
      pubsub_target = {
        data = "testing1"
        topic_name = "projects/xxxxxxxxx/topics/testing"
      }
    },
    {
      name = "testing45"
      description = "testing45"
      schedule = "*/9 * * * *"
      pubsub_target = {
        data = "testing156"
        topic_name = "projects/xxxxxx/topics/testing22222"
      }
    }
  ]
  • call the module
module "mycronjobs" {
  source = "/xxxx/xxx/ccc-scheduler"
  job_name = var.jobname
}

So basically you are doing what you did inside the module (in your example) but in the root module.

Upvotes: 3

Related Questions