AWS OpsWorks
User Guide (API Version 2013-02-18)


You configure many packages by creating a configuration file and placing it in the appropriate directory. You can include a configuration file in your cookbook and copy it to the appropriate directory, but a more flexible approach is to have your recipes create the configuration file from a template. One advantage of a template is that you can use attributes to define the template's values. This allows you, for example, to modify a configuration file without touching the cookbook by using custom JSON to override the appropriate attribute values.

A template has essentially the same content and structure as the associated file. Here is an example file, httpd.conf.

ServerRoot "<%= node[:apache][:dir] %>" <% if node[:platform] == "debian" || node[:platform] == "ubuntu" -%> LockFile /var/lock/apache2/accept.lock <% else -%> LockFile logs/accept.lock <% end -%> PidFile <%= node[:apache][:pid_file] %> Timeout <%= node[:apache][:timeout] %> KeepAlive <%= node[:apache][:keepalive] %> MaxKeepAliveRequests <%= node[:apache][:keepaliverequests] %> KeepAliveTimeout <%= node[:apache][:keepalivetimeout] %> <IfModule mpm_prefork_module> StartServers <%= node[:apache][:prefork][:startservers] %> MinSpareServers <%= node[:apache][:prefork][:minspareservers] %> MaxSpareServers <%= node[:apache][:prefork][:maxspareservers] %> ServerLimit <%= node[:apache][:prefork][:serverlimit] %> MaxClients <%= node[:apache][:prefork][:maxclients] %> MaxRequestsPerChild <%= node[:apache][:prefork][:maxrequestsperchild] %> </IfModule> ...

The following example is the httpd.conf file that was generated for a Ubuntu instance:

ServerRoot "/etc/httpd" LockFile logs/accept.lock PidFile /var/run/httpd/ Timeout 120 KeepAlive Off MaxKeepAliveRequests 100 KeepAliveTimeout 3 <IfModule mpm_prefork_module> StartServers 16 MinSpareServers 16 MaxSpareServers 32 ServerLimit 400 MaxClients 400 MaxRequestsPerChild 10000 </IfModule> ...

Much of the template's text is simply copied from the template to the httpd.conf file. However, <%= ... %> content is handled as follows:

  • Chef replaces <%= node[:attribute][:sub_attribute][:...]%> with the attribute's value.

    For example, StartServers <%= node[:apache][:prefork][:startservers] %> becomes StartServers 16 in the httpd.conf.

  • You can use <%if-%>, <%else-%>, and <%end-%> to conditionally select a value.

    The example sets a different file path for accept.lock depending on the platform.


You are not limited to the attributes in your cookbook's attribute files. You can use any attribute in the instance's node object. For example, generated by a Chef tool called Ohai and also incorporated into the node object. For more information on attributes, see Overriding Attributes.

For more information on templates, including how to incorporate Ruby code, see About Templates.