Creating a Windows VM in Azure using Terraform is a great way to manage your infrastructure as code. Here's a step-by-step guide to help you get started:
Step 1: Prerequisites
Azure Subscription: Ensure you have an active Azure subscription.
Terraform: Install Terraform on your local machine.
Azure CLI: Install and configure the Azure CLI.
Step 2: Create Terraform Configuration Files
Create a directory for your Terraform configuration files and navigate to it:
mkdir terraform-windows-vm
cd terraform-windows-vm
Create a main.tf
file and add the following Terraform code:
# Provider configuration
provider "azurerm" {
features {}
}
# Resource group
resource "azurerm_resource_group" "rg" {
name = "terraform-windows-vm-rg"
location = "East US"
}
# Virtual network
resource "azurerm_virtual_network" "vnet" {
name = "terraform-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
# Subnet
resource "azurerm_subnet" "subnet" {
name = "terraform-subnet"
address_prefix = "10.0.1.0/24"
virtual_network_name = azurerm_virtual_network.vnet.name
resource_group_name = azurerm_resource_group.rg.name
}
# Public IP
resource "azurerm_public_ip" "public_ip" {
name = "terraform-public-ip"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Dynamic"
}
# Network interface
resource "azurerm_network_interface" "nic" {
name = "terraform-nic"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
}
ip_configuration {
name = "public"
subnet_id = azurerm_subnet.subnet.id
public_ip_address_id = azurerm_public_ip.public_ip.id
}
}
# Storage account
resource "azurerm_storage_account" "storage" {
name = "terraformstorage"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
account_tier = "Standard"
account_replication_type = "LRS"
}
# Virtual machine
resource "azurerm_windows_virtual_machine" "vm" {
name = "terraform-vm"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
network_interface_ids = [azurerm_network_interface.nic.id]
vm_size = "Standard_D2_v4"
storage_account_name = azurerm_storage_account.storage.name
storage_account_key = azurerm_storage_account.storage.primary_access_key
os_disk_size_gb = 30
os_disk_caching = "ReadWrite"
admin_username = "adminuser"
admin_password = "Password1234!"
computer_name = "terraform-vm"
}
Step 3: Initialize Terraform
Run the following command to initialize Terraform and download the required providers:
terraform init
Step 4: Create Execution Plan
Run the following command to create an execution plan:
terraform plan
Step 5: Apply the Execution Plan
Run the following command to apply the execution plan and create the resources in Azure:
terraform apply
Step 6: Verify the Results
Once the resources are created, you can verify the results by checking the Azure portal or using Azure CLI commands.
No comments:
Post a Comment