terraform-module-versions icon indicating copy to clipboard operation
terraform-module-versions copied to clipboard

Terragrunt files

Open nilsdebruin opened this issue 4 years ago • 3 comments

It could be interesting to include terragrunt.hcl files to the version check. Terragrunt files have the following terraform configuration block (this is an example):

terraform { source = "[email protected]:terraform-aws-modules/terraform-aws-iam.git//modules/iam-assumable-roles?ref=v2.21.0" }

Do you know how this could be implemented?

nilsdebruin avatar Jan 28 '21 16:01 nilsdebruin

Hi @nilsdebruin, sorry about the late response - last month was a busy one for me.. I had a look at how Terragrunt does config parsing, and it looks like it actually provides a way to parse just those terraform { source=... } blocks:

I think we should be able to generate ModuleCall structs from these. I gave it a shot in #35 - could you have a look at that? I'm not really familiar with Terragrunt, so someone who actually uses it should definitely review that PR 😄

The meat of it is the file pkg/scan/terragrunt.go, the rest of the diff consists almost entirely of changes to vendored packages.

Here's how its output looks right now:

$ terraform-module-versions -terragrunt=true list examples 
TYPE NAME CONSTRAINT VERSION SOURCE
registry consul ~0.7.3 hashicorp/consul/aws
local local ./local
git examples/terragrunt.hcl v2.21.0 [email protected]:terraform-aws-modules/terraform-aws-iam.git//modules/iam-assumable-roles?ref=v2.21.0
git example_git_ssh_branch master git::ssh://[email protected]/keilerkonzept/terraform-module-versions?ref=master
git example_git_scp ~> 0.12 0.12.0 git::[email protected]:keilerkonzept/terraform-module-versions?ref=0.12.0
git consul_github_ssh ~0.1.0 0.1.0 [email protected]:hashicorp/terraform-aws-consul?ref=0.1.0
git consul_github_https_no_ref github.com/hashicorp/terraform-aws-consul
git consul_github_https_missing_ref 0.7.3 github.com/hashicorp/terraform-aws-consul
git consul_github_https 0.8.0 v0.8.0 github.com/hashicorp/terraform-aws-consul?ref=v0.8.0
$ terraform-module-versions -terragrunt=true check examples 
UPDATE? NAME CONSTRAINT VERSION LATEST MATCHING LATEST
(Y) consul ~0.7.3 0.7.11 0.8.4
(Y) consul_github_https 0.8.0 v0.8.0 v0.8.4
(Y) consul_github_https_missing_ref 0.7.3 v0.7.3 v0.8.4
(Y) consul_github_https_no_ref v0.8.4
Y consul_github_ssh ~0.1.0 0.1.0 v0.1.2 v0.8.4
(Y) example_git_scp ~> 0.12 0.12.0 3.0.9
(Y) example_git_ssh_branch master 3.0.9
(Y) examples/terragrunt.hcl v2.21.0 v3.8.0

sgreben avatar Feb 09 '21 10:02 sgreben

Hi @sgreben, don't worry, thank you for your time and effort! I am really appreciating that you are taking the time to dive into this. As I am not a Go developer, I can only look at it from a functional perspective. I checked out your code, build it, and tested it on a directory with .hcl files. I can validate that it works for this first example, really cool!

I have included a more elaborate example in the examples directory which I have submitted as a PR: https://github.com/keilerkonzept/terraform-module-versions/pull/39. This is a more extended example, which is based on the following repo: https://github.com/gruntwork-io/terragrunt-infrastructure-live-example.

When I run the Terragrunt version as follows:

./binaries/osx_x86_64/terraform-module-versions -terragrunt check examples/

I get the following output:

Error: Error in function call

  on examples/terragrunt-example/terraform/terragrunt.hcl line 9, in locals:
   9:   global_vars = read_terragrunt_config(find_in_parent_folders("global.hcl"))

Call to function "find_in_parent_folders" failed: ParentFileNotFound: Could not find a global.hcl in any of the parent folders of examples. Cause: Traversed
all the way to the root..`

ERRO[0000] Encountered error while evaluating locals.   
[terraform-module-versions] read terragrunt config "examples/terragrunt-example/terraform/terragrunt.hcl": examples/terragrunt-example/terraform/terragrunt.hcl:9,40-63: Error in function call; Call to function "find_in_parent_folders" failed: ParentFileNotFound: Could not find a global.hcl in any of the parent folders of examples. Cause: Traversed all the way to the root..

I think this is due to the fact that a terragrunt function is triggerd which is present in the .hcl file, namely:

find_in_parent_folders()

I am unsure if this can turned off and that the file can be scanned / parsed as is. I can imagine that this is quite a complex problem, so I understand if you just leave it at the first iteration (which in my opinion is already very cool!)

nilsdebruin avatar Feb 10 '21 21:02 nilsdebruin

This would be great feature man. Let me know if I can help somehow, but my Go knowledge is near to zero :(

zen avatar Dec 29 '21 17:12 zen