How to migrate multiple modules in a single state file to separate state files for terraform 0.12

while executing
terraform output --module=abc doesn’t work and gives error
The -module option is no longer supported since Terraform 0.12
This breaks our current process where we are building modules on target basis.

Hi,

When you say separate state files do you mean splitting the modules up into their own individual set of Terraform configs. I.e. given a single config with 3 modules and single state file, your end goal would be 3 sets of Terraform config and 3 state files?

If this is the case there is unfortunately no easy way to do this and would require manually importing infrastructure (terraform import) into the new files.

That said; if you only want to see the outputs for a single module terraform 0.12 supports complex objects and full resources, you could aggregate multiple module outputs into a single output variable.

Output variable:

output "aggregated" {
  value = {
    grafana_password    = data.kubernetes_secret.grafana.data.admin-password
    loadbalancer_consul = kubernetes_service.consul.load_balancer_ingress.0.ip
  }
}

Terraform output:

$ terraform output aggregated
aggregated = {
  "grafana_password" = "0XRFO9tN6hTVfRJC23kY8vug6jZsIYrPaotXDb5"
  "loadbalancer_consul" = "10.0.1.23"
}

Or as as JSON:

$ terraform output -json aggregated
{
  "grafana_password": "0XRFO9tN6hTVfRJC23kY8vug6jZsIYrPaotXDb5",
  "loadbalancer_consul": "10.0.1.23"
}

Or as JSON and piped into jq:

$ terraform output -json aggregated | jq -r .grafana_password
0XRFO9tN6hTVfRJC23kY8vug6jZsIYrPaotXDb5

Hi nic

Thanks for the reply, also to get the aggregated output, the output needs to be defined at root level along with module level, which may be difficult for our current setup.
For this, we are planning to split the state files per module.
for example we have a single state file with 3 modules, we need to generate 3 state files.