Using Search on a Linux Stack - AWS OpsWorks

Using Search on a Linux Stack

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 or through AWS Premium Support.

This example is based on a Linux stack with a single PHP application server. It uses Chef search to obtain the server's public IP address and puts the address in a file in the /tmp directory. It retrieves essentially the same information from the node object as Obtaining Attribute Values Directly , but the code is much simpler and does not depend on the details of the stack configuration and deployment attribute structure.

The following briefly summarizes how to create the stack for this example. For more information, see Create a New Stack.

Note

If you have not run a custom recipe on an AWS OpsWorks Stacks instance before, you should first go through the Running a Recipe on a Linux Instance example.

Create a stack
  1. Open the AWS OpsWorks Stacks console and click Add Stack.

  2. Specify the following settings, accept the defaults for the other settings, and click Add Stack.

    • Name – SearchJSON

    • Default SSH key – An Amazon EC2 key pair

    If you need to create an Amazon EC2 key pair, see Amazon EC2 Key Pairs. Note that the key pair must belong to the same AWS region as the instance. The example uses the US West (Oregon) region.

  3. Click Add a layer and add a PHP App Server layer to the stack with default settings.

  4. Add a 24/7 instance with default settings to the layer and start it.

To set up the cookbook
  1. Create a directory within opsworks_cookbooks named searchjson and navigate to it.

  2. Create a metadata.rb file with the following content and save it to opstest.

    name "searchjson" version "0.1.0"
  3. Create a recipes directory within searchjson.

  4. Create a default.rb file with the following recipe and save it to the recipes directory.

    phpserver = search(:node, "layers:php-app").first Chef::Log.info("**********The public IP address is: '#{phpserver[:ip]}'**********") file "/tmp/ip_addresses" do content "#{phpserver[:ip]}" mode 0644 action :create end

    Linux stacks support only the node search index. The recipe uses this index to obtain a list of instances in the php-app layer. Because the layer is known to have only one instance, the recipe simply assigns the first one to phpserver. If the layer has multiple instances, you can enumerate them to retrieve the required information. Each list item is a hash table containing a set of instance attributes. The ip attribute is set to the instance's public IP address, so you can represent that address in the subsequent recipe code as phpserver[:ip].

    After adding a message to the Chef log, the recipe then uses a file resource to create a file named ip_addresses. The content attribute is set to a string representation of phpserver[:ip]. When Chef creates ip_addresses, it adds that string to the file.

  5. Create a .zip archive of opsworks_cookbooks, Upload the archive to an Amazon S3 bucket, make the archive public, and record the archive's URL. For more information on cookbook repositories, see Cookbook Repositories.

    Content delivered to Amazon S3 buckets might contain customer content. For more information about removing sensitive data, see How Do I Empty an S3 Bucket? or How Do I Delete an S3 Bucket?.

You can now install the cookbook and run the recipe.

To run the recipe
  1. Edit the stack to enable custom cookbooks, and specify the following settings.

    • Repository typeHttp Archive

    • Repository URL – The cookbook archive URL that you recorded earlier

    Use the default values for the other settings and click Save to update the stack configuration.

  2. Edit the custom layer configuration and assign searchjson::default to the layer's Setup event. AWS OpsWorks Stacks will run the recipe after the instance boots or if you explicitly trigger the Setup event.

  3. Run the Update Custom Cookbooks stack command, which installs the current version of your custom cookbook repository on the stack's instances. If an earlier version of the repository is present, this command overwrites it.

  4. Execute the recipe by running the Setup stack command, which triggers a Setup event on the instance and runs searchjson::default. Leave the Running command setup page open.

After the recipe has run successfully, you can verify it.

To verify searchjson
  1. The first step is to examine the Chef log for the most recent Setup event. On the Running command setup page, click show in the php-app1 instance's Log column to display the log. Scroll down to find your log message near the middle, which will look something like the following.

    ... [2014-09-05T17:08:41+00:00] WARN: Previous bash[logdir_existence_and_restart_apache2]: ... [2014-09-05T17:08:41+00:00] WARN: Current bash[logdir_existence_and_restart_apache2]: ... [2014-09-05T17:08:41+00:00] INFO: **********The public IP address is: '192.0.2.0'********** [2014-09-05T17:08:41+00:00] INFO: Processing directory[/etc/sysctl.d] action create (opsworks_initial_setup::sysctl line 1) ...
  2. Use SSH to log in to the instance and list the contents of /tmp, which should include a file named ip_addresses that contains the IP address.