Introduction
This article will focus on deploying an Azure Windows virtual machine using Terraform as infrastructure as code.
In this article, you will learn how to set up your Azure account, configure your Terraform environment, write Terraform code to define your VM and associated resources, and deploy your VM to Azure using Terraform.
Prerequisites
- Azure subscription
- Install and configure Terraform
- Visual Studio Code
Solution Diagram
Step 1. Create a file directory in your local location.
Step 2. Create a named Provider.tf file.
Step 3. Insert the following code in Provide.tf file.
provider "azurerm" {
features {}
}
Step 4. In Main.tf file use the following code to create a resource group (RG).
resource "azurerm_resource_group" "RG" {
name = "Test-RG"
location = "East US"
}
Step 5. In Main.tf file use the following code to create a Virtual Network and Subnet.
resource "azurerm_virtual_network" "VNet" {
name = "Test-VNet"
address_space = ["10.99.0.0/16"]
location = azurerm_resource_group.RG.location
resource_group_name = azurerm_resource_group.RG.name
}
resource "azurerm_subnet" "AzureSubnet" {
name = "TestSubnet"
resource_group_name = azurerm_resource_group.RG.name
virtual_network_name = azurerm_virtual_network.VNet.name
address_prefixes = ["10.99.2.0/24"]
}
Note: As per your requirement you can choose the IP address space.
Step 6. Create a Network Interface Card (nic) using the following code.
resource "azurerm_network_interface" "nic" {
name = "test-nic"
location = azurerm_resource_group.RG.location
resource_group_name = azurerm_resource_group.RG.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.TestSubnet.id
private_ip_address_allocation = "Dynamic"
}
}
Step 7. Create a Public IP address for VM access.
resource "azurerm_public_ip" "PubIP" {
name = "Test-PIP"
resource_group_name = azurerm_resource_group.RG.name
location = azurerm_resource_group.RG.location
allocation_method = "Static"
}
Step 8. Create a Network Security Group (NSG) for securing your VM.
resource "azurerm_network_security_group" "NSG" {
name = "Test-NSG"
location = azurerm_resource_group.RG.location
resource_group_name = azurerm_resource_group.RG.name
}
Step 9. Create a Windows Virtual Machine.
resource "azurerm_windows_virtual_machine" "VM" {
name = "Shanuka-VM"
resource_group_name = azurerm_resource_group.RG.name
location = azurerm_resource_group.RG.location
size = "Standard_B2ms"
admin_username = "azureuser"
admin_password = "P@$$w0rd1234!"
network_interface_ids = [
azurerm_network_interface.nic.id,
]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2022-Datacenter"
version = "latest"
}
}
Note: Before running this configuration file, make sure you have the Azure CLI installed and have authenticated using the az login command. You'll also need to replace the placeholders for subscription_id, client_id, client_secret, and tenant_id with your own values.
Once you have done the az login command automatically you will get the Microsoft Azure sign-in page.
Key commands in Terraform
- Terraform init – Initialize a terraform working directory. Example: terraform init
- Terraform plan – Generate and show an execution plan. Example: terraform plan
- terraform apply – Build or change infrastructure. Example: terraform apply
Step 1. In Terminal type terraform plan command
Step 2. Open the terminal and type terraform apply -auto-approve command.
Step 3. Once deployment is done, you can verify your resource from the Azure portal.
Conclusion
This article taught us how to deploy Azure virtual machines using Terraform code Infrastructure as Code (IaC). If you have any questions, don't hesitate to comment below.
0 Comments