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

before_hook not running before #2872

Open MattKitmanLabs opened 8 months ago

MattKitmanLabs commented 8 months ago

Describe the bug If I run aws-vault exec sandbox-poweruser -- chamber exec datadog -- terragrunt plan but my state files are all kept in production, terragrunt prompts me to create a new bucket in sandbox, then if I hit yes it creates a random bucket in sandbox and then runs the before_hook script to check that I'm attempting to run against production and tells me I'm in the wrong account. whoops too late you went and made a random bucket in sandbox.

What's going on here? It seems impossible to get the before hook to actually run before.

aws-vault exec sandbox-poweruser -- chamber exec datadog -- terragrunt plan
Remote state S3 bucket test-state does not exist or you don't have permissions to access it. Would you like Terragrunt to create it? (y/n) y
INFO[0016] Executing hook: terraform_account_check       prefix=[...]
!! Environment / Account mismatch !! please use:
aws-vault exec production-<engineers|poweruser> -- chamber exec datadog -- terragrunt <init|plan|apply|output|destroy>
ERRO[0016] Error running hook terraform_account_check with message: [...] exit status 1  prefix=[...]
ERRO[0016] Errors encountered running before_hooks. Not running 'terraform'.  prefix=[...]
ERRO[0016] 1 error occurred:
    * [...] exit status 1

Nice to have

Versions

Additional context Add any other context about the problem here.

denis256 commented 8 months ago

Hi, before_hook hook is executed before the execution of terragrunt command not terragrunt itself. To run custom commands/scripts can be attempted to use run_cmd function.

https://terragrunt.gruntwork.io/docs/features/hooks/ https://terragrunt.gruntwork.io/docs/reference/built-in-functions/#run_cmd

MattKitmanLabs commented 8 months ago

@denis256,

First let me thank you for the rapid response and useful info, but I also found this disable_init = true seems to stop terragrunt creating any buckets.

MattKitmanLabs commented 8 months ago

Scrap that, with disable_init = true terragrunt seems to have a hard time creating new modules.

github-actions[bot] commented 5 days ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for raising this issue.