claranet / jinjaform

Terraform wrapper with Jinja2 templates
MIT License
35 stars 8 forks source link

State moving does not happen in original tfstate #17

Closed Dyllaann closed 5 years ago

Dyllaann commented 5 years ago

Hi!

Found another problem. I have to move some resources because of provider changes. Using: jinjaform state mv azurerm_azuread_service_principal.servicePrincipal azuread_service_principal.servicePrincipal, I only see the reflected changes on the terraform.tfstate that ends up in .jinjaform/.

Which means, because I have to change three resources, the previous move gets lost as the next move will overwrite the state that is in .jinjaform/ resulting in no moving at all in the end.

I hope I explained it enough, if not; please let me know :)

raymondbutcher commented 5 years ago

Hi.

You know what, I have not actually used Jinjaform with a local state file, and totally missed this use case.

I think for all state operations (apply, import, state mv/rm, destroy) this will happen:

  1. Jinjaform copies terraform.tfstate (if it exists) into the .jinjaform workspace
  2. Terraform runs inside the .jinjaform workspace using .jinjaform/terraform.tfstate which does get updated when there are state changes
  3. The original terraform.tfstate from outside of the workspace is never updated

In short, I don't think local state works at all in Jinjaform right now!

As a hack, updating your .jinjaformrc file to add this last line might work:

GIT_CHECK_BRANCH master
GIT_CHECK_CLEAN
GIT_CHECK_REMOTE
WORKSPACE_CREATE
TERRAFORM_RUN
RUN cp "${JINJAFORM_WORKSPACE}/terraform.tfstate" "${JINJAFORM_WORKSPACE}/../terraform.tfstate"

Please back up your state file before playing around with this!

Dyllaann commented 5 years ago

Good point! Didn't even think about the apply not working then, as of the same reason.

I added the hack to my .jinjaformrc and will play around a bit. Will report how it behaves!

Dyllaann commented 5 years ago

Works fine!