Step 13: Update the Cookbook to Use Data Bags - AWS OpsWorks

Step 13: Update the Cookbook to Use Data Bags

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 references stack settings that AWS OpsWorks Stacks stores on the instance in a set of data bags. This recipe displays messages in the log about specific stack settings that are stored on the instance. For more information, see the AWS OpsWorks Stacks Data Bag Reference.

To update the cookbook on the instance and to run the new recipe
  1. On your local workstation, in the recipes subdirectory in the opsworks_cookbook_demo directory, create a file named data_bags.rb that contains the following code:

    instance = search("aws_opsworks_instance").first layer = search("aws_opsworks_layer").first stack = search("aws_opsworks_stack").first Chef::Log.info("********** This instance's instance ID is '#{instance['instance_id']}' **********") Chef::Log.info("********** This instance's public IP address is '#{instance['public_ip']}' **********") Chef::Log.info("********** This instance belongs to the layer '#{layer['name']}' **********") Chef::Log.info("********** This instance belongs to the stack '#{stack['name']}' **********") Chef::Log.info("********** This stack gets its cookbooks from '#{stack['custom_cookbooks_source']['url']}' **********")

    This recipe displays messages in the log about specific stack settings that are stored on the instance.

  2. At the terminal or command prompt, use the tar command create a new version of the opsworks_cookbook_demo.tar.gz file, which contains the opsworks_cookbook_demo directory and its updated contents.

  3. Upload the updated opsworks_cookbook_demo.tar.gz file to your S3 bucket.

  4. 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::data_bags.

To test the recipe
  1. With the Running command execute_recipes page displayed from the previous procedure, for cookbooks-demo1, for Log, choose show. The execute_recipes log page is displayed.

  2. Scroll down through the log and find entries that look similar to the following:

    [2015-11-14T14:39:06+00:00] INFO: ********** This instance's instance ID is 'f80fa119-81ab-4c3c-883d-6028e52c89EX' ********** [2015-11-14T14:39:06+00:00] INFO: ********** This instance's public IP address is '192.0.2.0' ********** [2015-11-14T14:39:06+00:00] INFO: ********** This instance belongs to the layer 'MyCookbooksDemoLayer' ********** [2015-11-14T14:39:06+00:00] INFO: ********** This instance belongs to the stack 'MyCookbooksDemoStack' ********** [2015-11-14T14:39:06+00:00] INFO: ********** This stack gets its cookbooks from 'https://s3.amazonaws.com/opsworks-demo-bucket/opsworks_cookbook_demo.tar.gz' **********

    This recipe displays messages about specific stack settings that are stored on the instance.

In the next step, you will update the cookbook to run recipe code multiple times.