Xenonstack Recommends

Infrastructure as Code (IaC) using Terraform Overview

Acknowledging Data Management
          Best Practices with DataOps


What is Terraform?

Terraform is a tool used for changing, building, and versioning infrastructure efficiently and safely. It is used for managing existing and popular service providers or custom in house solutions. The components running for a single application or entire datacenter Configuration files describe to Terraform. An execution plan is being generated by Terraform, which defines what it will do to reach the desired state and then executes it to build the described infrastructure. By the time configuration changes, Terraform can determine what changed and create incremental execution plans which can be applied. Infrastructure Terraform manages including low-level components such as compute instances, and networking, as well as high-level components such as DNS entries, SaaS features, etc.
  • Infrastructure as code.
  • It is a tool to manage virtual server life cycles (AWS, VMWare, etc.)
  • It is a tool to manage supporting services (DNS, Email)
  • It is a tool to manage system services (MySQL, PostgreSQL)
  • Configuration files can be HCL or JSON.
  • Created by Hashicorp (Vagrant et al.)
  • Written in Go

Read in detail about Terraform - The Open Source IaC tool

Features of Terraform

Infrastructure as a Code - A high-level configuration syntax is a use for describing support. It allows a blueprint of our datacenter to be versioned and treated it you would any other code. Also, infrastructure can be shared and re-used. Execution Plans - It has a “planning” step from which it generates an execution plan. This execution plan shows what Terraform will do when they apply. This eliminates any surprises when Terraform manipulates infrastructure. Resource Graph - A graph of all resources builds by Terraform, and parallelize the creation and modification of any non-dependent resources. By doing so, Terraform builds infrastructure as efficiently as possible, and operators get insight into dependencies in their infrastructure. Change Automation - With minimal human interaction, complex changesets can be applied to infrastructure. What Terraform will change and in what order can be known by the previously mentioned execution plan and resource graph, which helps in avoiding many possible human errors.

Terraform Use Cases

Heroku App Setup

Heroku is a PaaS for hosting web applications. Developers create an application and then attach add-ons like database or email provider. Its best feature is the ability to scale the number of dynos or workers elastically. Terraform is used for codifying the setup required for a Heroku application, ensuring that all necessary add-ons are available. But it can go even further -
  • Configuring DNSimple to set a CNAME,
  • By setting Cloudflare as CDN for the app.

Multi-Tier Applications

One of the pervasive patterns is N-tier architecture. A widespread 2-tier architecture is a pool of web servers that uses a database tier. More tiers can be added for API servers, caching servers, routing meshes, etc. To scale tiers independently and provide a separation of concerns, this pattern is used. Terraform is an optimal tool for building and managing these infrastructures. Every tier is described as a collection of resources and automatically handles the dependencies between each tier. Before the web servers are started, the database tier is available will ensure by Terraform and that the load balancers are aware of the web nodes. By using Terraform, each tier can be scaled easily by modifying a single count configuration value because the creation and provisioning of a resource are codified and automated, elastically with load becomes trivial.

Self-Service Clusters

In a specific organizational size, to manage a large and growing infrastructure becomes very challenging for a centralized operations team. Instead, to make “self-serve” infrastructure becomes more attractive, using tooling provided by the central operations team allowing product teams to manage their infrastructure. By using Terraform, the ability of how to build and scale a service can be codified in a configuration. To enabling customer teams to use the configuration as a black box Terraform configurations can be shared within an organization and to manage their services Terraform is used as a tool.

Why Terraform?

The main goal of DevOps is to perform more efficient software delivery and to make delivery efficient and quick. We need some tools like Terraform, which help companies with infrastructure as a code and automation. It is changing the DevOps world by changing the way infrastructure is managed, and makes faster and more efficient for executing DevOps. There exist technologies like Ansible, Chef, or Puppet for automating and provisioning of software. Terraform also uses the same law “infrastructure as code,” but it targets on automation of infrastructure itself. Complete cloud infrastructure like instances, IPs, volumes, networking can be defined easily.

Compairing Terraform with Chef, Ansible, Puppet OR Salt?

On a machine that already exists, the configuration management tools install and manages. Terraform does the same, and also it allows us to focus on bootstrapping and initializing resources. Configuration management tools can also be used along with Terraform to configure things inside a virtual machine. Terraform makes the configuration management tool to be used to set up a resource once it has been created by using provisioners. When using Docker containers for running applications, these are self-sufficient and will contain the whole configuration of the application. Tools like Chef, Puppet, Ansible, etc. is not needed. But still, need something to manage infrastructure because the container will run anyway in the top of a server/virtual machine. Terraform is used to create infrastructure for containers to run on. Tools such as Chef, Ansible, Puppet, etc. are used as IAS or Infrastructure as Code, but Terraform is best for this because it can even maintain the state of infrastructure.

Advantages of Terraform

  • Orchestration, not merely Configuration: For server configuration, all the configuration management tools were created, and to manage and to install the software already existed in the server / virtual machine where their primary goal.
  • Terraform focuses more on server provisioning. When the complete cloud infrastructure is considered as code, and all the parameters were combined in a configuration file, all the members of the team can easily collaborate on them as they would do any other code.
  • Multi-Provider - The most versatile feature of Terraform is that it supports multi-cloud orchestration like AWS, Azure, OpenStack, etc. and also premises deployments. It is beneficial when using two different resources from two various cloud providers at the same time.
  • Immutable Infrastructure - When using Chef, Puppet, Salt runs any software updates on servers; this often leads to configuration drift when differences in the configuration lead to bugs that lead to security breaches. Terraform solves this issue by using an immutable infrastructure approach in which every change in configuration leads to a separate configuration snapshot which means de-provisioning the old one and deployment of a new one. Updating the development environment by this way goes smooth and bug-free and returning the old configuration is easy like rolling back to a specific version.
  • Syntax - HCL (HashiCorp Configuration Language) is a custom language that is used by Terraform.
  • Dry Runs - Terraform uses a command known as “terraform plan,” which creates an execution plan; by using this execution plan, we can check whether the set of changes meets the expectation without performing any changes to real resources or state. For example, by running the terraform plan before committing a change to version control, check whether it will work as expected or not.
  • Client-only architecture - Terraform eliminates the need for additional checks for provisioning the infrastructure, which leverages the cloud provider’s API. In Ansible, this is done by connecting through SSH but with limitations. Terraform works on APIs and have a wide variety of options, which helps to make it more secure, reliable, and easy to use.
  • Super Portability - There are only a single tool and single language for describing the infrastructure which is used for multiple cloud providers. The problem of migrating to vendors is not a problem.

Read about the  10 Best Infrastructure as Code (IaC) Tools to Boost Your Productivity

How to implement Terraform Using AWS?

There are following steps to implement Terraform (By using AWS)
  • Setting up an AWS account
  • Installing Terraform
  • Deploying a single server
  • Deploying a single web server
  • Deploying a cluster of web servers
  • Deploying a load balancer
  • Clean up
Setting up AWS account - (Infrastructure across different types of cloud providers like AWS, Google Cloud, Azure, DigitalOcean, and many others) Cloud hosting services provided by AWS were reliable and scalable. AWS is one of the most popular cloud infrastructure providers. Installing Terraform - Find the supported package of Terraform for system and download it. Terraform is download as a zip archive, then unzip the package. After installing, verify the installation by opening a terminal session and checking that Terraform is available. Deploying a single server - HCL language is used for writing Terraform code in files with extension “.tf.” HCL is a declarative language that describes the infrastructure which is wanted, and Terraform will find out how to create it. Infrastructure across different platforms or providers can be created by Terraform like AWS, Google Cloud, Azure, DigitalOcean, and many others. Configure the provider want to use is the first step for using Terraform. Deploying a single web server: In the next step, run a web server on this instance. Deploying a cluster of web servers - To running a single server can be a good start, but a single server can be a single point of failure. If the server overwhelmed by heavy traffic or the server crashes, users could no longer access the site. To eliminate this run, a cluster of servers, which are routing around servers that go down and on the base of traffic, adjust the size of cluster up or down. There is a lot of work for managing such a cluster manually. Fortunately, AWS will take care of this by using the Auto Scaling Group (ASG). Deploying a load balancer - One more problem to solve before launching the ASG: that there are many Instances, for this need a load balancer to distribute traffic across all of them. There is a lot of work for creating a load balancer that is highly available and scalable. AWS will let take care of this by using an Elastic Load Balancer (ELB) Clean up - After doing experimenting with Terraform, remove all the created resources, so AWS doesn’t charge for them. Terraform keeps track of created resources, and cleanup is a breeze.

Terraform Commands (CLI)

The command-line interface (CLI) is used for controlling Terraform.
  • apply - Use for changes or builds infrastructure
  • console - Use for interactive console
  • destroy - Use for impair Terraform – managed infrastructure
  • fmt - Use for rewriting config files into canonical format
  • get - Use for downloading and installing modules for configuration
  • graph - Use for creating visual graph of the Terraform resources
  • import - Use for importing existing infrastructure into Terraform
  • init - Use for initializing existing or new Terraform configuration
  • output - Use for reading output from a state file
  • plan - Use for generating and showing an execution plan
  • providers - Use for printing tree of providers used in the configuration
  • push - Use for uploading this Terraform module to Terraform Enterprise to run
  • refresh - Use for updating local state file against real resources
  • show - Use for inspecting Terraform state or plan
  • taint - Use for manually marking resources for recreation
  • untaint - Use for manually unmarking a resource as tainted
  • validate - Use for validating terraform files
  • version - Use for printing Terraform version
  • workspace - Use for workspace management

A Comprehensive Approach

To learn more about Configuration management and Compliance and governance and provisioning IT infrastructure we recommend following steps -

Related blogs and Articles

Understanding the Role of Containers in DevOps

Deployment Automation

Understanding the Role of Containers in DevOps

Introduction to Containers in DevOps Let us start the blog with Role of Containers in DevOps, but basics first. What are Containers? Containers give a standard approach to package your application's source code, configuration files, libraries, and dependencies in a single object. A single box can be used to run small microservices or large software applications. Containers provide OS-level...