New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] First class support for modules (HCL code generation and orchestration) #1115
Comments
Hi @cwe1ss, as discussed in discord, this use case makes sense for repositories managing local modules that want to benefit from code generation. Adding a possibility to orchestrate different commands by default or context seems reasonable too. Thanks for creating the detailed issue here, please give us some time to investigate different ways of making the desired functionality available. The proposed solutions make sense and will be considered and I will share a draft proposal asap. |
This definitely is not critical as there are some workarounds so please don't feel any pressure! I appreciate your work on this tool and I really have been liking it more and more over the last few weeks, so thank you very much! ❤️ |
It would be awesome to be able to do maybe also consider providing a metadata to be able to distinguish between modules and stacks when using |
We are planning new feature to make this possible. It will not be possible as a first class feature but the combination of different features can lead to the desired outcome.
This combination will allow to either use |
this is possible as of today. With experimental terramate scripts you can even define a script inside the |
I personally made peace with the fact that my reusable modules also are "stacks" and the mentioned combination of scripts & tag inheritance will make it much easier to differentiate between these reusable modules and actual runnable stacks. Having a separate I leave it up to you to decide if this issue should stay open. |
If we put the scripts inside a modules directory, would we be able to detect the changes with --changed ? this is real need here. |
Yes, Or in other words, on changes stacks a script is not defined for, the script will not run. |
The current main focus of Terramate is on stacks. Once a directory contains a
stack {}
-block, it can be used as a target for code generation, change detection and orchestration:generate_hcl
to generate any dynamic .tf file - e.g. to dynamically set backends, providers, or versions.terramate run terraform init
.This feature request proposes extending the Terramate functionality to regular modules.
In contrast to a stack, a module is a reusable set of Terraform resources, that is not meant to be planned/applied directly. Instead, it is referenced by one or many stacks with the appropriate variables for the target environment.
While modules are no target for "terraform plan/apply", there are still scenarios where modules would benefit from Terramate functionality:
Generate HCL
One might want to use the same variable definitions for a set of modules. For example, multiple modules might need a context.tf (from Cloud Posse) which allows passing common variables between modules and sub-modules.
With Terramate's "Generate HCL" feature, one could declare this file once and have it generated for each module, making the solution more DRY.
The demonstrated
_generated_context.tf
content is static, but there are also use-cases for generating dynamic HCL. Each module must declare itsrequired_providers
with a source and minimum version. By using thegenerate_hcl
feature of Terramate, one could centralize the generation of these declarations to keep version numbers in one location and to keep the code DRY. This means, that modules could also benefit from usingglobals
with their lazy evaluation.Generate File (with local file names)
It is already possible to generate regular files in arbitrary directories by setting
context = root
- e.g.However, this only supports setting an "absolute" path starting from the repository root ("/some/folder/file.txt"), so it is not possible to place a tm.hcl-file inside a module and generate a file within the same module without specifying the directory (which is error-prone if the directory is renamed)
Terramate Run
There are multiple scenarios where it would be good to use "terramate run" to invoke another program in all modules - e.g.
terraform validate
to validate the modulesterraform-docs
to create documentation for all modulesDescribe the solution you'd like
I don't know the internals of Terramate and I don't have a lot of experience with it yet, so I can only guess what a possible solution might be from a user's perspective:
module {}
-block that inherits most of the features fromstack
(HCL code generation, globals, ...).module
-context (analog to thestack
-context)module
-block/concept it can be documented accordingly with its differences to stacksIntroducing this into the CLI is probably tricky. I think, the nicer integration would be to introduce "stack"/"module" sub-commands, but this would obviously be a breaking change.
With sub-commands, instead of
terramate create
we could haveterramate stack create
andterramate module create
and adjust the logic/documentation to its needs.Other commands like
fmt
,generate
(which already operate on all child directories) would not need stack/module sub-commands.Another option would be to introduce e.g. a
--module
option to integrate the logic into existing commands - e.g.terramate create --module modules/my-module
. However, this option might not work with other options (e.g.--after
,--before
) and it would make the documentation more complex, since it would have to describe the differences.Describe alternatives you've considered
stack {}
blockstack {}
block within your modules. To ensure modules aren't included when running anyterramate
statements, one can use tags to exclude modules.generate_file
withcontext = root
generate_file
andcontext = root
. However,Additional context
Related Discord discussion
The text was updated successfully, but these errors were encountered: