minamijoyo / tfmigrate

A Terraform / OpenTofu state migration tool for GitOps
MIT License
1.14k stars 57 forks source link

tfmigrate reports migration plan success when Terraform plan command returns an error #140

Closed yardbirdsax closed 1 year ago

yardbirdsax commented 1 year ago

Thank you for all your work on tfmigrate, it has saved our bacon many times when performing state file surgery! 🙇‍♂️

We recently encountered a scenario where tfmigrate reported a successful plan with no changes, however after applying the migration we found that there were changes shown when we ran terraform plan manually in the new state's home. We traced this down to the fact that there were non default/auto .tfvars files that were required to be specified at plan time, and if these were not specified the plan command as run by tfmigrate returns an error. This is observed as well when debug logging is turned on.

2023/08/01 16:57:54 [DEBUG] [executor@/Users/joshua.feierman/repos/something]$ terraform plan -state=/var/folders/nz/p1nscz5j1_q4xwrb7kfpr3q80000gq/T/tmp3169562229 -out=/var/folders/nz/p1nscz5j1_q4xwrb7kfpr3q80000gq/T/tfplan3479111866 -input=false -no-color -detailed-exitcode
2023/08/01 16:57:54 [DEBUG] [executor@/Users/joshua.feierman/repos/something] failed to run command: (*exec.ExitError)(0x14000030dc0)(exit status 1)
2023/08/01 16:57:54 [INFO] [migrator@/Users/joshua.feierman/repos/something] check diffs
...
2023/08/01 16:58:17 [INFO] [migrator] multi state migrator plan success!

This doesn't seem like appropriate behavior, in that I would expect tfmigrate to throw an error if it cannot run the terraform plan command successfully. Granted, I'm not sure how we'd specify those .tfvars files be used, but that's more of a feature gap in my mind.

minamijoyo commented 1 year ago

Hi @yardbirdsax, Thank you for reporting this!

Which tfmigrate version do you use? I suspect a regression was introduced in tfmigrate v0.3.12 via #139. If you use the current latest v0.3.12, can you test in v0.3.11?

By the way, the -var-file argument has not been supported yet, but you can pass extra args to terraform plan command via environment variable TF_CLI_ARGS_plan="-var-file=somevars.tfvars". See #27.

minamijoyo commented 1 year ago

@yardbirdsax I was able to reproduce the bug and fixed it in v0.3.13.

jrab66 commented 4 months ago

awesome feature, I was able to perform state surgery also.

TF_CLI_ARGS_plan="-var-file=somevars.tfvars"