Step 8: Update the Cookbook to Create and Copy Files
Important
The AWS OpsWorks Stacks service reached end of life on May 26, 2024 and has been disabled for both new and existing customers.
We strongly recommend customers migrate their workloads to other solutions as soon as possible. If you have questions about migration, reach out to the AWS Support Team on AWS re:Post
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_demo
directory, create a subdirectory namedfiles
. -
In the
files
subdirectory, create a file namedhello.txt
with the following text:Hello, World!
-
In the
recipes
subdirectory in theopsworks_cookbook_demo
directory, create a file namedcreate_files.rb
with the following code. For more information, go to fileand cookbook_file . 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
The
file
resource creates a file in the specified path. Thecookbook_file
resource copies the file from thefiles
directory that you just created in the cookbook (Chef expects to find a standard-named subdirectory namedfiles
that 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.gz
file, which contains theopsworks_cookbook_demo
directory and its updated contents. -
Upload the updated
opsworks_cookbook_demo.tar.gz
file 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
opsworks_cookbook_demo::create_files
.
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:
sudo cat /tmp/create-directory-demo/index.html sudo cat /tmp/create-directory-demo/hello.txt
The files' contents are displayed:
<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.