The official terraform documentation defined Terraform as an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently.
Instructions are written in a simple, readable format to describe how everything should be configured. Terraform then follows these instructions to set everything up for you.
One thing to note as well is that Just like terraform which is an infrastructure as code tool, other cloud providers have theirs as well.. Bicep and Arm template is also an Azure Infrastructure as Code service. AWS has the AWS Cloud Formation which also helps you model and set up your AWS resources
This blog will serve as a beginners guide to understand the important steps to deploying Azure resources with Terraform.
Prerequisite
An Azure account with an active subscription. You can also create a free account here if you do not have one.
Install Terraform.. Visit the official terraform website to install and configure terraform.
Download and install Visual Studio Code. Install the terraform extension on the vs code
Ensure you have Azure CLI installed on your host machine to enable you login to azure through the CLI
Create a Terraform Configuration Folder
Open visual studio code in new windows
Navigate to "file" as shown in the picture below, then click on open folder.
Click on create a new folder and give your folder a name. Ensure the new folder has been named
Then click on "Select Folder"
From the screenshot below, within the created folder, click on the file icon to create a main.tf file
The main.tf file contains the main set of configuration for your module.
- Once the file is named main.tf, then work begins..
Declare Your Provider Configuration
- The first lines of code we need to input in our main.tf file is the Azurerm provider.
To get the latest version of azurerm provider, click on this link
Once you click on the link, at the top-right corner of the page, click on "user provider" to copy the code as well as the latest version.
Ensure to save the code
On visual studio code, click on the (3 dots) ellipsis
Select the terminal button, and then new terminal
Before we continue, for better intellisense, when we configure the provider and the resources that the provider provides, we should initialize our terraform working directory.
To do this, run the "terraform init" command
- From the screenshot provided below, our working directory as been successfully initialized
Lets go ahead and declare our provider configuration
We will set an empty features block which is required.
On line 13 input "skip_provider_registration = true"
- You can go ahead and start declaring your resources. Because I have an existing resource group, I will just go ahead and state the resource group which is testing-rg located in the UK South region.
Authenticate Your Azure Account
To login to your azure account, you need to input the az login command.
You will be prompted to login into your azure account
- Once you are logged in, you can close the page as shown below, then navigate back to visual studio code.
- Once you are signed in, a success output would be seen on your terminal.
Import The Existing Resource Group
To import the existing resource group, navigate to the resource group on the azure portal.
This ensures that terraform doesn't create another resource group when we deploy our infrastructure
Scroll down to properties, there you have it.
Copy the resource ID
- Go back to the Visual Studio Code terminal and run the command as seen below terraform import and the type of resource we want to to import.
azurerm_resource_group.(the instance of the resource) followed by the resource ID.
- Our Import was successful.
- Declare your Virtual Network and Subnets Resource
Format and Validate the Configuration
We can then go ahead and validate our configuration by running the terraform validate command.
Plan and Deploy Your Configuration
- Once we have been able to validate our deployment, then we can go ahead and run terraform plan and save the plan out to a file called tfplan
We can also go ahead and run terraform apply to deploy our configuration and save the plan as well to tfplan file.
From the output, we can see that the deployment was a success
Azure Portal Verification
- If we go back to the Azure portal, we will find our resources successfully deployed there.
- As well as our subnets based on how many you configured and deployed.
Tidy Up Your Environment
The terraform destroy command terminates all resources specified in the Terraform state. This is a Terraform command that removes infrastructure that has been provisioned using Terraform Infrastructure-as-Code (IaC) configuration.
In Summary
Well-done!!, you've come this far.
This guide lays the groundwork for advancing your infrastructure-as-code practices on the Azure cloud platform. To further enhance your Azure deployments, consider exploring additional Terraform resources and modules. Thank you!!