If you have missed the first article of this series, refer to it here,
The first article gave you an insight of how to start working with Terraform. Here, in this second article of the series, I will guide you through the below points.
- Terraform status file
- How to connect to Azure using Terraform
Terraform Status File
If you run the commands mentioned in my first article, there's a file generated after final execution with an extension .tfstate. This is the status file. This file keeps track of all modifications you do inside your .tf file after the very first execution. So, you will get historical information about the changes you are doing in your infrastructure if you use terraform to run your infrastructure needs. Also, one more interesting fact is that usual .tf files contain a large number of lines and if some of the lines broken during execution, terraform won't rollback the successful execution that happened up to that broken line, but it will register that broken area inside the status file.
After you correct that broken line inside .tf and execute it again, then terraform will start executing from that last broken line going forward only. So all this tracking information is in this status file. Then how to read this status file? You can't by just opening it, but you have the below command to run. First, open a command prompt and go to the folder where your status file is. Then, run the command "terraform show." Below is one of the outputs I received from my status file which is handling the infrastructure provisioning.
How to Connect to Azure
As I mentioned in my 1st article, terraform supports multiple cloud services even though I focus on Azure. I also mentioned in my 1st article that you need to fill out .tf file with certain keywords for Terraform to interpret it and execute. One of the keywords is provider, which is meant to be the external provider to which you need to communicate. This provider will decide which cloud service you need to use; i.e., Azure, AWS, Alibaba etc. It assumes that you have an Azure Service Principal.
Service Principal in Azure
Service Principal is one of the suggested ways to connect to Azure remotely and manage Azure resources. Terraform supports Service Principal to connect to Azure. If you don't know how to create an Azure Service Principal, follow this,
https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal
Connect through Azure Service Principal. You need a total of four keys, and if you have these keys handy, you can set the below environment varaibles so that terraform will read them and connect without mentioning them in the .tf file.
- ARM_SUBSCRIPTION_ID
- ARM_CLIENT_ID
- ARM_CLIENT_SECRET
- ARM_TENANT_ID
Now write inside.tf file that you need azure provider, like below.
This is the only content in your .tf file
Verifying Azure Connection
Make sure to remove any old files -- .fsstate etc. -- from your executing folder so it contains only terraform.exe and your.tf file mentioned above. My executing path is:
D:\JAISH\TERRAFORM. Open a new command prompt window and as mentioned in my previous article, run init, plan and apply and verify the below results.
Last, please verify that the azurerm provider has been downloaded, by checking the physical path.
Now, you are ready to play with a terrific combination of Terraform + Azure. In the third article, I will guide through creating some resources remotely using Terraform in Azure. I uploaded the .tf file for your reference. Thank you!