Infrastructure as Code — Terraform
By: Manthan Lal
Tags: Microsoft Azure, Infrastructure Management, Cloud, Terraform, Infrastructure As Code

Problem

IT infrastructure has evolved rapidly since the internet.

Virtualization and containerization have changed the way we think about IT infrastructure.

Cloud computing has solved the problem of availability and scalability, but it doesn’t solve the inconsistency issues and related complexity.

IaaS, PaaS, SaaS platforms further increase the complexity of the infrastructure configuration.

When you have more than one person performing the configurations, you’re bound to get discrepancies.

Can we manage Infrastructure like we manage the code?

Infrastructure as Code (IaC)

Historically, managing IT infrastructure was a manual process. People would physically put servers in place and configure them. Applications would be deployed only after the machines were configured to the correct settings required by the OS and applications. Unsurprisingly, this manual process would often result in several problems such as follows –

Cost

Scalability

Availability

Inconsistency

Infrastructure as Code (IaC) is the Missing Piece of the Puzzle to create a manageable and consistent IT infrastructure. Infrastructure as Code evolved to solve the problem of environment drift in the release pipeline. Without IaC, teams must maintain the settings of individual deployment environments. Over time, each environment becomes a snowflake, that is, a unique configuration that cannot be reproduced automatically. Inconsistency among environments leads to issues during deployments.

Teams who implement IaC can deliver stable environments rapidly and at scale. Teams avoid manual configuration of environments and enforce consistency by representing the desired state of their environments via code.

Idem-potence is a principle of Infrastructure as Code. Idem-potence is the property that a deployment command always sets the target environment into the same configuration, regardless of the environment’s starting state.

Modern cloud-native applications embrace the widely accepted practice of Infrastructure as Code or IaC. With IaC, you automate platform provisioning.

What is Infrastructure as Code?

Infrastructure as a code is the process of managing and provisioning computer data centers through machine definition files rather than physical hardware configuration or interactive configuration tools and IT infrastructure managed by this compromise both physical equipment’s such as bare metal servers as well as virtual machines and associated configuration resource so let’s let me put this in simpler terms, infrastructure as code simply means to manage your IT infrastructure using configuration files, the key takeaway from this definition is that before IaC IT personnel would have to manually change configurations to manage their infrastructure, mainly they would use a throw script to automate some task but that was the extent of it, with IaC your infrastructures configuration takes the form of a code file, since it is just text it is easy for you to edit copy and distribute it, you can and you should put it under source control just like any other source code file, so this was about the definition infrastructure as a code.

Infrastructure as Code enables DevOps teams to test applications in production-like environments early in the development cycle. These teams expect to provision multiple test environments reliably and on-demand.

How Infrastructure as Code works?

A developer defines the configuration parameters in a domain-specific language (DCL).

The instruction files are sent to a master server, a management API, or a code repository.

The IaC platform follows the developer’s instructions to create and configure the infrastructure.

With IaC, users don’t need to configure an environment every time they want to develop, test, or deploy software. All infrastructure parameters are saved in the form of files called manifests.

As with all code files, manifests are easy to reuse, edit, copy and share. Manifests make building, testing, staging, and deploying infrastructure quicker and consistent.

Developers codify the configuration files store them in version control. If someone edits a file, pull requests and code review workflows can check the correctness of the changes.

Imperative vs Declarative Approach

IaC tools can vary as far as the specifies of how they work but we can generally divide them into two main types the one that follows the Imperative approach and the one that follows the Declarative approach if you think the above category have something to do with the programming language paradigms then you are absolutely correct.

The imperative approach gives orders to define a sequence of commands or instructions so the infrastructure can reach the final result.

For wear and tear down of VMs, or environment, or for scaling up or down, we have to write custom scripts.

A declarative approach on the other hand declares the desired outcome instead of explicitly outlining the sequence of steps the infrastructure needs to reach the final result. The declarative approach shows what the final result looks like.

A declarative approach allows you to more easily manage the configuration.

Mutable vs Immutable Infrastructure

When it comes to IaC, mutable refers to an infrastructure that needs to be constantly updated and changed to continue to meet the changing needs of the business. This means that IT professionals have to individually address each server and switch, which can translate into long hours spent identifying problems and coming up with solutions instead of taking the time to rebuild the system in a way that removes any quirks. Since each component is different and requires a unique approach that can often only be performed by one person, the servers are sometimes referred to as snowflakes.

