This website uses cookies to better the user experience of its visitors. Where applicable, this website uses a cookie control system, allowing users to allow or disallow the use of cookies on their computer/device on their first visit to the website. This complies with recent legislative requirements for websites to obtain explicit consent from users before leaving behind or reading files such as cookies on a user’s computer/device. To learn more click Cookie Policy.

Privacy preference center

Cookies are small files saved to a user’s computer/device hard drive that track, save, and store information about the user’s interactions and website use. They allow a website, through its server, to provide users with a tailored experience within the site. Users are advised to take necessary steps within their web browser security settings to block all cookies from this website and its external serving vendors if they wish to deny the use and saving of cookies from this website to their computer’s/device’s hard drive. To learn more click Cookie Policy.

Manage consent preferences

These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
Cookies list
Name _rg_session
Provider rubygarage.org
Retention period 2 days
Type First party
Category Necessary
Description The website session cookie is set by the server to maintain the user's session state across different pages of the website. This cookie is essential for functionalities such as login persistence, ensuring a seamless and consistent user experience. The session cookie does not store personal data and is typically deleted when the browser is closed, enhancing privacy and security.
Name m
Provider m.stripe.com
Retention period 1 year 1 month
Type Third party
Category Necessary
Description The m cookie is set by Stripe and is used to help assess the risk associated with attempted transactions on the website. This cookie plays a critical role in fraud detection by identifying and analyzing patterns of behavior to distinguish between legitimate users and potentially fraudulent activity. It enhances the security of online transactions, ensuring that only authorized payments are processed while minimizing the risk of fraud.
Name __cf_bm
Provider .pipedrive.com
Retention period 1 hour
Type Third party
Category Necessary
Description The __cf_bm cookie is set by Cloudflare to support Cloudflare Bot Management. This cookie helps to identify and filter requests from bots, enhancing the security and performance of the website. By distinguishing between legitimate users and automated traffic, it ensures that the site remains protected from malicious bots and potential attacks. This functionality is crucial for maintaining the integrity and reliability of the site's operations.
Name _GRECAPTCHA
Provider .recaptcha.net
Retention period 6 months
Type Third party
Category Necessary
Description The _GRECAPTCHA cookie is set by Google reCAPTCHA to ensure that interactions with the website are from legitimate human users and not automated bots. This cookie helps protect forms, login pages, and other interactive elements from spam and abuse by analyzing user behavior. It is essential for the proper functioning of reCAPTCHA, providing a critical layer of security to maintain the integrity and reliability of the site's interactive features.
Name __cf_bm
Provider .calendly.com
Retention period 30 minutes
Type Third party
Category Necessary
Description The __cf_bm cookie is set by Cloudflare to distinguish between humans and bots. This cookie is beneficial for the website as it helps in making valid reports on the use of the website. By identifying and managing automated traffic, it ensures that analytics and performance metrics accurately reflect human user interactions, thereby enhancing site security and performance.
Name __cfruid
Provider .calendly.com
Retention period During session
Type Third party
Category Necessary
Description The __cfruid cookie is associated with websites using Cloudflare services. This cookie is used to identify trusted web traffic and enhance security. It helps Cloudflare manage and filter legitimate traffic from potentially harmful requests, thereby protecting the website from malicious activities such as DDoS attacks and ensuring reliable performance for genuine users.
Name OptanonConsent
Provider .calendly.com
Retention period 1 year
Type Third party
Category Necessary
Description The OptanonConsent cookie determines whether the visitor has accepted the cookie consent box, ensuring that the consent box will not be presented again upon re-entry to the site. This cookie helps maintain the user's consent preferences and compliance with privacy regulations by storing information about the categories of cookies the user has consented to and preventing unnecessary repetition of consent requests.
Name OptanonAlertBoxClosed
Provider .calendly.com
Retention period 1 year
Type Third party
Category Necessary
Description The OptanonAlertBoxClosed cookie is set after visitors have seen a cookie information notice and, in some cases, only when they actively close the notice. It ensures that the cookie consent message is not shown again to the user, enhancing the user experience by preventing repetitive notifications. This cookie helps manage user preferences and ensures compliance with privacy regulations by recording when the notice has been acknowledged.
Name referrer_user_id
Provider .calendly.com
Retention period 14 days
Type Third party
Category Necessary
Description The referrer_user_id cookie is set by Calendly to support the booking functionality on the website. This cookie helps track the source of referrals to the booking page, enabling Calendly to attribute bookings accurately and enhance the user experience by streamlining the scheduling process. It assists in managing user sessions and preferences during the booking workflow, ensuring efficient and reliable operation.
Name _calendly_session
Provider .calendly.com
Retention period 21 days
Type Third party
Category Necessary
Description The _calendly_session cookie is set by Calendly, a meeting scheduling tool, to enable the meeting scheduler to function within the website. This cookie facilitates the scheduling process by maintaining session information, allowing visitors to book meetings and add events to their calendars seamlessly. It ensures that the scheduling workflow operates smoothly, providing a consistent and reliable user experience.
Name _gat_UA-*
Provider rubygarage.org
Retention period 1 minute
Type First party
Category Analytics
Description The _gat_UA-* cookie is a pattern type cookie set by Google Analytics, where the pattern element in the name contains the unique identity number of the Google Analytics account or website it relates to. This cookie is a variation of the _gat cookie and is used to throttle the request rate, limiting the amount of data collected by Google Analytics on high traffic websites. It helps manage the volume of data recorded, ensuring efficient performance and accurate analytics reporting.
Name _ga
Provider rubygarage.org
Retention period 1 year 1 month 4 days
Type First party
Category Analytics
Description The _ga cookie is set by Google Analytics to calculate visitor, session, and campaign data for the site's analytics reports. It helps track how users interact with the website, providing insights into site usage and performance.
Name _ga_*
Provider rubygarage.org
Retention period 1 year 1 month 4 days
Type First party
Category Analytics
Description The _ga_* cookie is set by Google Analytics to store and count page views on the website. This cookie helps track the number of visits and interactions with the website, providing valuable data for performance and user behavior analysis. It belongs to the analytics category and plays a crucial role in generating detailed usage reports for site optimization.
Name _gid
Provider rubygarage.org
Retention period 1 day
Type First party
Category Analytics
Description The _gid cookie is set by Google Analytics to store information about how visitors use a website and to create an analytics report on the website's performance. This cookie collects data on visitor behavior, including pages visited, duration of the visit, and interactions with the website, helping site owners understand and improve user experience. It is part of the analytics category and typically expires after 24 hours.
Name _dc_gtm_UA-*
Provider rubygarage.org
Retention period 1 minute
Type First party
Category Analytics
Description The _dc_gtm_UA-* cookie is set by Google Analytics to help load the Google Analytics script tag via Google Tag Manager. This cookie facilitates the efficient loading of analytics tools, ensuring that data on user behavior and website performance is accurately collected and reported. It is categorized under analytics and assists in the seamless integration and functioning of Google Analytics on the website.

