Trigger module based on input

I need to trigger a module only if a variable is present, is that achievable in terraform

so the module should run only if the var.lbname has some value.

module “record_set_creation” {
source = “…/r53-private-hosted-zone/record-set”
enable = var.create_subdomain
private_zone_id = var.private_zone_id
record_set_name = var.record_set_name
elb_hostname = var.lbname

If elb_hostname doesn’t have a default value, the module should fail, right? Do you want to silently skip it? I don’t think it’s possible to put a giant IF around that module statement. Maybe if you play with count?

If you’re using Terraform >= 0.12.20, you can use the experimental validation rules to ensure that elb_hostname is properly specified. But I’m afraid that’s not what you’re looking for here.

Thanks @gtirloni for your response.
I was looking for terraform to skip that module if the elb doesnot have a value.

Unfortunately module blocks don’t support meta parameters like count and for_each, so you can’t use the usual tricks to suppress a module completely.

Hi @dhineshbabuelango,

It looks like the module you’re using there already has a mechanism to “enable” it, in which case perhaps you can just extend the value of that argument:

module “record_set_creation” {
  source = “…/r53-private-hosted-zone/record-set”

  enable          = var.create_subdomain && var.lbname != ""
  private_zone_id = var.private_zone_id
  record_set_name = var.record_set_name
  elb_hostname    = var.lbname
}

I’m assuming here that inside the module it’s using conditional expressions to produce zero of all of the resources when var.enable is false. If that’s not already true, then making it true is the best answer to this question with the Terraform language features as they exist today.