Immutable IaC represents the future by requiring that each component is built according to exact specifications. There is no room for small deviations that have to be individually addressed. Once a change is required, the infrastructure is provisioned according to the new requirements, and the old IaC is taken out of commission.

Benefits of Infrastructure as Code

Speed — The first benefit IaC provides the speed, it enables you to quickly set up your complete infrastructure by running a script, you can do that for every environment from development to production passing through staging QA and more. IaC can make the entire software development lifecycle more efficient.

— The first benefit IaC provides the speed, it enables you to quickly set up your complete infrastructure by running a script, you can do that for every environment from development to production passing through staging QA and more. IaC can make the entire software development lifecycle more efficient. Consistent — Manual processes result in mistakes, communication is hard and we are in general pretty bad at it as you already know manual infrastructure management the result in discrepancies no matter how hard you try, we can easily solve the problem by having the config files themselves the single source of truth, that means you guarantee that same configuration will be deployed over and over without discrepancies.

— Manual processes result in mistakes, communication is hard and we are in general pretty bad at it as you already know manual infrastructure management the result in discrepancies no matter how hard you try, we can easily solve the problem by having the config files themselves the single source of truth, that means you guarantee that same configuration will be deployed over and over without discrepancies. Accountability — Since you can version IaC configuration files like any other source code file, you have full traceability of the changes each configuration suffered, no more guessing games about who did what and when.

— Since you can version IaC configuration files like any other source code file, you have full traceability of the changes each configuration suffered, no more guessing games about who did what and when. Efficiency — By employing IaC you can deploy your infrastructure architecture in many stages that make the whole software that makes the development lifecycle more efficient raising the team’s productivity to new levels, you could have programmers using the IaC to create and launch sandbox environments allowing them to develop in the isolation safely, the same would be true for QA professionals as well who can have the perfect copies of the production environments in which to run their tests, final minutes the deployment time you can push the infrastructure and code to the production in one step.

— By employing IaC you can deploy your infrastructure architecture in many stages that make the whole software that makes the development lifecycle more efficient raising the team’s productivity to new levels, you could have programmers using the IaC to create and launch sandbox environments allowing them to develop in the isolation safely, the same would be true for QA professionals as well who can have the perfect copies of the production environments in which to run their tests, final minutes the deployment time you can push the infrastructure and code to the production in one step. Cost — One of the main benefits of IaC without a doubt is lowering the cost of infrastructure management by employing cloud computing, along with IaC you dramatically reduce your cost that is because you don’t have to spend money on hardware, hire people to operate it, and build or rent physical space to store it. But IaC also lowers your cost in another and circular way that is what we call the opportunity cost.

Infrastructure as Code Tools

AWS CloudFormation

Azure Resource Manager

Google Cloud Deployment Manager

Terraform

Puppet

Chef

Ansible

The tool we are going to talk about is Terraform, the 3 tools (AWS CloudFormation, Azure Resource Manager, and Google Cloud Deployment Manager) are designed to work with specific cloud environment but that is not the case with the tool called Terraform, developed by HashiCorp. Terraform is completely cloud agnostic and helps you tackle large infrastructure for complex distributed applications, for example with more ease than working on a cloud-specific platform. These ships and is orchestrated in varying degree with the focus on the core plan or apply cycle.

Terraform

So, what is Terraform, Terraform is an infrastructure provisioning tool where you can store your cloud infrastructure setup as code, it’s very similar to tools such as CloudFormation which you would use to automate your AWS infrastructure but you can only use that on AWS, with Terraform you can use it on other providers you can use it for AWS, you can use it for Google, Azure, and thing like Cloudflare as well.

While we have ARM templates, ARM templates are specific to Azure. They’re not multi-Cloud, they don’t work with an on-premise environment, and both sides of the Devs and the infrastructure teams really struggle. That’s where Terraform comes in.

Most of the end-users are coming from an on-premise environment. A lot of them use things like VMware On-Prem and they try to orchestrate that. They are using one tool for VMware and they start looking at the Cloud platform GCP and AWS, there’s a different tool for that and then when they come to Azure, they have to use ARM templates. So, Terraform resolves that issue. You can deploy Terraform to VMware, GCP, and AWS. So, you have you use a different template but the same formatting. So, it’s loading one format of code, basically.