The Ultimate Guide to Configuring a Rails App on Amazon EC2 with Chef: Part 2

  • 14858 views
  • 9 min
  • Jun 06, 2018
Yaroslav B.

Yaroslav B.

Ruby/JS Developer

Daryna P.

Daryna P.

Copywriter

Dmytro H.

Dmytro H.

Backend Development Lead

Share

This is the second part of our in-depth guide devoted to configuring Rails app on Amazon EC2 with Chef. In the previous part, you’ve learned the basic concepts of Infrastructure as Code and main components of the Chef repository. You’ve also started to describe the basic configuration for your server such as configurations for data bags and environment.

In this part, we show you how to write your own cookbooks. Besides, we explain the wrapper-cookbook notion using scripts for installing and configuring the PostgreSQL database and other software necessary for the correct operation of server and the application on it. Here we go.

Base setup

We’ll start with the basic components:

  • Hostname
  • Project attributes
  • Users groups
  • Sudo privilege

Hostname

This site cookbook will set the hostname for our server. To do this, use the ready cookbook chef_hostname.

Then, write a wrapper cookbook for this task.

Wrapper cookbooks

Put simply, a wrapper cookbook is the simplest type of cookbook; it includes recipes from other cookbooks and defines default attributes.

A wrapper cookbook is structured as follows:

Let’s specify the dependency on the chef_hostname cookbook in the Berksfile.

Next, create a directory with the cookbook.

Describe the additional information about the cookbook in metadata.rb. Here you should define the name, version, and dependencies of the cookbook on other cookbooks. You can also add information about the author and so on. You can find more information about metadata in the Chef documentation.

