Creating a Cookbook and Recipe - Chef Automate on AWS

Creating a Cookbook and Recipe

Now that the Chef workstation is fully configured, you’re ready to create a cookbook.

  1. In the ChefDK console, create a cookbook named webserver for a web server:

    knife cookbook create webserver
  2. To set up the cookbook, you can use sample files that are included with this Quick Start. Copy the provided recipe into your new cookbook:

    $source = 'c:\cfn\downloads\default.rb' $dest = 'c:\chef-repo\cookbooks\webserver\recipes' Copy-Item -Path $source -Destination $dest
  3. Copy a basic default web page into your webserver cookbook. This file will be used to show a "hello world" message to visitors who navigate to the web server after you’ve configured your node.

    $source = 'c:\cfn\downloads\index.html' $dest = 'c:\chef-repo\cookbooks\webserver\files' Copy-Item -Path $source -Destination $dest
  4. Open the recipe file in a text editor such as Notepad++ to review the code. The recipe defines a series of resources that will execute in order to configure your node as a web server.

                        The webserver recipe

    Figure 13: The webserver recipe

    Note the following about the code listed in Figure 13:

    • Line 12 – The execute resource runs a single command—in this case, the apt-get update command. This command downloads package lists and updates them with the newest versions of packages and their dependencies.

    • Line 18 – The package resource uses the appropriate package manager on the node to take the specified action on the package. In this case, we’re installing the Apache 2.0 web server.

    • Line 24 – The service resource starts the Apache service after it’s installed, and then enables the service for automatic startup. The Linux distribution launched by this Quick Start already handles the automatic startup, but it’s provided here for completeness, so you can use it as the basis for another server if necessary.

    • Line 28 – The cookbook_file resource uses the index.html file you copied into the files folder of your webserver cookbook, and deploys it to the web server.

    This is a very simple recipe that uses some of the common Chef resources. For more information about developing cookbooks, see the Additional Resources section.

  5. Now that the cookbook is fully configured, upload it to Chef Automate:

    knife cookbook upload webserver