Open In App

Microsoft Azure – How To Deploy VM in Azure using Terraform?

Pre-requisite: Azure, Terraform 

This article demonstrates how to use Terraform to build a complete Linux environment with all the necessary resources. A virtual network, subnet, public IP address, and other resources are among them. 



Terraform is an open-source tool for building, changing, and versioning infrastructure safely and efficiently. It may manage both specialized internal solutions and well-known service suppliers. Terraform operates by describing the desired state of the infrastructure in a configuration file and then making the necessary changes to reach that state. The configuration file is written in the HashiCorp Configuration Language (HCL), which is a human-readable language that is used to describe the infrastructure as code

Steps To Create a Terraform Script

Step 1: In the folder create a new file named providers.tf.



 

Step 2: After this create a new file named main.tf. It should contain all the resources required in order to create the resources.

 

 

 

 

Step 3: After that create a new file named variables.tf, and add the following lines of code to it.

 

Step 4: The next step is to create a new file named outputs.tf, and add the following lines of code to it.

 

Once the files are created, there are three steps you are supposed to do:

Step 1: Run the `terraform init` command to download and install the required Azure provider and any other dependencies.

terraform init

 

Step 2: Run the `terraform plan` command to see a preview of the changes that will be made to your Azure environment.

terraform plan

 

Step 3: Finally, run the `terraform apply` command to deploy the infrastructure to Azure. Terraform will execute the plan and make the changes to your Azure environment.

terraform apply

 

 

 

To verify whether the deployment was successful we can visit the azure portal and check.

 

NOTE: Please destroy the infrastructure, after you are done to remove the extra cost.

To destroy simply use: 

terraform destroy 

 


Article Tags :