Hello!
In this article I want to talk about autoconfiguration in the cloud. For example, run ec2-instance on which WordPress will “cook”.
By autoconfiguration, I understand the automatic installation and configuration of packages, the deployment of applications. The configuration process should be done without a login via ssh to a configurable instance, literally one command.
')

To get an instance with WordPress installed, we need
Amazon EC2 API Tools and
Chef-Solo , as well as a
cookbook for it.
A cookbook is a collection of recipes according to which Chef-Solo will “prepare” the server (configure it). We will need several such "cookbooks", namely:
Everything except wp is a cookbook from the
community repository . wp is a simple one-recipe cookbook, with which Chef-Solo will download the latest WordPress version from svn, create a database and install it. All the necessary cookbooks I gathered in
the git
repository .
To run Chef-Solo, two solo.rb and node.json configuration files are required.
solo.rb:file_cache_path "/var/chef-solo" cookbook_path "/var/chef-solo/cookbooks"
This is the path to the cookbook.
node.json: { "run_list": [ "recipe[php::package]", "recipe[php::module_mysql]", "recipe[apache2]", "recipe[apache2::mod_php5]", "recipe[subversion]", "recipe[mysql::server]", "recipe[wp]" ], "php" : { "conf_dir" : "/etc/" }, "mysql" : { "server_root_password" : "xxxie0AiquaiX", "service_name" : "mysqld", "platform" : "amazon" } }
In this file, we specify which recipes to run, and set the parameters.
Preparations are over, you can proceed. All we have to do is to raise the instance with a command from the Amazon EC2 API Tools set:
ec2-run-instances {ami} -t {instance shape} -k {key_name},
download cookbooks and configs, install and run chef-solo.
But! We have agreed that we will not log in via ssh to the instance. How do we execute several commands without logging in to the instance? We can help a very useful feature AWS -
user-data . Using it, you can transfer any data to the instance, including the execution of bash scripts.
chef-solo-inst.sh:
Now we are ready to raise the auto-configurable instance:
ec2-run-instances ami-1624987f -t t1.micro -k {your_key_name} --user-data-file chef-solo-inst.sh
where - user-data-file chef-solo-inst.sh is an option that says to use locally located bash-script as user-data.
After a few minutes, you can execute the ec2-describe-instances command, copy the dns-name of the raised instance and open http: // {instance_dns_name} / wp in the browser, and the WordPress setup page should open:

If the page is not available, then something went wrong. What exactly you can find out by looking at the autoconf-log. {Date} log file, which will be located on the instance in the / root directory.
Thus, we got a fully configured and ready to work instance. Using chef-recipes in conjunction with user-data, you can configure systems of any complexity. Moreover, recipes can be cross-platform, and they can be used to configure instances with different operating systems.
The most interesting thing about this approach is that we work with infrastructure as with code (Infrastructure as a Code). Therefore, you can use the same techniques as in software development (for example,
TDD ).
This article shows one of the simplest ways to autoconfigure, but you can go further: make images with a chef client already installed so that the instance, at startup, is configured by a chef server depending on the role. But this is a topic for the next article.