Terraform in Azure— Introduction
Use IaC to provision and manage any cloud, infrastructure or service. It provision infrastructure across multiple public clouds and services using a single workflow.
To install terraform in your machine you would need to download the executable file from the official website https://www.terraform.io/ and then work accordingly.
Since terraform executable is already installed in Azure Cloud Shell and hence I will be skipping the installation process.
Let’s look into below diagram:
Terraform Configuration File: Here you would need to provide the details like Provider and Cloud account details. Provider helps to extend the functionality of terraform. i,e. :- Azure, AWS, OCI etc.
We also have to define the resource details which needs to be deployed as part of this IaC deployments.
Terraform Commands:
terraform init: This is used to downloads the provider plugins.
terraform plan: This is used to create the plan which terraform uses to create a plan on what to do based on the configuration file.
terraform apply: This is used to apply the configuration file which was carried out in plan.
terraform destroy: This is used to destroy all the resources.
For more info of these Command you can refer below url.
Please refer the below snippet of the terraform code and just replace the subscription id and tenant id with your azure ids.
In this exercise, we are going to create a storage account in azure using terraform. Please make sure you save the below code with the extension .tf, i,e(storage.tf)
Once you have above code in your local machine, you can go ahead and upload this file in azure cloud shell and then run terraform command to create the resource. refer the below image to upload the .tf file onto azure cloud shell.
Once you upload the file onto Azure cloud shell then please verify if the terraform configuration file has been loaded or not. Now you can go ahead and run “terraform init” command, it will initialize the provider plugins.
Once it has initialized the provider plugin, you can go ahead and run “terraform plan -out storage.tfplan” — You can pass any unique name to print the plan output file.
Once plan has been generated without any error you can now run “terraform apply storage.tfplan”.
Here you go, you have successfully created a storage account in azure using terraform.
Once you run apply command it creates a .tfstate, which maps real world resources to your configuration.
If you want you can go ahead and delete this newly created resource you can use “terraform destroy” command. It refers the .tfstate file which was maintained as part of terraform and destroy the resource.
For more information you can refer below resource.
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Please follow me in LinkedIn :