liveBook · Manning

Refactoring

As explained in Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform

Refactoring is the disciplined restructuring of an implementation without changing its externally observable behavior.

Refactoring is the disciplined restructuring of configuration or code without changing existing infrastructure resources. In infrastructure as code (IaC), it reduces technical debt, limits change conflicts and blast radius, shortens feedback cycles, and helps adopt standardized infrastructure modules. section 10.0 section 10.1

Use a gradual workflow that preserves live resource state:

  • Identify high-level resources with no dependencies and group them into a separate configuration, directory, or repository. section 10.2 section 10.2.3
  • Copy the configuration, initialize separate state, and import the existing resources rather than recreating them. section 10.2.1
  • Run a dry run or plan, deploy, and verify that the provider reports no unintended changes. section 10.2.1
  • Remove the resources from the monolith’s state and configuration only after the separated configuration manages them successfully. section 10.2.1
  • Repeat from higher-level dependent resources toward lower-level resources, using dependency injection to retrieve required identifiers. section 10.2.3 section 10.3

Extracting IAM from a monolithic GCP configuration

A configuration containing IAM roles and service accounts is copied into a separate directory while the live GCP resources remain unchanged. The resources are imported into the new state, Terraform plan reports no changes, and the IAM resources are then removed from the monolith’s state and code. The monolith can be applied again with zero resources added, changed, or destroyed. section 10.2.1

Figure 10.11 The workflow for refactoring an IaC monolith starts by identifying high-level resources with no dependencies.

Use the workflow figure to inspect the dependency-first order for breaking down an IaC monolith. section 10.2.3

How Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform covers it

How deep it goes
Explains definitions, technical debt, dependency ordering, state imports, verification, cleanup, and failure containment without detailing every provider operation.
Examples
Uses GCP IAM extraction, separate directories and state, Terraform plans, and dependency-ordered project, network, database, server, load balancer, and DNS configurations.
What it assumes
Assumes familiarity with IaC resources, configuration state, dependencies, infrastructure providers, and Terraform-style planning and state operations.
What you can do after
Apply refactoring via Terraform by copying configurations, importing resources, validating plans, removing old state, and repeating by dependency order.
How it connects
Connects refactoring with technical debt, monolithic architecture, dependency injection, idempotency, feature flags, and rolling updates.

Also explained in

Compare how 22 books explain refactoring →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform. The section links point to the parts of the book it draws on.