Infrastructure-as-Code (IaC): Methodologies, Approach and Best Practices

Mir Ali
7 min readMay 11, 2021

IaC Overview

As everything is digitized now, especially after the covid pandemic, it is now even more important to properly manage the IT infrastructure of an organization. [1] [2]

Earlier, this management of IT infrastructure was done manually by the system administrators. They managed all the hardware and software that was required for an application to run. Tech has progressed a lot in the past few years, and now there is an alternative to this manual management, called Infrastructure as Code or IaC in short.

Let us define IaC in more descriptive terms. Infrastructure as code is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

IaC tackles problems that were present before its use, such as manual environment build process, manual approval process, high costs, hardware issues, and errors caused by human beings.

IaC Methodologies

Let us now see the four methodologies of Infrastructure as Code, which are as follows:

Ad Hoc Scripts

Ad Hoc Scripting is the most straightforward approach for the automation of processes. These scripts convert manual processes to automated processes just by simply breaking them down into discrete steps. You can achieve this with the help of scripting languages like Ruby, Python, Bash, PowerShell, etc.

If you run an ad hoc script, it will definitely give the expected results, and the running process is also fairly easy. However, there are chances of an error if you run the same ad hoc script multiple times. For instance, if you create a folder using an ad hoc script, then you would have to check again later if that folder still exists or not.

Let us summarize the workflow of ad hoc scripts step-wise:

● Take manual work and break it down into discrete steps

● Choose your favourite scripting language

● Define and write each of those steps in code

● Execute the scripts

Configuration Management Tools

Configuration Management Tools are designed to install and manage software on existing servers. These tools have a code with a consistent and predictable structure, clearly named parameters, proper secrets management, and even a simple file layout.

Some of the Configuration Management tools include Ansible, Chef, Puppet, and SaltStack. These tools are also known as Idempotent codes. An Idempotent code is a code or a script that produces the same results, no matter how many times you run or execute the same code.

The above-mentioned Configuration Management Tools are designed in such a way that they can install and manage software on existing servers. For instance, Ansible uses YAML files for the playbook. They can also manage a large number of remote servers.

Server Templating

Server Templating tools are used to create an image of a server. This image captures a self-contained “snapshot” of the operating system, software, files, and all other relevant details. They are considered to be an alternative to Configuration Management Tools. Some of the popular Server Templating tools include Docker, Packer, and Vagrant.

Let us check the above-mentioned tools in more detail:

Docker: Docker is used to create isolated environments for applications called containers.

Packer: It helps in packaging all dependencies and building deployable Virtual Machine images.

Vagrant: It is used for configuring virtual machines for a development environment. Vagrant runs on top of VM solutions like VirtualBox, VMware, Hyper-V, etc.

With Server Templating Tools, there is no need to make any changes once you have deployed the server. If you want to deploy your modified code, you have to create a new image and then deploy it on new servers. This is what makes Server Templating tools essential for immutable infrastructure.

Server Provisioning

Server provisioning is the process of setting up a server so that it could be used in a network based on required resources. It consists of all the operations needed to create a new machine and bring it to a working state and includes defining the desired state of the system.

Some of the Server Provisioning Tools that can help create your servers include Terraform, Azure resource manager templates, Amazon Web Services (AWS) cloud formation, and Open Stack Heat.

Terraform is a cloud infrastructure provisioning tool that supports all kinds of public and private cloud provisioning. It helps in maintaining the state of the infrastructure using a concept called state files.

Besides creating servers, the above-mentioned Server Provisioning tools can also enable one to create other resources such as databases, load balancers, firewall settings, storage, and so on.

IaC Approach

There are two major approaches to Infrastructure as Code, which are Imperative approach and Declarative approach. In both approaches, the configurations for IaC are done on a template, wherein the user describes the resources needed for every server in the infrastructure.

Let us learn about these approaches in more detail and understand the difference between the two.

Imperative approach

This approach defines the particular steps or commands needed to achieve the desired configuration. It also defines the correct order in which these commands need to be executed. Basically, it deals with “the how,” i.e., how we reached our desired configuration.

Declarative approach

This approach defines the desired state of the system. Only additional information such as resources required and the properties need to be defined other than the state, and the IaC tool will configure everything itself. It deals with “the what,” i.e., what is the desired state for the configuration.

IaC Cross Platform Landscape

IaC Best Practices

Some of the best practices you can use to make the most out of IaC are as follows:

Go Slow

When shifting to Infrastructure as Code, it could be a challenging process for many of the employees, especially those who are old.

This is because they are not much experienced with technology and how to use it on a day-to-day basis for the proper functioning of an organization. [7] [8] Thus, the more team members can easily grasp IaC, the better output they will be able to provide.

Codify Everything

The whole purpose of IaC is to automate everything and get rid of all the manual work as much as possible, therefore try codifying the whole infrastructure. Explicitly code all the infrastructure specifications in configuration files. There should be no room for documentations, and everything related to infrastructure management should be in these configuration files.

Continuous Testing, Integration, and Deployment

IaC is a code, and like any other code in this world, it should also be continuously tested, integrated, and deployed. This helps check for any errors and inconsistencies that may occur in servers before deploying them to production. It also ensures that the whole infrastructure functions without any lags or bugs.

Immutable Infrastructure

As mentioned before, immutable infrastructure refers to the servers or virtual machines that are never modified or updated after deployment. It helps in lowering IT complexities and failures, improves security, and makes troubleshooting easier. Immutable infrastructure also eliminates any kind of server patching.

Version Control

Putting all of your configuration files under the source control is also a recommended Infrastructure as Code Practice. Version control is important to keep track of changes and to enable every team member to work on the latest version. It should be used for all the codes and assets on which multiple team members are collaborating.

Other than just managing and tracking files, it also helps in developing and shipping products faster. This is possible as it helps improve the visibility, collaboration of team members and also accelerates product delivery.

Modularity

Modularity can be defined as the development of modules that can be joined or integrated together in various ways by writing many small playbooks or modules. These modules can be reused for any future projects.

Conclusion

Automation of infrastructure is now a major requirement for every team in an organization. There is an urgent need to shift manually managed and configured infrastructures to IT or automated infrastructures for easier and efficient functioning as well as management of a system.

For this automation, many tools and methodologies are available. One tool will not be able to fulfil the requirements of one’s organization or team. Therefore, tools should be selected on the basis of the requirements of the organization and various other factors such as cost, skillset, functionality, and so on.

--

--