Terraform: A method to disable module conditionally

Created on 8 Aug 2018  ยท  5Comments  ยท  Source: hashicorp/terraform

It has been over a year since this issue was last updated (though its still open). Since it is locked I can't ask for an update in the issue.

The lack of this feature is getting very painful. If not via count, have any other methods to disable modules conditionally been developed?

Using enable on a module and then count internally doesn't work if there are data resources, since those resources don't exist.

Most helpful comment

I realised I didnt explain that very well so try this @tyrsius :

variable enable_module_mystuff {
    type = "string"
    default = "1"
}

module "mystuff" {
    source = "....."
    enabled = "${var.enable_module_mystuff}"
}

Then in the module:

variable "enabled" { 
    type = "string"
    default = "0"
}

resource "foo" {
    count = "${var.enabled}"
    ...
}

# example of a resource where you typically want a few of them...
resource "bar"
    count = "${var.enabled * var.num_availability_zones}"
    ...
}

# example of a datasource
data "aws_route_table" "my_rt" {
  count  = "${var.enabled * var.num_availability_zones}"
  tags {
    Name = "my_rt_${element(var.availability_zones, count.index)}"
  }
}

By having the var.enabled propagated through to the module, and having var.enabled on EVERY resource possible in the module, its possible to make the whole module conditional.

Hoping tf fix the count attribute on a module soon though, it would make it ++++better

All 5 comments

Agree that its sorely needed.

In the meantime, I use count on all my resources in my module, including datasources. The only place I cant use count is in outputs (outputs dont support count (there should be another bug about this I feel) )

For places which dont support count, I end up using a locals {} block with a lookup(), providing a default if not existing to achieve the same in an output.

In this way I can make the whole module conditional, but it does involve some tricks.

Let me know if you want an example.

I realised I didnt explain that very well so try this @tyrsius :

variable enable_module_mystuff {
    type = "string"
    default = "1"
}

module "mystuff" {
    source = "....."
    enabled = "${var.enable_module_mystuff}"
}

Then in the module:

variable "enabled" { 
    type = "string"
    default = "0"
}

resource "foo" {
    count = "${var.enabled}"
    ...
}

# example of a resource where you typically want a few of them...
resource "bar"
    count = "${var.enabled * var.num_availability_zones}"
    ...
}

# example of a datasource
data "aws_route_table" "my_rt" {
  count  = "${var.enabled * var.num_availability_zones}"
  tags {
    Name = "my_rt_${element(var.availability_zones, count.index)}"
  }
}

By having the var.enabled propagated through to the module, and having var.enabled on EVERY resource possible in the module, its possible to make the whole module conditional.

Hoping tf fix the count attribute on a module soon though, it would make it ++++better

Hi @tyrsius and @gtmtech,

No updates have been posted on the other issue because we've not yet got to that work. It's still planned to do, and you can see the initial groundwork for it being discussed in the recent blog post on _For and For Each_ (the section titled _Module count and for_each_)

We'll post in #953 again once that work has begun. Until then, since this issue is effectively a duplicate of #953 I'm going to close this one. The comments on that issue were closed because it seemed like useful discussion had concluded and it was just attracting "me too!" comments, which are not needed because we already understand the need and have a plan to address it.

Agree that its sorely needed.

In the meantime, I use count on all my resources in my module, including datasources. The only place I cant use count is in outputs (outputs dont support count (there should be another bug about this I feel) )

For places which dont support count, I end up using a locals {} block with a lookup(), providing a default if not existing to achieve the same in an output.

In this way I can make the whole module conditional, but it does involve some tricks.

Let me know if you want an example

my code calls modules under modules until it reaches the last leaf which creates the code. It would be clumsy to have this field with all of them.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thebenwaters picture thebenwaters  ยท  3Comments

ronnix picture ronnix  ยท  3Comments

shanmugakarna picture shanmugakarna  ยท  3Comments

c4milo picture c4milo  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments