Skip to content

Instantly share code, notes, and snippets.

@Warns
Created November 5, 2021 12:55
Show Gist options
  • Save Warns/054fa0d71e0a4dd4fb5540f4fc5ee9a4 to your computer and use it in GitHub Desktop.
Save Warns/054fa0d71e0a4dd4fb5540f4fc5ee9a4 to your computer and use it in GitHub Desktop.
## tf/mv-state -- Moves a given resource from one root module to another via `terraform state mv`.
## Example:
## DEST_DIR=monitor \
## DEST_WORKSPACE=dev \
## SRC_DIR=k8s \
## SRC_WORKSPACE=dev \
## RESOURCE=module.datadog_monitors \
## make tf/mv-state;
tf/mv-state:
cd ./components/terraform/$(DEST_DIR); \
[[ "$(DEST_WORKSPACE)" != "default" ]] && terraform workspace select $(DEST_WORKSPACE); \
terraform state pull > $(DEST_WORKSPACE)-`date +"%Y-%m-%d"`.tfstate; \
cd ../$(SRC_DIR); \
[[ "$(SRC_WORKSPACE)" != "default" ]] && terraform workspace select $(SRC_WORKSPACE); \
terraform state mv -state-out=../$(DEST_DIR)/$(DEST_WORKSPACE)-`date +"%Y-%m-%d"`.tfstate $(RESOURCE) $(RESOURCE); \
cd ../$(DEST_DIR); \
terraform state push $(DEST_WORKSPACE)-`date +"%Y-%m-%d"`.tfstate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment