Step 8: Update the Cookbook to Create and Copy Files
Update your cookbook by adding a recipe that adds two files to the instance. The first resource in the recipe creates a file completely with recipe code. This is similar to running the Linux cat, echo, or touch commands or the Windows echo or fsutil commands. This technique is useful for a few, small, or simple files. The second resource in the recipe copies a file in the cookbook to another directory on the instance. This is similar to running the Linux cp command or the Windows copy command.This technique is useful for many, large, or complex files.
Before you start this step, complete Step 7: Update the Cookbook to Create a Directory to make sure that the files' parent directory already exists.
To update the cookbook on the instance and run the new recipe
On your local workstation, in the
opsworks_cookbook_demodirectory, create a subdirectory named
filessubdirectory, create a file named
hello.txtwith the following text:
file "Create a file" do content "<html>This is a placeholder for the home page.</html>" group "root" mode "0755" owner "ec2-user" path "/tmp/create-directory-demo/index.html" end cookbook_file "Copy a file" do group "root" mode "0755" owner "ec2-user" path "/tmp/create-directory-demo/hello.txt" source "hello.txt" end
fileresource creates a file in the specified path. The
cookbook_fileresource copies the file from the
filesdirectory that you just created in the cookbook (Chef expects to find a standard-named subdirectory named
filesthat it can copy files from) to another directory on the instance.
At the terminal or command prompt, use the tar command create a new version of the
opsworks_cookbook_demo.tar.gzfile, which contains the
opsworks_cookbook_demodirectory and its updated contents.
Upload the updated
opsworks_cookbook_demo.tar.gzfile to your S3 bucket.
Follow the procedures in Step 5: Update the Cookbook on the Instance and Run the Recipe to update the cookbook on the instance and to run the recipe. In the "To run the recipe" procedure, for Recipes to execute, type
To test the recipe
Log in to the instance, if you have not done so already.
From the command prompt, run the following commands, one at a time, to confirm that the new files were added:Copy
sudo cat /tmp/create-directory-demo/index.html sudo cat /tmp/create-directory-demo/hello.txt
The files' contents are displayed:Copy
<html>This is a placeholder for the home page.</html> Hello, World!
In the next step, you will update the cookbook to run a command on the instance.