Chef

Also known as: Chef Infra

On this page 7

Chef, also known as Chef Infra, is a configuration management platform in which administrators describe the desired state of servers in Ruby-based code, and an agent on each node repeatedly converges the machine toward that state. Within infrastructure as code, Chef belongs to the family of tools that configure the inside of machines (packages, files, services, users) rather than provisioning the cloud resources themselves.

In cloud operations, Chef is typically paired with a provisioning tool such as Terraform or AWS CloudFormation: the provisioning tool creates the virtual machines, and Chef makes every machine look exactly as its policy requires, then keeps it that way to counter configuration drift.

How Chef converges a node

Chef works through a pull-based agent model. A program called Chef Infra Client runs on each managed machine, called a node, either as a daemon, a scheduled job or a one-off command. On each run it identifies the node, downloads the relevant policy from Chef Infra Server, compiles the recipes into a resource collection and then applies each resource in order.

Every resource is idempotent by design: before acting, the client tests the current state and changes the system only when it differs from the declared state. A package resource installs software only if it is missing, and a template resource rewrites a file only when the rendered content differs. This test-and-repair loop is what makes repeated runs safe and what lets Chef correct drift automatically.

A Chef run proceeds in two distinct phases, which is the source of many subtle bugs. The following table summarizes the stages of a typical client run.

StageWhat happensPractical consequence
Node identificationClient authenticates with its key and loads node dataEach node needs a unique name and key pair
Ohai data collectionPlatform, memory, network and cloud facts are gatheredRecipes can branch on real hardware and OS facts
Policy synchronizationCookbooks and attributes are downloadedLarge cookbooks slow down every run
Compile phaseRuby code is evaluated and resources are queuedPlain Ruby runs before any resource executes
Converge phaseQueued resources test and repair system stateChanges and notifications happen here
ReportingUpdated resources and failures are sent to the serverFeeds compliance and audit dashboards
Stages of a Chef Infra Client run

Core building blocks of Chef code

Chef code is organized into cookbooks, which are versioned packages of recipes, templates, files, attributes and tests. A recipe is a Ruby file containing an ordered list of resources, and the vocabulary of resources forms a domain-specific language that reads almost like a checklist of desired outcomes.

ArtifactPurposeTypical example
ResourceDeclares one piece of desired stateA service that must be enabled and running
RecipeOrdered collection of resourcesInstall and configure a web server
CookbookVersioned unit of distribution and testingA database cookbook at version 4.2.1
AttributeTunable value with precedence levelsListening port or memory limit
Custom resourceReusable abstraction written by the teamAn internal application deployment resource
PolicyfileLocks the exact cookbook set for a group of nodesProduction web tier policy
Data bagGlobal JSON data, optionally encryptedUser accounts or service credentials
Main Chef artifacts and their roles

Attributes are resolved through a precedence hierarchy with levels such as default, normal, override and automatic, applied from cookbooks, roles, environments and Ohai. This flexibility is powerful but is also the most common reason a value on a node is not what an engineer expects. Modern practice favors Policyfiles over roles and environments because a Policyfile pins every cookbook version in a single lock file, which makes promotion from staging to production reproducible and auditable.

Chef compared with other infrastructure as code tools

Chef is best compared with the other configuration management tools, Puppet and Ansible, and contrasted with provisioning tools that manage cloud APIs. The decisive differences are the language, the execution model and whether an agent is required.

ToolLanguage and styleExecution modelMain focus
ChefRuby DSL, ordered resourcesAgent pulls from server, or local modeConfiguring server internals
PuppetOwn declarative language, dependency graphAgent pulls from serverConfiguring server internals
AnsibleYAML playbooksAgentless push over SSHConfiguration and ad hoc orchestration
TerraformHCL, declarativeCLI calls provider APIs, keeps stateProvisioning cloud resources
AWS CloudFormationJSON or YAML templatesManaged service applies stacksProvisioning AWS resources
Chef and neighboring tools

Chef evaluates resources in the order written, while Puppet builds a dependency graph and orders resources from explicit relationships. Engineers with a software development background often find Chef’s full programming language natural, while teams that prefer minimal code tend to choose Ansible. Unlike Terraform, Chef keeps no central state file describing the world; the node itself is the source of truth that each run inspects.

Running Chef in cloud environments

In the cloud, Chef is used in two operating patterns. In the classic pattern, long-lived virtual machines run Chef Infra Client every 15 to 60 minutes and are continuously corrected. In the image-baking pattern, Chef runs once in local mode while a tool builds a machine image, and the resulting image is deployed as immutable infrastructure.

SituationRecommended patternReason
Long-lived servers with frequent changesServer mode with scheduled runsContinuous drift correction
Autoscaling groups and short-lived instancesBake images with Chef in local modeFast boot without a first long converge
Small team, few nodesChef Infra Client in local modeNo server to operate
Regulated environmentsServer mode plus compliance scanningRun reports provide audit evidence
Choosing a Chef deployment pattern

Chef cookbooks are tested with Test Kitchen, which converges a cookbook on a temporary virtual machine or container, and with InSpec or ChefSpec for verification. A mature pipeline lints, unit-tests and integration-tests every cookbook before a Policyfile is promoted, which treats server configuration with the same release discipline as application code.

Limitations and common mistakes

Chef has a steeper learning curve than agentless tools, because effective use requires Ruby, the attribute precedence model and the compile and converge distinction. The most frequent problems fall into a few recognizable patterns.

  • Ruby logic in the compile phase reads system state before earlier resources have changed it, so conditions evaluate against stale data; guards such as only_if or lazy evaluation solve this.
  • Non-idempotent execute resources run shell commands on every converge; each one needs a guard or a creates property.
  • Unpinned cookbook versions let a new upstream release change production unexpectedly; Policyfiles prevent this.
  • Manual changes on nodes are silently reverted at the next run, which surprises operators who did not know the node is managed.
  • Secrets in plain attributes end up in node objects visible to many users; encrypted data bags or an external secrets manager are safer.

Chef also adds operational overhead: the Chef Infra Server must be backed up, upgraded and secured, and client keys must be managed for every node. For container-based platforms, where images are rebuilt rather than modified, configuration management agents are increasingly replaced by image builds, so Chef fits best where long-lived machines remain.

In plain words

Chef is like a detailed instruction sheet that every server keeps reading. The sheet says which programs must be installed, which settings must be in place and which services must be running. A small helper on each server checks the sheet regularly and fixes anything that does not match.

If someone changes a setting by hand, the helper notices on its next check and puts it back. That way hundreds of servers stay identical and predictable, and the instructions themselves can be reviewed and tested like any other code.

A real-world example

An online retailer runs about 400 Linux virtual machines in a public cloud for its web tier, payment services and internal tools. Before adopting Chef, each server had been configured by hand, and audits regularly found different TLS settings and outdated packages across machines.

The operations team writes cookbooks for the web server, logging agent and security baseline, locks them in a Policyfile per tier and schedules Chef Infra Client to run every 30 minutes. When a new TLS policy is required, an engineer changes one attribute, tests it with Test Kitchen and promotes the Policyfile from staging to production. Within an hour all 400 servers report the new configuration, and the run reports serve as audit evidence that no machine has drifted from the approved baseline.