Infrastructure as code decisions
What infrastructure as code solves
Infrastructure as code (IaC) describes infrastructure in versioned files so changes can be reviewed and reproduced. Instead of asking an administrator to remember every step for a bookstore network, two application servers and a private database, a team records the intended resources, their relationships and selected settings. An engine works out operations needed to reach that intent. It is not a promise of identical results in every region, nor a continuous reconciliation loop unless another system triggers it.
Declarative means describing the outcome rather than listing every API call in order. Imperative automation specifies steps. Both can coexist: a declarative network resource may be defined by an imperative program that generates a template. A script that runs the same command twice is not necessarily safe twice.
Provisioning versus configuration management
Provisioning establishes resources such as virtual networks, disks, VMs and managed databases. Configuration management sets up operating systems and applications, for example ensuring a service is installed, configured and running. Image building prepares a reusable artifact before deployment. These boundaries overlap, so assign exactly one owner for each setting. A provisioning tool and configuration-management tool fighting over the same setting create drift.
Example: the bookstore needs two private VMs, an internal service identity and a web server on each VM. IaC owns the network, VM and identity lifecycle; a configuration role owns the web server package and configuration. An image pipeline can instead build the package into an immutable image. The appropriate choice depends on patching, rollback, boot time and operational ownership.
Idempotence means repeating an operation against the same intended state should not cause additional changes to the managed outcome. A module that ensures a package is present can be idempotent; an arbitrary script appending a line every run is not. Ansible modules often implement idempotence, but not every task does. Check mode is a prediction with module-specific limits, not a guarantee of harmless execution.
Read a dependency graph before provisioning
A dependency is a relationship that must be satisfied before another resource can be created or used. Consider this offline, non-deployable JSON inventory. The dependsOn lists are teaching metadata, not Terraform, ARM or CloudFormation syntax.
{ "network": [], "subnet": [ "network" ], "identity": [], "app": [ "subnet", "identity" ], "database": [ "subnet" ]}A valid creation order is network, subnet, identity, app, database. Identity could be created earlier, and app and database could be independent in this graph. If the application needs a database endpoint at creation time, add database to its dependencies. A cycle such as network depending on app while app depends on network has no valid topological order. Destruction may follow reversed dependency order, but retention policies and actual service dependencies need review. Creation order alone does not establish application readiness.
Compare the provider toolchains
Terraform and OpenTofu use configuration, provider plugins and resource state to manage infrastructure. Their language and provider ecosystem overlap, but release features, licensing, state compatibility and encryption options must be checked separately. Modules encapsulate reusable resources and expose inputs and outputs; pin module and provider versions and review upgrades. Do not equate portability of the language with portability of a provider resource schema.
AWS CloudFormation interprets templates and manages stacks. AWS CDK runs a program to generate CloudFormation templates and deployment artifacts; CDK is not a separate general-purpose cloud provisioning API. Review the generated template and change set. Azure Bicep is a declarative language for Azure Resource Manager (ARM); its what-if operation previews intended changes. ARM incremental deployment is not equivalent to deleting all resources omitted from a template; deletion requires a deliberate lifecycle design. Google Cloud Infrastructure Manager operates Terraform configurations in a managed service and supports deployment previews. Google Cloud Deployment Manager support ended April 1, 2026; treat it as historical migration material, not a new-project baseline.
For guest configuration across these platforms, Ansible is a candidate. Select tools according to service support, skills, operational ownership, review workflow and existing state; a shared syntax does not erase provider-specific permissions or network and storage differences.
Desired state, observed state and recorded state
Desired state is the configuration you intend to run. Observed state is what actually exists, as far as the tool can determine. Recorded state maps the tool's resource addresses to managed objects and relevant attributes. A team that manually changes a VM size creates drift from the intended configuration. First find out whether the change was emergency mitigation, an accidental edit or an approved requirement; then decide whether to update the configuration or restore the intended setting.
Treat state, plan artifacts, backups and logs as potentially sensitive. Restrict access, encrypt storage, use supported locking and backup/versioning, separate environments and verify recovery. Terraform's sensitive display annotation does not generally remove values from saved state and plans. OpenTofu offers version-dependent state/plan encryption, which still requires key management and migration planning. Never share raw plans or state as lesson artifacts. Avoid managing the same resource from two independent state owners.
Practice and next steps
Choose a toolchain for the bookstore's existing Azure environment, where the team knows Bicep and uses an established configuration-management role. Bicep is a reasonable candidate for provisioning, and the role can manage guest settings. If the team later adopts Terraform across providers, evaluate migration and ownership rather than starting a competing state file. Review Chapter 9 for identity, Chapter 13 for virtual compute and Chapter 16 for virtual networks. The next lesson explains how to reject a dangerous change before deployment.
Source anchors: Terraform plan and sensitive data, OpenTofu state and encryption, AWS CloudFormation change sets and CDK synthesis, Azure Bicep overview and deployment modes, Google Cloud Infrastructure Manager and Deployment Manager deprecation, Ansible playbooks and check mode. Exact links and review scope are in the package source register (v30-*).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Infrastructure as code decisions — knowledge check
Choose the best answer for each offline scenario.
Which tool responsibility best matches installing and configuring a web server inside an existing VM?
What does a dependency cycle mean in the teaching graph?
What does AWS CDK synthesis produce?
Which Google Cloud service is the current Terraform-based managed option in the source review?
Why is marking a Terraform value sensitive insufficient to secure saved plans?