Skip to content
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

Arguments not parsed when running Python script from before_hook #3814

Closed
kstevensonnv opened this issue Jan 28, 2025 · 2 comments
Closed

Arguments not parsed when running Python script from before_hook #3814

kstevensonnv opened this issue Jan 28, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@kstevensonnv
Copy link

Describe the bug

When I run a Python script from before_hook and use the argparse library arguments are not parsed.

Steps To Reproduce

.
├── deployment
│   └── terragrunt.hcl
└── modules
    └── some-module
        ├── main.tf
        └── my_script.py

deployment/terragrunt.hcl:

terraform {
  source = "../modules//some-module"

  before_hook "python_script" {
    commands     = ["plan", "apply"]
    execute      = [
      "./my_script.py",
      "--arg1 argument1",
      "--arg2 argument2",
      "--arg3 argument3"
    ]
  }
}

modules/some-module/main.tf:

output "my_output" {
    value = "value"
}

modules/some-module/my_script.py:

#!/usr/bin/env python3

import argparse


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-a", "--arg1", type=str, required=True)
    parser.add_argument("-b", "--arg2", type=str, required=True)
    parser.add_argument("-c", "--arg3", type=str, required=True)
    args = parser.parse_args()

    print(args)

Expected behavior

Arguments are parsed.

Device:some-module username$ ./my_script.py --arg1 argument1 --arg2 argument2 --arg3 argument3
Namespace(arg1='argument1', arg2='argument2', arg3='argument3')

Nice to haves

Device:deployment username$ terragrunt apply
16:00:23.455 INFO   Downloading Terraform configurations from ../modules into ./.terragrunt-cache/MRg6BF8iUt6goQDYcu5OoqH_p3k/HfCJfD21cilNj44BjpZsrrU6T90
16:00:23.655 INFO   terraform: Initializing the backend...
16:00:23.657 INFO   terraform: Initializing provider plugins...
16:00:23.657 INFO   terraform: Terraform has been successfully initialized!
16:00:23.657 INFO   terraform: 
16:00:23.657 INFO   terraform: You may now begin working with Terraform. Try running "terraform plan" to see
16:00:23.657 INFO   terraform: any changes that are required for your infrastructure. All Terraform commands
16:00:23.657 INFO   terraform: should now work.
16:00:23.657 INFO   terraform: If you ever set or change modules or backend configuration for Terraform,
16:00:23.657 INFO   terraform: rerun this command to reinitialize your working directory. If you forget, other
16:00:23.657 INFO   terraform: commands will detect it and remind you to do so if necessary.
16:00:23.661 INFO   Executing hook: python_script
usage: my_script.py [-h] -a ARG1 -b ARG2 -c ARG3
my_script.py: error: the following arguments are required: -a/--arg1, -b/--arg2, -c/--arg3
16:00:24.059 ERROR  Error running hook python_script with message: Failed to execute "./my_script.py --arg1 argument1 --arg2 argument2 --arg3 argument3" in ./.terragrunt-cache/MRg6BF8iUt6goQDYcu5OoqH_p3k/HfCJfD21cilNj44BjpZsrrU6T90/some-module
usage: my_script.py [-h] -a ARG1 -b ARG2 -c ARG3
my_script.py: error: the following arguments are required: -a/--arg1, -b/--arg2, -c/--arg3

exit status 2
16:00:24.059 ERROR  Errors encountered running before_hooks. Not running 'terraform'.
16:00:24.060 ERROR  error occurred:

* Failed to execute "./my_script.py --arg1 argument1 --arg2 argument2 --arg3 argument3" in ./.terragrunt-cache/MRg6BF8iUt6goQDYcu5OoqH_p3k/HfCJfD21cilNj44BjpZsrrU6T90/some-module
  usage: my_script.py [-h] -a ARG1 -b ARG2 -c ARG3
  my_script.py: error: the following arguments are required: -a/--arg1, -b/--arg2, -c/--arg3
  
  exit status 2

Device:deployment username$ cd ./.terragrunt-cache/MRg6BF8iUt6goQDYcu5OoqH_p3k/HfCJfD21cilNj44BjpZsrrU6T90/some-module
Device:some-module username$ ./my_script.py --arg1 argument1 --arg2 argument2 --arg3 argument3
Namespace(arg1='argument1', arg2='argument2', arg3='argument3')

Versions

  • Terragrunt version: v0.72.5
  • Terraform version: v1.10.5 on darwin_arm64
  • Environment details (Ubuntu 20.04, Windows 10, etc.): macOS 15.1.1 (24B91)

Additional context

Add any other context about the problem here.

@kstevensonnv kstevensonnv added the bug Something isn't working label Jan 28, 2025
@yhakbar
Copy link
Collaborator

yhakbar commented Jan 28, 2025

Hey @kstevensonnv ,

Try doing this instead:

terraform {
  source = "../modules//some-module"

  before_hook "python_script" {
    commands     = ["plan", "apply"]
    execute      = [
      "./my_script.py",
      "--arg1", "argument1",
      "--arg2", "argument2",
      "--arg3", "argument3",
    ]
  }
}

You're not splitting up the arguments for your script correctly in your snippet.

@kstevensonnv
Copy link
Author

Thanks for the quick response and correcting the hook @yhakbar.

16:32:52.374 INFO   Executing hook: python_script
Namespace(arg1='argument1', arg2='argument2', arg3='argument3')

Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants