Infrastructure as Code - Terraform
- What is Infrastructure as code
- Why is so important
- What is Terraform
- Features of terraform
- Use Cases
What is Infrastructure as code
Infrastructure as code (IaC) is a way of how you define the resources that you want to deploy as code. IaC helps to build, change, version your infrastructure at low-level (Compute instances, storage, networking) and high-level(DNS entries, SaaS features).
Why is so important
The traditional way of provision a compute instance is via Ticket services such as Service Now, so the user raises a ticket and then the sys admin takes that ticket and provision that compute instance through a GUI or maybe using the CLI, so what about if the user needs thousand of compute instances ? , well the task becomes to be tedious , we do not want to waste time provisioning those resources one by one. Here is a different question, what about if we want to scale down at nights to save cost and scale up during a peak day?
Since most of the IaC providers are API driven we can automate stuffs such as schedule a script to run every morning and provision thousands of compute instances. We can codify all process, automating and also versioning, we will be able to see who changes what, who my infrastructure is actually defined at any given time and also have a transparency documentation.
We can conclude then having infrastructure as code we are ble to:
- Control Version
- Reusability of the code
- Abilty to automate on top of it
What is Terraform
Terraform was introduced in the market by Hashicorp in 2014 as IaC. Terraform is a cloud-agnostic (AWS, Azure, GCP and others cloud platforms) tool to provision resources using a configuration file written in Hashicorp Language (HCL).
The workflow of Terraform for all clouds is basically:
- terraform init
- terraform plan
- terraform apply
Features of terraform
Infrastructure as code
You can define your resources that will be provisioned in a configuration file using HCL followig Terraform workflow.
Execution plans
You can test your deployment before apply changes. This way you will see what will be the result before fly, in case you see something odd that does not satify what you want you can update, once everything looks good you call apply
Resource Graph
Terraform builds a graph based on resources you want to deploy, in case a non-dependent resources are found those resources will be created and modified parallelly.
Changing automation
Based on infra provisioned previously we know what terraform will change and in what order. Terraform does not need to createn again the resources from scratch, only the new ones needed, also respects the dependencies.
Use Cases
-
Multi-Cloud Deployment: Terraform is cloud-agnostic and allows a single configuration to be used accross all clouds providers, so we can build large-scale multi-cloud infrastructures.
-
Heroku App Setup: A PAAS for hosting web apps. You can use terraform to provision the setup required for a Heroku app and config DNSimple to set CNAME or Cloudfare as a CDN for the app.
-
Multi-Tier Applications: You can group resources in each tier together, and Terraform will manages the dependencies between each tier. For example, Terraform will make sure the database is availabe before the web nodes are created and also the load balancers are connect to the web servers.
-
Self-Service Clusters: This is for large organization when exists a centralized operation team and they cannot handle everything because the demand it’s pretty obvious, this allow to product teams to handle their own infrastructure using tooling provided by the centralized operation team. A terraform configuration file can be shared across within an organization, so the rest of the product teams can use it.
-
Software Demos: For testing sofware we can use Vagrant to build virtualized envs for demos, it is still very difficult to use real infrastructure that matches production env. Developers can provide a Terraform configuration file to to create, provision, and bootstrap a demo on cloud providers such as Azure, GCP, and AWS.
-
Disposable Environments: It is common to have development, staging or QA and production environments. dev, staging or QA are a clone of production environment, but are used to test new features or applications before releasing in production. It’s very difficult to maintain an up-to-date staging environment.
We can use Terraform to write a configuration file for production and then shared with staging, QA or dev. so we can spin up a new env to test in and the easily removed when you are done testing. Terraform can help to maintain parallel envs.
-
Software-Defined Networking: This is very common and famous in Data Centers. Allows operators and developers have more control on it. Operator operates a Software-defined network easily and developers write applicatios on top of the nertwork infrastructure provided. There are two main components here:
- Control layer
- Infrastructure Layer
A configuration file can be written for sofware defined networks. Terraform can use that file to automatically set up and modify settings by interfacing with the control layer. For example, you can use Terraform to configure Azure Virtual Network and AWS VPC.
-
Resource Schedulers: Static assigment of applications to machines is very challenging. In terms of Terraform, there are a number of schedulers such as Borg, Mesos, YARN, and Kubernetes. Terraform can dynamically schedule Docker containers, Hadoop, Spark, and many other software tools.
Terraform is not limited to cloud providers such as Azure, GCP, and AWS. Resource Schedulers are providers, enabling Terraform to request resources from them. There is a kubernetes provider that can be configured using Terraform.
Sources: