Infrastructure as Code (IaC) is a widespread terminology among DevOps professionals. It is the process of managing and provisioning the complete IT infrastructure (comprises both physical and virtual machines) using machine-readable definition files. It is a software engineering approach toward operations. It helps in automating the complete data center by using programming scripts. With all the features that Infrastructure as Code provides, it has multiple challenges:

Need to learn to codeDon’t know the change impact.Need to revert the changeCan’t track changesCan’t automate a resourceMultiple environments for infrastructure

Terraform has been created to solve these challenges. Below are some of the benefits of using Terraform.

Does orchestration, not just configuration managementSupports multiple providers such as AWS, Azure, GCP, DigitalOcean and many moreProvide immutable infrastructure where configuration changes smoothlyUses easy to understand language, HCL (HashiCorp configuration language)Easily portable to any other providerSupports Client only architecture, so no need for additional configuration management on a server

Terraform Core concepts

Below are the core concepts/terminologies used in Terraform:

Variables: Also used as input-variables, it is key-value pair used by Terraform modules to allow customization.Provider: It is a plugin to interact with APIs of service and access its related resources.Module: It is a folder with Terraform templates where all the configurations are definedState: It consists of cached information about the infrastructure managed by Terraform and the related configurations.Resources: It refers to a block of one or more infrastructure objects (compute instances, virtual networks, etc.), which are used in configuring and managing the infrastructure.Data Source: It is implemented by providers to return information on external objects to terraform.Output Values: These are return values of a terraform module that can be used by other configurations.Plan: It is one of the stages where it determines what needs to be created, updated, or destroyed to move from real/current state of the infrastructure to the desired state.Apply: It is one of the stages where it applies the changes real/current state of the infrastructure in order to move to the desired state.

Terraform Lifecycle

Terraform lifecycle consists of – init, plan, apply, and destroy.

Terraform init initializes the working directory which consists of all the configuration filesTerraform plan is used to create an execution plan to reach a desired state of the infrastructure. Changes in the configuration files are done in order to achieve the desired state.Terraform apply then makes the changes in the infrastructure as defined in the plan, and the infrastructure comes to the desired state.Terraform destroy is used to delete all the old infrastructure resources, which are marked tainted after the apply phase.

How Terraform Works?

Terraform has two main components that make up its architecture:

Terraform CoreProviders

Terraform Core

Terraform core uses two input sources to do its job. The first input source is a Terraform configuration that you, as a user, configure. Here, you define what needs to be created or provisioned. And the second input source is a state where terraform keeps the up-to-date state of how the current set up of the infrastructure looks like. So, what terraform core does is it takes the input, and it figures out the plan of what needs to be done. It compares the state, what is the current state, and what is the configuration that you desire in the end result. It figures out what needs to be done to get to that desired state in the configuration file. It figures what needs to be created, what needs to be updated, what needs to be deleted to create and provision the infrastructure.

Providers

The second component of the architecture are providers for specific technologies. This could be cloud providers like AWS, Azure, GCP, or other infrastructure as a service platform. It is also a provider for more high-level components like Kubernetes or other platform-as-a-service tools, even some software as a self-service tool. It gives you the possibility to create infrastructure on different levels. For example – create an AWS infrastructure, then deploy Kubernetes on top of it and then create services/components inside that Kubernetes cluster. Terraform has over a hundred providers for different technologies, and each provider then gives terraform user access to its resources. So through AWS provider, for example, you have access to hundreds of AWS resources like EC2 instances, the AWS users, etc. With Kubernetes provider, you access to commodities, resources like services and deployments and namespaces, etc. So, this is how Terraform works, and this way, it tries to help you provision and cover the complete application setup from infrastructure all the way to the application. Let’s do some practical stuff. 👨‍💻 We will install Terraform on Ubuntu and provision a very basic infrastructure.

Install Terraform

Download the latest terraform package. Refer to the official download page to get the latest version for the respective OS. Extract the downloaded package. Move the terraform executable file to the path shown below. Check the terraform version. You can see these are the available commands in terraform for execution.

Provision AWS EC2 Instance Using Terraform

In this demo, I am going to launch a new AWS EC2 instance using Terraform. Create a working directory for this Terraform demo. Go to the directory and create a terraform configuration file where you define the provider and resources to launch an AWS EC2 instance. Note: I have changed the access and secret keys 😛, you need to use your own. From the configuration mentioned above, you can see I am mentioning the provider like AWS. Inside the provider, I am giving AWS user credentials and regions where the instance must be launched. In resources, I am giving AMI details of Ubuntu (ami-0a634ae95e11c6f91) and mentioning the instance type should be t2.micro You can see how easy and readable the configuration file is, even if you are not a die-hard coder.

terraform init

Now, the first step is to initialize terraform.

terraform plan

Next is the plan stage; it will create the execution graph for creating and provisioning the infrastructure.

terraform apply

The apply stage will execute the configuration file and launch an AWS EC2 instance. When you run apply command, it will ask you, “Do you want to perform these actions?”, you need to type yes and hit enter. Go to your AWS EC2 dashboard, and you will see a new instance with the instance id mentioned at the end of apply command has been created. You have successfully launched an AWS EC2 instance using Terraform.

terraform destroy

Finally, if you want to delete the infrastructure, you need to run the destroy command. If you recheck the EC2 dashboard, you will see the instance got terminated.

Conclusion

I believe the above gives you an idea to get it started with Terraform. Go ahead and try out the example I have just shown. You should also check out these infrastructure automation software. If you are interested in learning more, then I would suggest checking Learning DevOps with Terraform course.

An Introduction to Terraform for Beginners   Terraform Tutorial - 49An Introduction to Terraform for Beginners   Terraform Tutorial - 61An Introduction to Terraform for Beginners   Terraform Tutorial - 29An Introduction to Terraform for Beginners   Terraform Tutorial - 86An Introduction to Terraform for Beginners   Terraform Tutorial - 17An Introduction to Terraform for Beginners   Terraform Tutorial - 42An Introduction to Terraform for Beginners   Terraform Tutorial - 86An Introduction to Terraform for Beginners   Terraform Tutorial - 99An Introduction to Terraform for Beginners   Terraform Tutorial - 43An Introduction to Terraform for Beginners   Terraform Tutorial - 40An Introduction to Terraform for Beginners   Terraform Tutorial - 21An Introduction to Terraform for Beginners   Terraform Tutorial - 66An Introduction to Terraform for Beginners   Terraform Tutorial - 27An Introduction to Terraform for Beginners   Terraform Tutorial - 48An Introduction to Terraform for Beginners   Terraform Tutorial - 68An Introduction to Terraform for Beginners   Terraform Tutorial - 93An Introduction to Terraform for Beginners   Terraform Tutorial - 58An Introduction to Terraform for Beginners   Terraform Tutorial - 26An Introduction to Terraform for Beginners   Terraform Tutorial - 64An Introduction to Terraform for Beginners   Terraform Tutorial - 21An Introduction to Terraform for Beginners   Terraform Tutorial - 43An Introduction to Terraform for Beginners   Terraform Tutorial - 83An Introduction to Terraform for Beginners   Terraform Tutorial - 35An Introduction to Terraform for Beginners   Terraform Tutorial - 95An Introduction to Terraform for Beginners   Terraform Tutorial - 20An Introduction to Terraform for Beginners   Terraform Tutorial - 4An Introduction to Terraform for Beginners   Terraform Tutorial - 12An Introduction to Terraform for Beginners   Terraform Tutorial - 47