- The very first step is to Install Terraform in your machine, by visiting the official site of Terraform. Here you need to download the binary file as per your Operating System.
Site Link: https://developer.hashicorp.com/terraform/downloads
-
Unzip and copy the single executable file (terraform.exe) into a separate install directory (for example, inside c:\Terraform).
-
Set the path environment variable with the path to the install directory. • Run the command sysdm.cpl and in the Advanced tab, click on Environment variables. • Scroll down in system variables until you find PATH and click edit. • Add the installation directory of Terraform (c:\Terraform).
-
To Verify, if Terraform got installed.
- Install Azure CLI, for that go to Google and search for Download Azure CLI setup and install it.
Site Link: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
- Now, login to Azure via CMD by running “az login”. It will take you to the browser to login.
- Now create the Azure SP.
This created Azure Service Principle; we will use it to setup the authentication between Terraform and Azure.
-
Now, make a directory in your local system and create a file named “main.tf” and write the script to create the resources.
-
Where, we have written code to create resources like Resource Group, App Service Plan, and App Service.
-
Also code to upload the .tfstate file to the Azure Storage Account.
- Now, after creating the terraform file, we will initiate it with the help of the command “terraform init”.
- Now, we will run the following command to check what is the plan of terraform with this code, going forward. “terraform plan”.
- As, we can see it is planning to create three resources.
- Now, we will go ahead and create it, with the help of following command. “terraform apply”
- We can see Apply has been completed and new resources has been deployed in the above screenshot. Let’s verify it by visiting the Azure Portal.
All the desired services have been created successfully.
- We will open the App Service that we have created. Now, we have to deploy our code.
- For that, we will go to “Deployment Center” and select the Code Source as “GitHub”.
- Authenticate it with your GitHub account.
- Now, enter the Organization, Repository, Branch as per your requirements.
- We need to click on “Configure runtime”.
- Here, select the “Stack: .NET” and “.NET version: .NET 7 (STS)” and click on Save.
- Now, after saving check logs, it is in progress.
- From the logs, we can see it has been completed successfully.
- To check if your site is up, click on browse.
===========================================================================================================
To Upload .tfstate file in Storage Account:
- Create a storage account, where you want to store the tfstate file. As, we have created in the below screenshot,
- Now, go inside the storage account and add a Container by clicking on “Containers” on LHS.
- Create a new container. As, I have created named “chxtan”.
- Now, add the below block to the terraform file.
backend "azurerm" { resource_group_name = "NAME_OF_RG" storage_account_name = "NAME_OF_STORAGE_TO_STORE" container_name = "NAME_OF_CONTAINER_TO_STORE" key = "deploy.tfstate" }
- It will save the tfstate file to the Storage Account along with the resource creation.