Now create a default recipe for the cookbook.

This recipe sets the hostname of our server to the name of the node.

Project attributes

We need to create the cookbook that will contain project-specific attributes for the current project. These attributes often used while writing other cookbooks: the name of the project, the repository where the project is, and so on.

Let’s create default attributes for the cookbook.

Then we’ll define the attributes in which the project name and a link to the Github repository will be stored.

In this tutorial, we’ll be using the ready-made Spree app.

These attributes will be accessible through the node object such as node ['project'] ['name'].

Users and groups

We need to create users and groups such as deployer. To do this, we’ll employ the users cookbook.

Add dependencies for the users cookbook in the Berksfile.

Then create a directory for the сookbook:

and set the following metadata:

Next, create a directory with the system attributes for the cookbook.

This file describes the information needed for the system users of the project. System users are users who install and configure processes for certain software. We recommend connecting the distinct user for each installed software to minimize damage in case you detect vulnerabilities in a certain package or if a malefactor gets access rights of the owner of this package.

Now create a recipe.

Describe the installation of system groups and users in this recipe:

After that, describe the behavior for creating groups and users from data bags:

Finally, aggregate all the described recipes into the single recipe that will be launched by default for this cookbook.

Sudo privilege

Now we’ll install sudo on our machine. To do this, use the sudo cookbook.

Add the following dependency to the Berksfile:

Create a cookbook and set the metadata for it.

We want all users whose group box contains the sudo value as well as the groups containing wheel and sysadmin groups in their group box to have sudo privileges.

Let’s create a default recipe for the cookbook.

Then specify the following:

Once we’ve described the basic cookbooks, we’ll create a role called setup that aggregates these cookbooks.

In this role, write basic information such as the name and description. Then, install cookbooks from the current setup role in the run list.

Next, specify this role in the node of your YOUR_IP_ADDRESS.json server.

Database setup

At this point, we’ll describe the configurations for installing and configuring the database and its components on the server. We recommend using PostgreSQL as the main database.

PostgreSQL database

To install and configure PostgreSQL on the server, use postgresql_lwrp. Also, use the locale cookbook to determine the default system locale.

Here are step-by-step instructions:

  1. Initialize the main database cluster for PostgreSQL version 9.6.
  2. Create a PostgreSQL superuser (deployer) and a postgresql user for your project.
  3. Create a PostgreSQL database for the project.

Now let’s add the necessary dependencies in the Berksfile.

Finally, create a cookbook.

To make it more convenient to support this setup in future, set the following attributes in app-attributes:

  1. Default locales
  2. PostgreSQL version
  3. PostgreSQL users
  4. PostgreSQL databases

The PostgreSQL hash function has the signature format $password$username. For example, if the username was deployer and the password was spree-app, then the command to generate the password would look like this:

On Linux:

On macOS:

Replace the the stubbed value ‒ PASSWORD ‒ for each user with the result of this command.

Next, describe metadata cookbooks.

Define the default attributes for the cookbooks.

Before you determine the behavior for the tasks described above, create a default recipe and connect the dependencies at the beginning of the file. You also need to define variables with which you’ll work in the default recipe.

1. Initialize the main database cluster for PostgreSQL version 9.6.

Define the configurations for the client’s authentication for the main cluster called as pg_hba.

Now, initialize the main cluster.

2. Create a PostgreSQL superuser named deployer and also a PostgreSQL user for the project.

Let’s use the variables described above to create a PostgreSQL user and a PostgreSQL database with the user’s name:

3. Create a PostgreSQL database for the project by applying the variables listed above.

Then put the cookbook you’ve created into the database role:

Define the necessary information about the role in roles/database.rb.

Finally, add this role to run_list in nodes/YOUR_IP_ADDRESS.json.

Web setup

Let’s proceed to configuring the software necessary for the correct operation of the web component of the application. To do this, you’ll need to install:

  • Ruby
  • Node.js
  • Imagemagick
  • Redis
  • Nginx

Installing Ruby

To install Ruby, you need the chef_rvm cookbook.

Add the necessary dependencies in the Berksfile.

After that, put the attributes such as versions (all versions you need to install) and default (the default version) in site-cookbooks/app-attributes/attributes/default.rb.

Now create a cookbook.

