Description: Here I have explained, How to create a Virtual Machine in Azure using Terraform. I have broken the whole procedure into multiple sections as follow:
- Create Terraform File
- Define Variables
- Create AzureRM provider in terraform
- Define Virtual Network and Subnet
- Define New Public IP address
- Define Network Interface for VM
- Define Virtual Machine
- All define in one File
- Build VM using terraform
- Result after Deployment
1. Create Terraform File: Create one terraform file with name main.tf
2. Define Variables: Define variables in terraform file
variable "storage_account_name" { type=string default="serverstorage" } variable "network_name" { type=string default="testnetwork" } variable "vm_name" { type=string default="ServerTechVM" }
provider "azurerm"{ version = "=2.0" subscription_id = "XXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" tenant_id = "XXXX-XXXX-XXXXX-XXXX-XXXX-XXXXX-XXX" features {} }
4. Define Virtual Network and Subnet
resource "azurerm_virtual_network" "staging" { name = var.network_name address_space = ["10.0.0.0/16"] location = "East US" resource_group_name = "DevOPS" } resource "azurerm_subnet" "default" { name = "default" resource_group_name = "DevOPS" virtual_network_name = azurerm_virtual_network.staging.name address_prefix = "10.0.0.0/24" }
5. Define the Public IP address
resource "azurerm_public_ip" "myvm1publicip" { name = "pip1" location = "East US" resource_group_name = "DevOPS" allocation_method = "Dynamic" sku = "Basic" }
6. Define Network Interface for VM
resource "azurerm_network_interface" "interface" { name = "default-interface" location = "East US" resource_group_name = "DevOPS" ip_configuration { name = "interfaceconfiguration" subnet_id = azurerm_subnet.default.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.myvm1publicip.id } }
7. Define Virtual Machine
resource "azurerm_virtual_machine" "vm" { name = var.vm_name location = "East US" resource_group_name = "DevOPS" network_interface_ids = [azurerm_network_interface.interface.id] vm_size = "Standard_DS1_v2" storage_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "18.04-LTS" version = "latest" } storage_os_disk { name = "osdisk1" caching = "ReadWrite" create_option = "FromImage" managed_disk_type = "Standard_LRS" } os_profile { computer_name = "ServerTechVM" admin_username = "serverapprunner" admin_password = "P@ssw0rd123" } os_profile_linux_config { disable_password_authentication = false }
8. All define in one File:
variable "storage_account_name" { type=string default="serverstorage" } variable "network_name" { type=string default="testnetwork" } variable "vm_name" { type=string default="ServerTechVM" } provider "azurerm"{ version = "=2.0" subscription_id = "XXXX-XXXXX-XXXXX-XXXXX-XXXXXX" tenant_id = "XXX-XXXXX-XXXXX-XXXXXX-XXXXX" features {} } resource "azurerm_virtual_network" "staging" { name = var.network_name address_space = ["10.0.0.0/16"] location = "East US" resource_group_name = "DevOPS" } resource "azurerm_subnet" "default" { name = "default" resource_group_name = "DevOPS" virtual_network_name = azurerm_virtual_network.staging.name address_prefix = "10.0.0.0/24" } resource "azurerm_public_ip" "myvm1publicip" { name = "pip1" location = "East US" resource_group_name = "DevOPS" allocation_method = "Dynamic" sku = "Basic" } resource "azurerm_network_interface" "interface" { name = "default-interface" location = "East US" resource_group_name = "DevOPS" ip_configuration { name = "interfaceconfiguration" subnet_id = azurerm_subnet.default.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.myvm1publicip.id } } resource "azurerm_virtual_machine" "vm" { name = var.vm_name location = "East US" resource_group_name = "DevOPS" network_interface_ids = [azurerm_network_interface.interface.id] vm_size = "Standard_DS1_v2" storage_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "18.04-LTS" version = "latest" } storage_os_disk { name = "osdisk1" caching = "ReadWrite" create_option = "FromImage" managed_disk_type = "Standard_LRS" } os_profile { computer_name = "HarryVM" admin_username = "serverapprunner" admin_password = "P@ssw0rd123" } os_profile_linux_config { disable_password_authentication = false } }
9. Build Virtual Machine using Terraform
Upload the main.tf file to Azure Cloud Shell as mentioned in the previous blog
Run terraform plan command to create a plan
Run terraform apply to run the project
No comments:
Post a Comment