Example 6: Creating 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
After you have created directories, you often need to populate them with configuration files, data files, and so on. This topic shows two ways to install files on an instance.
Installing a File from a Cookbook
The simplest way to install a file on an instance is to use a cookbook_file
/srv/www/shared
after the directory is created. For
reference, here is the original recipe.
directory "/srv/www/shared" do mode 0755 owner 'root' group 'root' recursive true action :create end
To set up the cookbook
-
Inside the
opsworks_cookbooks
directory, create a directory namedcreatefile
and navigate to it. -
Add a
metadata.rb
file tocreatefile
with the following content.name "createfile" version "0.1.0"
-
Initialize and configure Test Kitchen, as described in Example 1: Installing Packages, and remove CentOS from the
platforms
list. -
Add a
recipes
subdirectory tocreatefile
.
The file to be installed contains the following JSON data.
{ "my_name" : "myname", "your_name" : "yourname", "a_number" : 42, "a_boolean" : true }
To set up the data file
-
Add a
files
subdirectory tocreatefile
and adefault
subdirectory tofiles
. Any file that you install withcookbook_file
must be in a subdirectory offiles
, such asfiles/default
in this example.Note
If you want to specify different files for different systems, you can put each system-specific file in a subfolder named for the system, such as
files/ubuntu
. Thecookbook_file
resource copies the appropriate system-specific file, if it exists, and otherwise uses thedefault
file. For more information, see cookbook_file. -
Create a file named
example_data.json
with the JSON from the preceding example and add it tofiles/default
.
The following recipe copies example_data.json
to a specified
location.
directory "/srv/www/shared" do mode 0755 owner 'root' group 'root' recursive true action :create end cookbook_file "/srv/www/shared/example_data.json" do source "example_data.json" mode 0644 action :create_if_missing end
After the directory resource creates /srv/www/shared
, the
cookbook_file
resource copies
example_data.json
to that directory and also sets the
file's user, group, and mode.
Note
The cookbook_file
resource introduces a new action:
create_if_missing
. You could also use a create
action, but that overwrites an existing file. If you don't want to overwrite
anything, use create_if_missing
, which installs
example_data.json
only if it does not already
exist.
To run the recipe
-
Run
kitchen destroy
to start with a fresh instance. -
Create a
default.rb
file that contains the preceding recipe and save it torecipes
. -
Run
kitchen converge
, then log in to the instance to verify that/srv/www/shared
containsexample_data.json
.
Creating a File from a Template
The cookbook_file
resource is useful for some purposes, but it just
installs whatever file you have in the cookbook. A template
This example modifies the createfile
cookbook to use a
template
resource to install a slightly modified version of
example_data.json
.
Here's what the installed file will look like.
{ "my_name" : "myname", "your_name" : "yourname", "a_number" : 42, "a_boolean" : true, "a_string" : "some string", "platform" : "ubuntu" }
Template resources are typically used in conjunction with attribute files, so the example uses one to define the following values.
default['createfile']['my_name'] = 'myname' default['createfile']['your_name'] = 'yourname' default['createfile']['install_file'] = true
To set up the cookbook
-
Delete the
createfile
cookbook'sfiles
directory and its contents. -
Add an
attributes
subdirectory tocreatefile
and add adefault.rb
file toattributes
that contains the preceding attribute definitions.
A template is a .erb
file that is basically a copy of the
final file, with some of the contents represented by placeholders. When the
template
resource creates the file, it copies the
template's contents to the specified file, and overwrites the placeholders with
their assigned values. Here's the template for
example_data.json
.
{ "my_name" : "<%= node['createfile']['my_name'] %>", "your_name" : "<%= node['createfile']['your_name'] %>", "a_number" : 42, "a_boolean" : <%= @a_boolean_var %>, "a_string" : "<%= @a_string_var %>", "platform" : "<%= node['platform'] %>" }
The <%=...%>
values are the placeholders.
-
<%=node[...]%>
represents a node attribute value.For this example, the "your_name" value is a placeholder that represents one of the attribute values from the cookbook's attribute file.
-
<%=@...%>
represents the value of a variable that is defined in the template resource, as discussed shortly.
To create the template file
-
Add a
templates
subdirectory to thecreatefile
cookbook and adefault
subdirectory totemplates
.Note
The
templates
directory works much like thefiles
directory. You can put system-specific templates in a subdirectory such asubuntu
that is named for the system. Thetemplate
resource uses the appropriate system-specific template if it exists and otherwise uses thedefault
template. -
Create a file named
example_data.json.erb
and put in thetemplates/default
directory. The template name is arbitrary, but you usually create it by appending.erb
to the file name, including any extensions.
The following recipe uses a template
resource to create
/srv/www/shared/example_data.json
.
directory "/srv/www/shared" do mode 0755 owner 'root' group 'root' recursive true action :create end template "/srv/www/shared/example_data.json" do source "example_data.json.erb" mode 0644 variables( :a_boolean_var => true, :a_string_var => "some string" ) only_if {node['createfile']['install_file']} end
The template
resource creates example_data.json
from a template and installs it in /srv/www/shared
.
-
The template name,
/srv/www/shared/example_data.json
, specifies the installed file's path and name. -
The
source
attribute specifies the template used to create the file. -
The
mode
attribute specifies the installed file's mode. -
The resource defines two variables,
a_boolean_var
anda_string_var
.When the resource creates
example_data.json
, it overwrites the variable placeholders in the template with the corresponding values from the resource. -
The
only_if
guard attribute directs the resource to create the file only if['createfile']['install_file']
is set totrue
.
To run the recipe
-
Run
kitchen destroy
to start with a fresh instance. -
Replace the code in
recipes/default.rb
with the preceding example. -
Run
kitchen converge
, then log in to the instance to verify that the file is in/srv/www/shared
and has the correct content.
When you are finished, run kitchen destroy
to shut down the instance.
The next section uses a new cookbook.