Azure deployment of IaaC with Terraform
Introduction:
In this blog, we will go through steps to deploy terraform for deploying infrastructure to Azure.
Using below steps, we can deploy infrastructure to Azure using Terraform by setting up environment to writing and executing Terraform code.
Below is a step-by-step guide:
Step 1: Install Terraform
-
Download Terraform: Visit the Terraform download page and based on your OS, download it. https://developer.hashicorp.com/terraform/install
-
Install Terraform: Follow the installation instructions for your OS. For example, on Ubuntu Linux:
$ wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg $ echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list $ sudo apt update && sudo apt install terraform
-
Check version for installation is proper:
$ terraform --version
Step 2: Install Azure CLI
-
Install Azure CLI: Visit the Azure CLI installation page for installation instructions.
-
Verify Installation: Check if Azure CLI is installed correctly:
$ az --version
Step 3: Login to Azure to check whether login is successful.
-
Login to Azure: Use the Azure CLI to log in to your Azure account:
$ az login
This command will open a web browser where you can sign in with your Azure credentials.
Step 4: Set Up Your Terraform Directory
-
Create a Directory: Create a new directory for your Terraform files:
$ mkdir terraform_test $ cd terraform_test
-
Create a Terraform Configuration File: Create a file named main.tf in your directory. This file will contain the Terraform code to deploy your Azure resources.
Step 5: Write Terraform Code
-
Define the Provider: In main.tf , define the Azure provider:
provider "azurerm" { features = {} }
-
Create a Resource Group: Add the following code to create a resource group:
resource "azurerm_resource_group" "example" { name = "example" location = "West Europe" }
-
Create Additional Resources: Depending on your requirements, you can add more resources like virtual networks, storage accounts, and virtual machines.
Step 6: Initialize Terraform
-
Initialize the Terraform Directory: Run the following command to initialize the Terraform working directory:
$ terraform init
This command downloads the necessary provider plugins.
Step 7: Plan the Deployment
-
Generate an Execution Plan: terraform plan command is for generating an execution plan:
$ terraform plan
This command shows you what changes Terraform will make to your Azure infrastructure.
Step 8: Apply the Configuration
-
Deploy the Infrastructure: Apply the Terraform configuration to deploy the resources:
$ terraform apply
You will be prompted to confirm the changes. Type “Yes” to proceed.
Step 9: Verify the Deployment
-
Check in Azure Portal: Go to the Azure Portal and verify that the resources have been created as specified in your Terraform configuration.
Step 10: Clean Up Resources
-
Destroy the Infrastructure: If you want to remove the deployed resources, use the following command:
$ terraform destroy
Again, confirm the destruction by typing “Yes” when prompted.
Conclusion:
Based on above steps, we can configure and deploy terraform in Azure environment.