aztfmod / rover

The rover is a docker container in charge of the deployment of the Terraform platform engineering for Azure
MIT License
173 stars 144 forks source link

Refactor rover apply to reuse an existing terraform plan #165

Closed LaurentLesle closed 2 years ago

LaurentLesle commented 3 years ago

At the moment the rover apply is running a terraform plan and apply behind the scene. This issue fix that and propose the following:

The scenario we want to cover are

Commands to separate plan from apply and destroy.

Plan for an apply

# Rover plan for an apply
rover \
  -lz /tf/caf/landingzones/caf_launchpad \
  -var-folder /tf/caf/configuration/contoso/platform/demo/level0/launchpad \
  -tfstate_subscription_id ${tfstate_subscription_id} \
  -target_subscription ${target_subscription} \
  -tfstate caf_launchpad.tfstate \
  -log-severity ERROR \
  -launchpad \
  -env it-sre \
  -level level0 \
  -p ${TF_DATA_DIR}/tf_plan_launchpad.tfplan \
  -a plan
# Rover apply based on the previous plan
rover \
  -lz /tf/caf/landingzones/caf_launchpad \
  -var-folder /tf/caf/configuration/contoso/platform/demo/level0/launchpad \
  -tfstate_subscription_id ${tfstate_subscription_id} \
  -target_subscription ${target_subscription} \
  -tfstate caf_launchpad.tfstate \
  -log-severity ERROR \
  -launchpad \
  -env it-sre \
  -level level0 \
  -p ${TF_DATA_DIR}/tf_plan_launchpad.tfplan \
  -a apply

Plan for a destroy

# Rover plan for a destroy
rover \
  -lz /tf/caf/landingzones/caf_launchpad \
  -var-folder /tf/caf/configuration/contoso/platform/demo/level0/launchpad \
  -tfstate_subscription_id ${tfstate_subscription_id} \
  -target_subscription ${target_subscription} \
  -tfstate caf_launchpad.tfstate \
  -log-severity ERROR \
  -launchpad \
  -env it-sre \
  -level level0 \
  -p ${TF_DATA_DIR}/tf_plan_launchpad.tfplan \
  -a plan -destroy
# Rover destroy based on the previous plan. Note -var-folder, -var-file or -var must be removed from the command line.
# As the destroy is now based on the plan, terraform is not prompting for the confirmation to destroy the resources
rover \
  -lz /tf/caf/landingzones/caf_launchpad \
  -tfstate_subscription_id ${tfstate_subscription_id} \
  -target_subscription ${target_subscription} \
  -tfstate caf_launchpad.tfstate \
  -log-severity ERROR \
  -launchpad \
  -env it-sre \
  -level level0 \
  -p ${TF_DATA_DIR}/tf_plan_launchpad.tfplan \
  -a destroy

Previous commands are still working

# Rover plan
rover \
  -lz /tf/caf/landingzones/caf_launchpad \
  -var-folder /tf/caf/configuration/contoso/platform/demo/level0/launchpad \
  -tfstate_subscription_id ${tfstate_subscription_id} \
  -target_subscription ${target_subscription} \
  -tfstate caf_launchpad.tfstate \
  -log-severity ERROR \
  -launchpad \
  -env it-sre \
  -level level0 \
  -a plan
# Rover apply
rover \
  -lz /tf/caf/landingzones/caf_launchpad \
  -var-folder /tf/caf/configuration/contoso/platform/demo/level0/launchpad \
  -tfstate_subscription_id ${tfstate_subscription_id} \
  -target_subscription ${target_subscription} \
  -tfstate caf_launchpad.tfstate \
  -log-severity ERROR \
  -launchpad \
  -env it-sre \
  -level level0 \
  -a apply
# Rover destroy
rover \
  -lz /tf/caf/landingzones/caf_launchpad \
  -var-folder /tf/caf/configuration/contoso/platform/demo/level0/launchpad \
  -tfstate_subscription_id ${tfstate_subscription_id} \
  -target_subscription ${target_subscription} \
  -tfstate caf_launchpad.tfstate \
  -log-severity ERROR \
  -launchpad \
  -env it-sre \
  -level level0 \
  -a destroy
LaurentLesle commented 2 years ago

rover aztfmod/rover-preview:1.0.7-2109.210406 has a preview version of this feature