The Anatomy of Chef Cookbooks in Engine Yard



While the Recipes installed with the Chef run can be customized, which occurs during the preparation stage in v5 and later, the main Recipes applied each time are essentially the same. These are called from ey-init::main, which is the starting point of the Chef run.

In this article we will explain how cookbooks are applied in the core Chef run. Please note that the v7 stack will be used for this example.


The core Chef run

The process to execute a Chef run is detailed below, and follows through the steps outlined in the ey-init::main recipe:

  1. Chef reads the dna.json file and executes ey-init::main. The following steps are called from this file.
  2. ey-base::resin_gems provides the base gems required by Chef. While the AMI (Amazon Machine Image) deployed contains some Gems already, Gems deployed via Recipe are easier to change and maintain. 
  3. ey-core installs some other important requirements for the following steps.
  4. ey-custom::before-main are custom Recipes applied before the main Chef recipes run. This is not normally used for custom Recipes, and requires a higher level of knowledge, as it is prone to break things.
  5. ey-base::bootstrap includes the main Recipes, creates the required directories, config files, etc.
    • It uses dna.json data to fill in multiple variables, such as the app name, choice of apps, versions, etc.
    • The last line of the Recipe will execute ruby based on the contents of json.dna. This is because in v6 and v7 there are two Ruby versions; one for the application, and one for Chef. Running ruby -v will show the version of Ruby.
  6. The next line, node.engineyard.instance.roles.each { |role| include_recipe "ey-#{role}::prep" }, will install Recipes specific to the instance role (application, DB, or utility).
  7. ey-base::post_bootstrap will install additional Recipes after setting the required role Recipes.
  8. ey-custom::after-main is the custom Recipes added after the main Chef runs. This is the normal place for custom Recipes to be added.
  9. Finally, ey-base::custom will add extra Recipes based on the environment variables set in the system.

Cookbook syntax

To understand the Chef recipes, it is useful to know some of the commands used:

  • include_recipe “<recipe>”: Imports another Recipe. This will import default.rb form within the Recipe folder.
  • include_recipe “<recipe>::<file>”: Imports any other file than default.rb from the Recipe folder.
  • execute “<command>”: This will define and execute a command, unless the action is set to "nothing", in which case it will only be defined.
  • package “<app>”: Installs a certain package at the operating system level.

Note that dna.json is accessed by the Recipes to get information required to install them:

  • "node" is the object used by Chef to configure the Chef run, which Enzyme amends with dna.json.
  • It is navigated like any JSON object. The syntax used can be one of the following, or a combination of the two:
    • node.engineyard.environment
    • node[“engineyard”][“environment”]


Cookbooks in Engine Yard are applied in the order described by ey-init::main, and can be debugged accordingly by understanding the anatomy of the core Chef run.


Article is closed for comments.