Best practices for non-cloud resource or data-only Terraform modules

I originally posted this to Reddit w/ the same title Best practices for non-cloud resource or data-only Terraform modules, but I think this is the right place to repost it too.

I’m looking for ways and ideas to develop a Terraform module which does not create any resources on cloud or any resources in local environment like files, but a module that generates some data or metadata, based on an input data.

For example, let’s say I need to capture a concept of a customer for which I need to generate some data or metadata and bundle those together as output and pass over as input read by other modules.

Something like this:

  1. A customer module takes a customer name and basic details about required AKS node(s) , then generates customer identifier, customer tags, digests AKS node requirements into more complete descriptor. Such module could make up a “customers metadata stack”. The important issue is that this module would not deploy any provider-based cloud resources. It would only create things like terraform_data, random_id, etc. So, although those feature do create actual Terraform resources, for this project I’d rather consider them as primitives, values, used similarly as in object value pattern in Domain-Driven Design.

  2. A cluster module is given the customer module output and turns it into AKS nodes(s) deployment that is provisioned, named and tagged according to what the customer module generated. This module would be part of the “customers platform stack”.

Although the part 2. is clear and simple yet typical Terraform module creating real on cloud resource with azurerm_kubernetes_cluster and azurerm_kubernetes_cluster_node_pool, the part 1. is not as clear and I have number of questions.

  • Does this idea make sense or am I trying to bend Terraform in ways it was not meant to be?
  • What other Terraform features apart from terraform_data, random_id are available for such data and metadata ‘values’?
  • What other suggestions would you have for such data/value-only module?
  • Do you know any Terraform modules which are good examples for data/value-only modules?

Although what you have described is not a pattern I’ve seen employed commonly, I think architecturally there’s nothing wrong with it and I’ve seen several organizations follow a pattern like this with success. (One of them was me, in a previous job.)

However, usually when I think of “data-only module” I don’t think about there being resource blocks inside it. That makes the module inherently stateful – those resources will persist from one plan/apply round to the next – which isn’t necessarily a problem but does require a little more care, such as making sure to preserve those stateful objects between rounds should you decide to refactor the module in future.

However, modern Terraform has facilities such as moved blocks to help with that, so I don’t think it’s a big deal in practice. It just means that if you make any changes to the module in future that would cause those resources to change addresses then you would need to add moved blocks too so that Terraform will automatically migrate the existing state objects to the new addresses.

1 Like

This is also the kind of answer (or confirmation) I am looking for. Thank you!