liveBook · Manning

Static analysis

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

Static analysis is the examination of source code or configuration without executing or deploying it to detect errors, inconsistencies, or violations of specified standards.

Static analysis examines plaintext infrastructure configuration or state before deployment, usually by parsing files rather than creating live resources. It detects incorrect values, inconsistent naming, dependency violations, and other configuration concerns without risking active systems. section 6.1.1

Apply static analysis as an early checkpoint in the infrastructure testing cycle:

  • Define or modify the infrastructure configuration. section 6.1
  • Parse the configuration, state, or—when necessary—a generated dry-run result. section 6.2 section 6.2.2
  • Check expected names, values, counts, formats, dependencies, and module contracts. section 6.1.1 section 6.3
  • Fix the configuration and rerun the tests; if correction is not practical, revert to a previously successful configuration. section 6.1.1
  • Apply changes only after the static checks pass, then use dynamic tests to verify the active infrastructure. section 6.1

Unit tests isolate the smallest configuration subset and avoid live resources or dependencies. Contract tests extend the practice across modules by checking that outputs provide the expected values or formats to dependent inputs. Configuration files are preferred because dry runs may require provider API access, take longer, or expose sensitive identifiers. section 6.2 section 6.2.2 section 6.3

Testing a network configuration

A factory module defines a network named hello-world-network and three subnets dividing 10.0.0.0/16. Tests inspect the configuration without creating resources, checking the network name, subnet count, and subnet ranges. A pytest fixture can load network.tf.json; alternatively, Terraform can generate a plan and convert it to JSON with `terraform plan -out=dry_run` followed by `terraform show -json dry_run > dry_run.json`. A contract test can then verify that the network exposes a facade containing its name and CIDR range for a server module. section 6.2 section 6.2.2 section 6.3

Figure 6.2 Infrastructure testing indicates whether you can apply changes to a system. After applying changes, you can use additional tests to confirm that the changes succeeded.

Use the infrastructure testing cycle to place static checks before deployment and dynamic checks after deployment. section 6.1

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

How deep it goes
Explains parsing, isolated unit tests, dry-run handling, contracts, correction, and rollback, but defers post-deployment verification to dynamic testing.
Examples
Demonstrates network names, subnet ranges, Terraform plans, pytest fixtures, and module facades connecting network outputs to server inputs.
What it assumes
Assumes familiarity with infrastructure configuration and deployment while teaching test isolation, expected values, dry runs, and rollback from operational examples.
What you can do after
Diagnose configuration errors, enforce standards, verify module contracts, and decide whether infrastructure changes are safe to apply.
How it connects
Connects infrastructure as code with unit testing, contract testing, dry runs, linting, formatting, and dynamic testing.

Also explained in

Compare how 6 books explain static analysis →

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.