TL;DR
This guide walks you through how to download and configure Terraform, then use it to create a virtual machine on Microsoft Azure. Terraform is a powerful tool for managing infrastructure as code across multiple cloud platforms, including Azure.
Introduction to Terraform on Azure
Infrastructure as Code (IaC) has become essential for managing modern cloud environments efficiently. Terraform, developed by HashiCorp, is one of the most popular IaC tools that allows developers to define, deploy, and manage infrastructure across cloud platforms like Azure.
In this guide, you will learn how to:
- Download and install Terraform.
- Configure Terraform to interact with Azure.
- Use Terraform to create a virtual machine (VM) in Azure.
Step 1: Download Terraform
To get started, you need to download Terraform for your operating system.
- Visit the Terraform download page.
- Select your operating system (e.g., MacOS, Windows, Linux).
- Download the appropriate binary.
- Extract the downloaded file and add Terraform to your system’s PATH. You can verify that Terraform is installed correctly by running the following command in your terminal or command prompt:
terraform -v
Step 2: Configure Terraform for Azure
Before you can use Terraform with Azure, you’ll need to set up authentication and configure Terraform to interact with your Azure account.
- Install the Azure CLI: If you haven’t already, install the Azure CLI by following the Azure CLI installation guide.
- Login to Azure: After installation, log in to your Azure account by running the following command in your terminal:
az login
- Create a Service Principal: Terraform needs a way to authenticate with Azure, so you’ll create a Service Principal to provide access. Run the following command to create one:
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/YOUR_SUBSCRIPTION_ID"
Replace YOUR_SUBSCRIPTION_ID
with your actual subscription ID. This command will return details like appId
, displayName
, and password
, which you’ll use to configure Terraform.
- Configure Terraform: Next, create a
provider
block in your Terraform configuration file to use the Azure Service Principal for authentication. Create a file namedmain.tf
and add the following code:
provider "azurerm" {
features {}
client_id = "YOUR_APP_ID"
client_secret = "YOUR_PASSWORD"
subscription_id = "YOUR_SUBSCRIPTION_ID"
tenant_id = "YOUR_TENANT_ID"
}
Replace YOUR_APP_ID
, YOUR_PASSWORD
, YOUR_SUBSCRIPTION_ID
, and YOUR_TENANT_ID
with the appropriate values.
Step 3: Create a Virtual Machine in Azure
Now that Terraform is configured, let’s create a simple virtual machine in Azure.
- Define the Resource Group: Add the following code to
main.tf
to create a resource group:
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "East US"
}
- Define the Virtual Network: Next, define a virtual network for your virtual machine:
resource "azurerm_virtual_network" "example" {
name = "example-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
- Create the Virtual Machine: Finally, add the configuration for the VM:
resource "azurerm_virtual_machine" "example" {
name = "example-vm"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
network_interface_ids = [azurerm_network_interface.example.id]
vm_size = "Standard_DS1_v2"
storage_os_disk {
name = "example-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "example-vm"
admin_username = "adminuser"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
- Apply the Configuration: Once your configuration is complete, run the following commands:
terraform init
terraform apply
Review the changes and confirm by typing yes
. Terraform will provision the virtual machine in Azure.
Conclusion
In this guide, we’ve walked through how to download, configure, and use Terraform to create a virtual machine in Microsoft Azure. By using Terraform, you can manage cloud infrastructure efficiently and consistently across multiple platforms.