Learn Why and How to Use Infrastructure as a Code



In an attempt to improve the success of software projects, approaches such as automated deployment and continuous integration have been widely adopted. The software part of projects have received a lot of attention, but that has not been the case with infrastructure. There are varying definitions of infrastructure.

One definition sees infrastructure as the combination of operating systems and hardware elements such as servers and routers, among others. A different definition of infrastructure sees it as a combination of different environments and the various services that support it such as firewalls. In the automated releasing article, we defined an environment as the configuration and resources needed by an application and in this tutorial this is what we refer to when an environment is mentioned.

The definition of infrastructure we provided earlier is in a traditional context. In recent years, the term infrastructure has emerged as code in the software development terminology. In the modern sense, an infrastructure is considered as all elements of a project solution, but excluding the application. Configured web servers and operating systems among other components make up the infrastructure.

Infrastructure as a code is a concept that has emerged to place an emphasis on using the techniques of organizing code in organizing your infrastructure. DevOps and Agile are not the originators of infrastructure automation because it existed long before. Infrastructure as a code helps solve the challenge of collaboration between development and operations by enabling understanding between the two. Before delving into more details, we need to demonstrate why adopting infrastructure as a code would be beneficial and explain the steps that need to be followed in the implementation.

In the traditional approach, each developer would set up their development environment without a common environment. Changes are made at the individual developer environment. None or very scanty documentation exists presenting an entry barrier to new team members. To address challenges that are present in the traditional approach tools are not tied to DevOps or the concept of infrastructure as a code were developed. Among these tools, Vagrant and puppet are some of the widely used. Vagrant supports teams in developing virtual environments that are not tied to any platform. Puppet supports setting up of configurations. The use of these tools lead to a set up where individual or common development and test environments are linked to version control systems (VCS).

In Vagrant, a test environment is specified in a text file which we then refer to as a Vagrant file. Before we can use Vagrant, we need to install it. The prerequisites of installing Vagrant are Ruby and Virtualbox. To check if VirtualBox is installed, run this vboxmanage –version command at a terminal. If it is installed the version number will be printed. If it is not installed use the command below to install.
install vbox
To check that Ruby is installed run the command ruby -v at the terminal. If a version number is not returned install Ruby using the command below.

install ruby
To install Vagrant, use this command sudo apt-get install vagrant.
install vagrant
A second way of installing Vagrant is downloading the installation package. Head over to here https://www.vagrantup.com/downloads.html and download the Debian package. Once the download is complete, move to the directory where it was downloaded and execute the command below.

After installing, we can check if the setup was correct by running the command vagrant –version which will return the version number if the setup was correct and an error otherwise.

Before you can begin using Vagrant, there are several concepts you need to understand. A vagrant box refers to base image of either an operating system or a kernel. It represents a virtual machine. The software that creates and manages the virtual machines is referred to as a provider. Examples of providers are Virtualbox and VMware. In this article, VirtualBox is used because it is available as an open source resource. Setting up the virtual server, installing required software and task execution are the responsibilities of the provisioner. Puppet, Chef and Ansible are some commonly used provisioner tools. The Vagrantfile contains the Vagrant configuration.

To create an instance, we first need to create a directory and move into it. The commands below will accomplish it.

We need to either add a remote or local box. To add a remote, we specify the local or remote URL that points to the box. For example, to add a remote ubuntu trust box, we use the command below.

To add a local box, we would use the command below:

To remove a box we use the command vagrant box remove and specify the name of the box.

These are just examples of commands available when using Vagrant. A comprehensive resource on Vagrant commands is available here https://www.vagrantup.com/docs/cli/.

Earlier, we noted there are several configuration tools available. Vagrant does not limit you to using Puppet and you can also use Chef, but in this article we will give a Puppet example.

The first step is to instruct Vagrant that Puppet will be used for provisioning and specify where Puppet manifests are located. The key concept in Puppet is a resource. A resource can refer to commands that will be executed, software packages and other resources such as files. There are several ways resources can be organized. They include node structure, classes and modules.

In this tutorial, we introduced infrastructure in the traditional and modern contexts. We introduced infrastructure provisioning as done in the traditional approach and identified the challenges that arise from such an approach. We then identified two tools Vagrant and Puppet, which can be used to overcome challenges of traditional infrastructure provisioning.


Please enter your comment!
Please enter your name here