Sobes.tech
Middle

What is the difference between declarative and imperative infrastructure description?

sobes.tech AI

Answer from AI

Declarative description focuses on the desired end state of the system – what should be. The tool itself determines the steps to achieve this state.

Imperative description focuses on the sequence of steps – how to reach the desired state. You explicitly specify which commands to execute and in what order.

Examples:

  • Declarative (Terraform, CloudFormation): Define that a VPC with specific CIDR, subnets, routing tables, and security groups is needed.
  • Imperative (Bash scripts, Ansible ad-hoc commands): Execute a command to create a VPC, then a command to create a subnet, then a command to attach the subnet to the VPC.

Main differences summarized in the table:

Aspect Declarative description Imperative description
Focus What should be How to achieve it
Change The tool determines the steps You define the steps
Idempotency Often built-in Requires manual implementation
Complexity Higher when defining the state Higher when managing complex processes
Readability Describes the desired architecture Describes the sequence of actions

Example configuration (simplified):

// Declarative (Terraform)
resource "aws_instance" "web_server" {
  ami           = "ami-0abcdef1234567890"
  instance_type = "t2.micro"

  tags = {
    Name = "WebServer"
  }
}
# Imperative (Bash)
#!/bin/bash
# Create an instance manually (pseudo-code)
aws ec2 run-instances --image-id ami-0abcdef1234567890 --instance-type t2.micro --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer}]'