terragrunt-atlantis-config
terragrunt-atlantis-config copied to clipboard
terraform source block in parent fails
If you would want to remove a duplication where you have in each child terragrunt.hcl:
terraform {
source = "..."
}
you can just move the block into your parent terragrunt.hcl by taking advantage of some terragrunt built-in functions(example)
However, terragrunt-atlantis-config does not seem to work with it. Just try to remove the terraform.source block from child terragrunt.hcl files in the official terragrunt live example repo and put the block in the root one:
root terragrunt.hcl:
locals {
# Automatically load account-level variables
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
# Automatically load region-level variables
region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
# Automatically load environment-level variables
environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
# Extract the variables we need for easy access
account_name = local.account_vars.locals.account_name
account_id = local.account_vars.locals.aws_account_id
aws_region = local.region_vars.locals.aws_region
}
terraform {
source = "git::[email protected]:gruntwork-io/terragrunt-infrastructure-modules-example.git//mysql?ref=v0.4.0" # yeah i know it's silly but that's not a point
}
The error you will get will be something like:
Error in function call; Call to function "find_in_parent_folders" failed: ParentFileNotFound: Could not find a account.hcl in any of the parent folders of..
version
terragrunt-atlantis-config version
terragrunt-atlantis-config 1.7.2
Oh interesting, this is a new issue for sure.
I guess the question comes though, how should terragrunt-atlantis-config determine if a file is a parent module? The best we can really do is guess if a file is a parent module or a child module (though we can sometimes be certain something is a child module), which this tool does using this logic: https://github.com/transcend-io/terragrunt-atlantis-config/blob/42554c62a0f51b7874d62808e75e28bf234e5f0a/cmd/parse_hcl.go#L25-L64
Without even diving into the code though, I'm just not sure at a high level even how we should tell parents from child modules if we can't look at the terraform.source field's presence as an indicator.
Is there a better way to do that check? 🤔
Thanks for the response! Hmm... even if we detect the parent file correctly, we still get another error like:
failed: ParentFileNotFound: Could not find a environment.hcl in any of the parent folders
Which I also believe it kind of contardicts with default --ignore-parent-terragrunt value. What do you think if we just ditch the isParent logic where it checks if the file has terraform.source and instead just add a new local variable like is_parent_terragrunt_file = true like it does it similarly with extra_atlantis_dependencies. Thus we can just explicitly tell in one place(or multiple if needed) that this file is indeed a parent if implicit isParent logic is not "smart" enough
I think that's ultimately the way to go, though it makes me a bit sad as it means this tool will no longer work (as in, give some atlantis.yaml output) for some repos unless they add the extra locals, which up until now have been entirely optional.
I think I will bundle this into the next major release, where I have a few other breaking changes in mind
You can still leave it optional. Is just that if there is a use-case like mine where isParentModule is not enough you can hint to terragrunt-atlantis-config explicitly. Like it hints with extra_atlantis_dependencies