How to create optional resources that depend on modules using Terraforms for_each

If you need to turn on/off resources that depend on a module that uses for_each read on

Eduardo Lugo
Eduardo Lugo
  • Twitter
  • LinkedIn

How to create optional resources that depend on modules using for_each in Terraform

Terraform 0.13 introduced a really cool and expected set of features with for_each and depends_on for modules, you can read some of it here.

In our use case we were using the Autoscaling Group (ASG) module and then we needed to scale the group up/down automatically, which meant we had dependent resources of the module. Since the choice of automatic scheduling is optional, the resources doesn't always get created.

At first we were only creating one ASG and two automatic scaling resources (startup/shutdown). We had a number of variables for both resources, and a count with a condition for the schedules to check if the resource was created or not.

Here’s what that looks like:

Multiple ASGs

Later our requirement changed and we needed several ASGs to handle operations during the work day. We needed to specify configurations for each ASG and a work day schedule for each of them.

One of the cool things about using for_each is that we can consolidate variables into a configuration map. That map looks like this

groups = {
  “main” = {
    “number_of_nodes” = 1,
    “startup_cron” = “40 15 * * MON”,
    “shutdown_cron” = “45 23 * * FRI”,
    “number_of_nodes_on_shutdown” = 0,
},
  “analytics” = {
    “number_of_nodes” = 2
    “startup_cron” = “40 15 * * MON”,
    “shutdown_cron” = “45 23 * * FRI”,
    “number_of_nodes_on_shutdown” = 0,
  }
}

Let’s see how the initial code change when using for_each. Notice that in this case scenario we are assuming all attributes are mandatory, so every ASG will have schedules to startup and shutdown.

That’s cool but in a real life scenario each ASG could have different configurations, automatic schedules should be optional!

How to we adapt the for_each to check if the dependent resources need to be created or not?

We need to have a configuration that will allow us to have a main ASG that’s always running and an analytics one that will turn on/off on schedule. So the cron related attributes become optional

groups = {
  “main” = {
    “number_of_nodes” = 1,
},
  “analytics” = {
    “number_of_nodes” = 2
    “startup_cron” = “40 15 * * MON”,
    “shutdown_cron” = “45 23 * * FRI”,
    “number_of_nodes_on_shutdown” = 0,
  }
}

For that our terraform will need to look like this

We now have optional resources using for_each on terraform!

TL;DR The for inside for_each goes thru the configuration map trying to find the attribute name inside the keys of each element, if found the resource gets created, otherwise it does not.

This worked out nicely for us. Hope it helps you too.

Latest Stories

Here’s what we’ve been up to recently.