If you are looking to start using Terraform in Azure, in this article you will find all the information needed.
To be more precise, let me give you an overview of what you will find out about in this article:
- Terraform introduction
- Stages of Terraform
- Commands used in Terraform
- Setup for Terraform directory
- How to deploy your first Azure resources through Terraform
- How to store a Terraform state file in a remote location – Azure Storage Account
Terraform defines as a common IaC (Infrastructure as code) toolset, having providers for multiple cloud providers, Microsoft Azure being one of them.
To quote its fathers, as an infrastructure as code tool, Terraform enables you to version, reuse and share the human-readable configuration files that can define both cloud and on-prem resources. (https://www.terraform.io/intro)
Stages of Terraform
For development and deployment, Terraform contains four principal stages, as follows:
- Initialization: Terraform deployment will always start with the initialization stage
- Planning: Before you start to create an Azure resource using Terraform you are able to create and view a Terraform plan
- Applying: To deploy the Azure resources desired, you are able to run the apply stage after reviewing the plan
- Destroying: This is an optional stage that allows you to remove every Azure related resource or change by running the destroy stage
Commands used in Terraform
These commands are used to run the stages of Terraform:
- terraform init: to initialize the Terraform directory
- terraform plan: to showcase the Terraform plan
- terraform apply: to apply the Terraform code
- terraform destroy: to destroy the resources deployed through Terraform
Setup for Terraform directory
The Terraform directories allows configuration in various ways, customizable depending on the request, on the project, the client, the team and so on.
Here I will illustrate a standard setup for terraform directory
Let me describe better all the files above
- main.tf: represents the main Azure resources to be deployed
- variables.tf: contains the variables to be used in main.tf
- providers.tf: represents the numerous Terraform providers I was mentioning about, that can be used when deploying multiple Azure resources
- locals.tf: is a local value which usually represents a common value that will not be changed between several deployments
- develop.tfvars: enable you to define the variables between multiple Terraform environments
How to deploy your first Azure resources through Terraform
Writing Terraform is the same as writing any sort of configuration language code. So, use the editor that you want and ensure the storage of any changes in a repository that is version controlled, regardless if it’s a local one or a remote one.
Of course, my strong recommendation is to store the configuration code in a remote repository.
Now, while you are working on the Terraform configuration, you should verify if your syntax is correct and ensure eliminating all the error that may appear in syntax or misconfiguration by running several Terraform Plans. By using this method, you will make sure that the Terraform configuration will come out as desired.
If everything is going according to plan, then it’s time to commit that change! When you commit to the requested change, you should run Terraform Apply that allows you to add/remove or change the infrastructure that was defined in the Terraform configuration process.
The basic workflow presented here is a continuous loop for any project. Therefore, you will have to use the exact same process for any new change or additional/removal you will need to make.