Create a Virtual Machine
A virtual machine (VM) is a digital environment that acts like a physical computer. Azure Virtual machines can run Windows or Linux operating systems. You can use virtual machines to manage access to other services, to run resource-heavy calculations, or to host content. For this tutorial, we want a virtual machine that serves content that users can access over the internet.
To make our virtual machine accessible, we must give it a reliable online address. To do so, we create a network interface with a static public IP address. With a public IP address, we can define network security rules that allow other computers to access our virtual machine.
This page describes how to create:
- A static public IP address
- A network interface
- A Linux virtual machine
Create a static IP address
A static public IP gives your virtual machine a consistent online address. If we don’t create a static IP, our virtual machine’s address may change at any time.
- Open the
main.tffile. -
Add the following Terraform code to the bottom of the file:
resource "azurerm_public_ip" "ip4" { resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location name = "azure-web-app-ip" allocation_method = "Static" lifecycle { create_before_destroy = true } }Configuration Definition Example nameThe name of your IP address. azure-web-app-ipallocation_methodThe way Azure assigns the IP address, either StaticorDynamic.Static - Save your changes to the
main.tffile.
Create a network interface
A network interface creates a connection between a virtual machine in a virtual network and the public internet.
-
In the
main.tffile, add the following Terraform code:resource "azurerm_network_interface" "nic" { resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location name = "azure-web-app-nic" ip_configuration { name = "public" subnet_id = azurerm_subnet.subnet.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.ip4.id } }Configuration Definition Example nameThe name of your network interface. azure-web-app-nicip_configurationThe IP connection between the virtual network and your public IP address. ip_configuration {name="public"subnet_id=azurerm_subnet.subnet.idprivate_ip_address_allocation="Dynamic"public_ip_address_id=azurerm_public_ip.ip4.id}See the Terraform Registry to learn more about the
azurerm_network_interfaceresource’s available arguments.
Define the virtual machine
-
In the
main.tffile, add the following Terraform code:resource "azurerm_linux_virtual_machine" "vm" { resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location name = "azure-web-app-vm" size = "Standard_F2" admin_username = "adminuser" admin_password = "myp@sswoRD11!" disable_password_authentication = false network_interface_ids = [azurerm_network_interface.nic.id] os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" } source_image_reference { publisher = "canonical" offer = "0001-com-ubuntu-server-jammy" sku = "22_04-lts" version = "latest" } }Configuration Definition Example nameThe name of the virtual machine. azure-web-app-vmsizeThe size of the virtual machine. Standard_F2admin_usernameThe admin user’s username. adminuseradmin_passwordThe admin user’s password. myp@sswoRD11!disable_password_authenticationDisallow users to access the machine with a password. falsenetwork_interface_idsThe network interfaces that connect the machine to the virtual network. azurerm_network_interface.nic.idos_diskThe machine’s storage disk. os_disk {caching="ReadWrite"storage_account_type="Standard_LRS"}source_image_referenceThe machine’s operating system. source_image_reference {publisher="canonical"offer="0001-com-ubuntu-server-jammysku="22_04-lts"version="latest"}See the Terraform Registry to learn more about the
azurerm_linux_virtual_machineresource’s available arguments.