-
-
Notifications
You must be signed in to change notification settings - Fork 980
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
[WIP] Terragrunt as a preprocessor #2403
base: main
Are you sure you want to change the base?
Conversation
This is incredible work. I love the approach. Much of my terraform code is a giant module in a single terraform state... It's so much easier to write. We've been in the process of migrating to terragrunt to get the benefits mentioned here, but it's been slow going. It's so nice to write terraform in this style, but as you said, it's a big headache operationally. One question: How would this handle nested modules? One of the reasons why you might want to have nested modules is that there may be cross-communication between different regions. Say you're deploying to multiple regions and you want to have roughly the same configuration in each region, but then you need to set up some communication between those regions (eg. a multi-region consul deployment). You could imagine a "region" module that includes a bunch of sub-modules, where each of the sub-modules gets a terraform state. Does this pre-processor handle that or is it limited to one level of modules? If it's limited to one level of modules, how would one implement multiple regions per env with some cross relationship between those regions? I guess you could have the tfvars split out into |
This is incredible work. I firmly believe that this is the way terraform should work (or at least allow us to work). In fact, I've been working on something similar, but from a totally external angle. I LOVE the way this is implemented and how it rolls into terragrunt. Specifically, I'm glad this is through terragrunt because terragrunt provides a number of conveniences and augmentations that terraform does not. It's become an indispensable tool for my work. I don't image building anything even sufficiently complex without terragrunt. I do, however, worry/wonder about the following: Ever been working on some CSS in LESS or SASS, and everything looks good --> and then you process and generate the stylesheets for your bundle...and you're thinking, "why the heck does my generated CSS not work/look right?" It's painful but workable to debug that and get to the bottom of the issue when you're able to run your code in a wannabe REPL (hot reloading vite/webpack server). In this case, I wonder what kind of pain or difficulty might emerge if/when the parsing/rendering produces an unexpected result, and you're trying to figure out what the root cause of that might be. Would there be some way to either step through or debug the I hope that made sense... |
Looks really great. |
As I understand, there is still something pending for this to work? |
Description
This is a WIP PR from a hackday project that implements the idea in #759 (comment) to turn Terragrunt into a preprocessor for Terraform (similar to how Sass and Less are preprocessors for CSS).
It is NOT yet ready for review and merge.
Video overview
video1729187535.mp4
Principles
Key idea: this is Terraform the way it should work. You get to write code in a way that works well from a developer perspective (simple, DRY) and after preprocessing that code, you get to deploy it in a way that works well from an operational perspective (secure, isolated, reviewable).
Input: pure, normal, native Terraform code
.tf
filesbackend
config in one place, handle dependencies between sub-modules, and so on.Here's an example of what the code could look like: https://github.com/gruntwork-io/terragrunt/tree/enhancement/hackday-terragrunt-preprocessor/test/fixture-preprocessor/before
By itself, this code is great from a developer perspective, but it's terrible from an operational perspective: see below for all the problems you'll run into.
Command:
terragrunt process
There's really only one command to run:
terragrunt process
.Output: pure, normal, native Terraform code
After you run
terragrunt pocess
, you get:.tf
files againbackend
is configured properly for each moduleterraform_remote_state
Here's an example of what the generated code could look like: https://github.com/gruntwork-io/terragrunt/tree/enhancement/hackday-terragrunt-preprocessor/test/fixture-preprocessor/after
This generated code is optimized to work well from an operational perspective.
Deploy using TF
terraform plan
,terraform apply
.terragrunt.hcl
, no_envcommon
, etc.The operational problems this fixes
Although "one giant root module with all your infra" is wonderful from a developer perspective, as it's easy to learn and keeps your code DRY, it has a bunch of drawbacks from an operational perspective:
plan
orapply
).plan
andapply
take forever (for a large infra, tens of minutes!).plan
output is way too big to meaningfully read, so you blindlyapply
changes, rarely catching mistakes that slip through.By using Terragrunt to "pre-process" your code, you get all the developer benefits when writing and maintaining the code, as it's simple and DRY, but now, because in the generated code, everything is broken up into separate environments and modules, all the operational problems above (security, speed, code review, automated testing, risk, and isolation) are mitigated!
TODOs before a full review & merging
source
URL in different environments) using override files.registry.terraform.io/hashicorp
URL).