Declarative Infrastructure
Declarative infrastructure defines the desired end state of systems and lets an Infrastructure as Code (IaC) tool determine the operations needed to reach it. This separates the target configuration from the step-by-step procedure used to create it.
In declarative IaC, you describe what resources and configuration should exist rather than specifying every command required to create them. The IaC engine compares that desired configuration with the current infrastructure and determines what changes are necessary.
A typical workflow stores infrastructure configuration in version-controlled files. When the configuration changes, the IaC tool calculates a new plan, identifies resources to add, modify, or remove, and then applies those changes. This makes infrastructure changes more repeatable and reviewable.
Declarative systems generally depend on a notion of current state. The tool may obtain that state from the provider, from a state file, or from both, depending on the tool and configuration. The engine uses the difference between desired and observed state to determine actions.
For a Java Backend Engineer, the key idea is similar to declaring application configuration or desired object state: you specify the result and rely on the underlying system to perform the required operations. Common declarative IaC tools include Terraform and AWS CloudFormation.
Key points
- Describe the desired infrastructure state instead of a command sequence.
- The IaC engine calculates the changes required to converge on the desired state.
- Infrastructure definitions can be version controlled, reviewed, and reproduced.
- State or provider observations are used to understand the infrastructure's current condition.
- Changing the desired configuration can cause resources to be created, updated, or removed.
Interview questions
What happens when the desired configuration differs from the current infrastructure?
The IaC engine evaluates the difference between the desired configuration and its view of the current infrastructure. It generates a set of actions needed to reconcile that difference. Depending on the change, those actions might create, update, or delete resources. The exact behavior depends on the resource and provider.
What is declarative Infrastructure as Code?
Declarative IaC describes the desired state of infrastructure rather than giving a list of commands to execute. An IaC engine compares that target with the current state and determines the necessary operations. This makes infrastructure definitions easier to review and reproduce. Terraform and CloudFormation are examples of tools that support declarative infrastructure definitions.
Why is declarative IaC useful?
It lets teams describe infrastructure consistently in files that can be version controlled. The tool can calculate changes instead of requiring engineers to manually write every infrastructure operation. This supports repeatability, review, and automation. It can also help detect and correct differences between intended and actual infrastructure.
How does declarative IaC differ from an imperative deployment script?
An imperative script focuses on the sequence of commands needed to perform an operation. Declarative IaC instead describes the final configuration and lets the IaC engine derive the operations. This can make repeated application of the same configuration more predictable because the tool reasons about the target state. However, engineers still need to understand dependencies, provider behavior, and state management.
What is infrastructure drift in a declarative IaC system?
Drift occurs when the real infrastructure no longer matches the configuration that IaC intends to manage. For example, someone might change a resource directly through a cloud console. During a later planning or reconciliation operation, the IaC tool can detect differences between the expected and observed state, depending on the tool's state and refresh behavior. Teams should control out-of-band changes and regularly review plans to manage drift.