Open In App

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

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Terraform script for version selection

 

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

Terraform script for VN,SUBNETS

 

Terraform script for SG,NETWORKINTERFACE

 

Terraform script for RG

 

Terraform script for VM,SSH_KEY

 

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

Terraform script for variables

 

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

Terraform script for outputs

 

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
Applying terraform init command

 

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

terraform plan
Applying terraform plan command

 

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

Applying terraform apply command

 

terraform apply

 

Output of terraform apply command

 

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

Azure console

 

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

To destroy simply use: 

terraform destroy 
terraform destroy

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads