Introduction to Terraform
Terraform is an open-source infrastructure as code software tool developed by HashiCorp. Terraform provides a way to build change and version infrastructure safely and efficiently with the Hashicorp Configuration Language. It is like a human readable blueprint that can execute and automate everything we do in the cloud.
First of all, Let’s know what is infrastructure as code.
Infrastructure as Code (IaC)
Infrastructure as Code (IaC) is the managing of infrastructure through code instead of through manual processes. With IaC, configuration files are created that contain your infrastructure specifications, which makes it easier to edit and distribute configurations.It also ensures that you provision the same environment every time.IaC helps reduce human error. It helps to increase the efficiency of software development.
There are many open-source infrastructure as code software tools Like Ansible, Puppet, Vagrant, AWS Cloud Formation, Azure Resource Manager, Google Cloud Deployment Manager and so on.
AWS Cloud Formationn, Azure Resource Manager and Google Cloud Deployment Manager can use only their relevant cloud platforms only. But terraform, Ansible, Puppet and other software tools can be used in any cloud platforms.
So Let’s talk about Terraform
Terraform provides a lot of features. As I mentioned Terraform uses a human readable JSON based language called Hashicorp Configuration Language (HCL). Using Terraform we can manage the infrastructure on multiple cloud platforms.
We can develop a terraform script with the necessary resources and using that we can deploy them in multiple cloud platforms and multiple regions at once. We can commit Terraform configurations to version control to safely collaborate on infrastructure.
Terraform has great documentation. Through that you can learn how to work with terraform very easily.
These are the major features in terraform. Let’s talk about how to work with Terraform
Get Started with Terraform
First, let’s know how to install Terraform.
Install Terraform
Using the link below you can download the Terraform setup according to your OS.
If you’re a windows user, you can follow this video tutorial to install Terraform successfully.
After install you can check your terraform version using the terraform –version
Command in the command prompt. If it shows 1.2.5 or above, congratulations, you installed Terraform successfully.
Create Terraform File
You can create a new terraform file like “YouFileName.tf”. If you use your favorite code editor for that you can see the terraform logo in front of the file name.
Syntax
Let’s learn how to write Terraform code. Terraform consists of only a few basic elements.
<BLOCK TYPE> “<BLOCK LABEL>” “<BLOCK LABEL>” {
# Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
There are several Block types like “resource”, “variable”, “output” and so on. You can use them according to your need. Block labels vary by cloud platforms. The other Block Label is used to name the resource. It can be defined by the user. Identifiers may vary by resource. The user can assign values to the identifiers. You can go through the following example.
resource "azurerm_resource_group" "main" { name = "test-resource-group" location = "centralus" tags = { environment = "dev" source = "terraform" }}
This is an example for configure a resource group for Azure.
RUN
Now you can test your terraform code. First, we have to initialize our working directory containing Terraform configuration files. For that we can use the terraform init
Command. After a few seconds you can see the “.terraform.lock.hcl” file with a success message.
Then you can hit the terraform plan
Command to determine the desired state of all the resources it declares. If there are any errors you can see them in the terminal with line numbers. Else you can see how many resources that you added in the terminal.
After all these configurations now you can deploy your first cloud resource in your favorite cloud platform using the terraform apply
Command.
Congratulations, you deploy your cloud resources in your favorite cloud platform using Terraform.
Destroy
If you don’t want to keep running your resources in the cloud platform, then you should hit the terraform destroy
Command and delete all your deployed resources very easily.
In the next article, I will show how to deploy some resources to the Azure cloud platform with better folder structure and some good practices.
See you in the next article Bye. 👌
Happy coding!!! 💻