Step 10: Update the Cookbook to Run a Script
Important
AWS OpsWorks Stacks is no longer accepting new customers. Existing customers will be able to use the OpsWorks console, API, CLI, and CloudFormation resources as normal until May 26, 2024, at which time they will be discontinued. To prepare for this transition, we recommend you transition your stacks to AWS Systems Manager as soon as possible. For more information, see AWS OpsWorks Stacks End of Life FAQs and Migrating your AWS OpsWorks Stacks applications to AWS Systems Manager Application Manager.
Update your cookbook by adding a recipe that runs a script on the instance. This recipe creates a directory and then creates a file in that directory. Writing a recipe to run a script that contains multiple commands is easier than running those commands one at a time.
To update the cookbook on the instance and run the new recipe
-
On your local workstation, in the
recipes
subdirectory in theopsworks_cookbook_demo
directory, create a file namedrun_script.rb
with the following code. For more information, go to script. script "Run a script" do interpreter "bash" code <<-EOH mkdir -m 777 /tmp/run-script-demo touch /tmp/run-script-demo/helloworld.txt echo "Hello, World!" > /tmp/run-script-demo/helloworld.txt EOH end
-
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::run_script
.
To test the recipe
-
Log in to the instance, if you have not done so already.
-
From the command prompt, run the following command to confirm that the new file was added:
sudo cat /tmp/run-script-demo/helloworld.txt
The file's contents are displayed:
Hello, World!
In the next step, you will update the cookbook to manage a service on the instance.