Then set the metadata for the cookbook.

To install rvm, you need to add a gpg2 key. To do this, create a recipe that will add a gpg2 key to your machine. You can read more information about gpg2 keys here.

Now create a recipe for installing Ruby.

Describe the installation of all necessary versions of Ruby in this recipe.

Next, create a default cookbook for the recipe.

In this cookbook, you need to aggregate the created recipes in the required order.

Install Node.js

We’ll use Node.js as the environment for JavaScript runtime execution. Node.js allows you to use Coffeescript and the asset pipeline in your Rails application, which combines and minimizes your JavaScript to ensure a faster production environment. To install the node from the official prebuilt binaries, use nodejs.

Add dependencies in the Berksfile.

Next, put the version (version of Node.js) and checksum attributes in site-cookbooks/app-attributes/attributes/default.rb.

Now create a cookbook.

Then set the cookbook metadata.

Create default attributes for the cookbook.

Next, you need to specify that you’ll use the binary installation method.

Create a default recipe for the cookbook.

Connect an external cookbook for installation of Node.js.

Installing ImageMagick

Use carrierwave to process downloaded files and perform image operations on the server side. Carrierwave hinges on mini_magick, which in turn is based on ImageMagick. Therefore, you should install ImageMagick. Use the imagemagick cookbook to do this.

Add dependencies in the Berksfile.

Create a cookbook.

Set the metadata for the cookbook.

Create a default recipe for the cookbook.

Then describe the installation of the cookbook.

Installing Redis

You need to apply Sidekiq for background processing for Ruby. Sidekiq uses Redis as a repository. To install Redis, use the chef-redis cookbook.

Add dependencies in the Berksfile.

Create a cookbook.

Set the metadata for the cookbook.

Create default attributes for the cookbook.

Then set the maxmemory for Redis.

Create a default recipe for the cookbook.

Connect an external cookbook to install Redis.

Installing Nginx

We’ll use Nginx as the web server for the application. To install Nginx, use the nginx cookbook.

First, add dependencies in the Berksfile.

Before you start to install and configure Nginx, create a system user. Later, this system user will be tied to Nginx. To create a user, add the following code to the attributes of system users:

Next, include this user in the recipe for installing system users.

Then create a cookbook.

Set the metadata for the cookbook.

After that, put the version (version of Nginx) and checksum attributes in site-cookbooks/app-attributes/attributes/default.rb.

Create default attributes for the cookbook.

Set attributes for Nginx:

We’ll buffer all requests and responses between the client and the Rails application through Puma. To do this, set the path to puma.sock, which will be located in the project directory. Since the path to the project is necessary for the deployment and other components depending on the deployment, we’ll create default attributes for the app-deploy cookbook for future use. We’ll describe the app-deploy cookbook later.

Set the metadata for the app-deploy cookbook.

You also need to create the default attributes.

In the default cookbooks, describe the basic information about the project deployment including the username and group on whose behalf you’ll be deploying. You also need to describe the path to the directory in which you’ll put the app.

After that, add this cookbook to the dependencies.

Now you can start creating a default recipe for the cookbook.

Connect an external cookbook and describe the installation of Nginx.

We recommend putting the Nginx configuration into a separate template that’s tied to a specific environment. In our case, this is dev.erb.

Create the templates directory and the Nginx configuration file for the environment.

Then write the configurations.

We recommend learning how to configure Nginx here.

Now you can collect all these recipes in a role called web for future use in the node of the server.

Next, describe basic information about the role and its run list.

Then add this role to the run_list of the nodes/YOUR_IP_ADDRESS.json node.

Now you already know how to write your own cookbooks. In the next part of our guide, we’ll end writing the cookbooks with configurations for the server security and for monitoring server processes. We’ll apply all configurations written earlier to deploy the Spree application on Ruby on Rails to the previously set up EC2.

Check out the third part of our tutorial.

CONTENTS

Authors:

Yaroslav B.

Yaroslav B.

Ruby/JS Developer

Daryna P.

Daryna P.

Copywriter

Dmytro H.

Dmytro H.

Backend Development Lead

Be the first user to rate this article!

Nay
So-so
Not bad
Good
Wow
0 rating, average 0 out of 5

Share article with

Comments (0)

There are no comments yet

Leave a comment

Subscribe via email and know it all first!