gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
7.92k stars 966 forks source link

terragrunt-exclude-dir not respected when running apply-all #2481

Open JustinYeoh93 opened 1 year ago

JustinYeoh93 commented 1 year ago

I'm trying to exclude a directory when I run terragrunt apply. But from what I've been trying

The command terragrunt run-all apply --terragrunt-exclude-dir us-west-1 is to run everything except the directory in us-west-1. But as the pre-apply output below shows, it is still applying us-west-1.

➜  platform git:(master) tg run-all apply --terragrunt-exclude-dir us-west-1                                                                  
INFO[0000] The stack at /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform will be processed in the following order for command apply:
Group 1
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/kms
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/networking/vpc
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/vpn/ami
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/vpn/ip
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/vpn/key_pair
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/global/route53/public_hosted_zone
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/us-west-1/networking/vpc

Group 2
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/acm/public
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/vpn/security_group
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/global/route53/private_hosted_zone
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/us-east-1/acm/public
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/us-west-1/cluster/eks

Group 3
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/cluster/eks

Group 4
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/cluster/flux
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/rds
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/vpn/ec2

Group 5
- Module /Users/someuser/Projects/some-repo/iac/live/123456789012/some-repo/some-proj/test/platform/ap-northeast-1/vpn/openvpn

Any opinion on what I'm doing wrong would be much appreciated.

denis256 commented 1 year ago

Hi, was attempted to use --terragrunt-exclude-dir **/us-west-1 or --terragrunt-exclude-dir us-west-1/** ?

JustinYeoh93 commented 1 year ago

Hi there Denis,

Thank you very much for responding!

I've tried both your suggestions and it still takes the us-west-1 into consideration.

naresh-juniper commented 11 months ago

@denis256 Any updates on this issue?

bushong1 commented 8 months ago

I'm running into this same issue. Best I can tell, globstar [**] is not something that works out of the box in golang. Glob [*] will only work for 1 directory, as wildcard on paths will never include a slash.

So in this example, you're trying to exclude us-west-1, but in the code it's looking for a module at path us-west-1. There's no module at us-west-1, because the modules are us-west-1/networking/vpc and us-west-1/cluster/eks. Further, you couldn't do us-west-1/* because that would only match one subdirectory. You could do us-west-1/*/*, but if you have future levels of subdirectories, they wouldn't be excluded.

So... the [very annoying] way around this would be:

terragrunt run-all apply --terragrunt-exclude-dir 'us-west-1/*' --terragrunt-exclude-dir 'us-west-1/*/*' --terragrunt-exclude-dir 'us-west-1/*/*/*' --terragrunt-exclude-dir 'us-west-1/*/*/*/*' # ... etc

Or slightly more concisely but less readable:

terragrunt run-all apply --terragrunt-exclude-dir=us-west-1/{*,*/*,*/*/*,*/*/*/*}

Edit:

You could also do this using the native envvar splitter and the undocumented env var "TERRAGRUNT_EXCLUDE_DIR"

So it would look like:

export TERRAGRUNT_EXCLUDE_DIR="us-west-1/*,us-west-1/*/*,us-west-1/*/*/*,us-west-1/*/*/*/*"
terragrunt run-all apply
ghost commented 7 months ago

I'm seeing similar behavior with terragrunt run-all init. I have a monorepo of internal modules that looks like this:

./aws-module-1
./aws-module-2
...

I'm setting the TERRAGRUNT_EXCLUDE_DIR variable as described above:

export TERRAGRUNT_EXCLUDE_DIR='aws-module-1,aws-module-1/*,aws-module-1/*/*,aws-module-2,aws-module-2/*'

But when I execute the run-all init command, I see the following in my pipeline logs:

msg=The stack at /builds/terraform-aws-modules will be processed in the following order for command init:
Group 1
- Module /builds/terraform-aws-modules/aws-module-1
- Module /builds/terraform-aws-modules/aws-module-2
- ...

Any ideas? I've also tried this with the CLI argument (--terragrunt-exclude-dir) but was getting errors with the run-all init.

cpboyd commented 5 months ago

It'd be nice if globstar was supported, or at least specifying a prefix as is desired in this case.