使用Terraform在Azure上创建虚拟机

在本文中,将介绍如何从头开始使用Terraform在Azure上创建虚拟机。将从创建Terraform文件开始,然后定义Azure资源组、虚拟网络、子网、公共IP地址、网络接口,最后定义虚拟机本身。这个过程将展示Terraform的强大功能,以及它如何简化云基础设施的部署和管理。

创建Terraform文件

首先,需要创建一个名为main.tfTerraform配置文件。这个文件将包含所有的资源定义和配置。

main.tf文件中,需要定义Azure提供者。这将允许Terraform与Azure进行通信并管理资源。

provider "azurerm" { version = "=2.0.0" features {} }

接下来,需要定义一个资源组,所有的资源都将在这个资源组中创建。

resource "azurerm_resource_group" "rg" { name = "my-first-terraform-rg" location = "northeurope" }

定义虚拟网络和子网是创建虚拟机的重要步骤,因为它们定义了虚拟机的网络环境。

resource "azurerm_virtual_network" "myvnet" { name = "my-vnet" address_space = ["10.0.0.0/16"] location = "northeurope" resource_group_name = azurerm_resource_group.rg.name } resource "azurerm_subnet" "frontendsubnet" { name = "frontendSubnet" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.myvnet.name address_prefix = "10.0.1.0/24" }

公共IP地址将允许虚拟机与互联网通信。

resource "azurerm_public_ip" "myvm1publicip" { name = "pip1" location = "northeurope" resource_group_name = azurerm_resource_group.rg.name allocation_method = "Dynamic" sku = "Basic" }

网络接口是虚拟机与虚拟网络之间的连接点。

resource "azurerm_network_interface" "myvm1nic" { name = "myvm1-nic" location = "northeurope" resource_group_name = azurerm_resource_group.rg.name ip_configuration { name = "ipconfig1" subnet_id = azurerm_subnet.frontendsubnet.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.myvm1publicip.id } }

最后,需要定义虚拟机本身。这包括虚拟机的大小、操作系统、存储等配置。

resource "azurerm_windows_virtual_machine" "example" { name = "myvm1" location = "northeurope" resource_group_name = azurerm_resource_group.rg.name network_interface_ids = [azurerm_network_interface.myvm1nic.id] size = "Standard_B1s" admin_username = "adminuser" admin_password = "Password123!" source_image_reference { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2019-Datacenter" version = "latest" } os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" } }

构建虚拟机

在定义了所有资源之后,需要登录到Azure并初始化Terraform,然后应用配置文件来创建虚拟机。

首先,使用AzureCLI登录到Azure:

az login

然后,初始化Terraform

terraform init

最后,应用配置文件:

terraform apply

这个过程可能需要几分钟时间,取决于资源的创建速度。

完整的配置文件

provider "azurerm" { version = "=2.0.0" features {} } resource "azurerm_resource_group" "rg" { name = "my-first-terraform-rg" location = "northeurope" } resource "azurerm_virtual_network" "myvnet" { name = "my-vnet" address_space = ["10.0.0.0/16"] location = "northeurope" resource_group_name = azurerm_resource_group.rg.name } resource "azurerm_subnet" "frontendsubnet" { name = "frontendSubnet" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.myvnet.name address_prefix = "10.0.1.0/24" } resource "azurerm_public_ip" "myvm1publicip" { name = "pip1" location = "northeurope" resource_group_name = azurerm_resource_group.rg.name allocation_method = "Dynamic" sku = "Basic" } resource "azurerm_network_interface" "myvm1nic" { name = "myvm1-nic" location = "northeurope" resource_group_name = azurerm_resource_group.rg.name ip_configuration { name = "ipconfig1" subnet_id = azurerm_subnet.frontendsubnet.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.myvm1publicip.id } } resource "azurerm_windows_virtual_machine" "example" { name = "myvm1" location = "northeurope" resource_group_name = azurerm_resource_group.rg.name network_interface_ids = [azurerm_network_interface.myvm1nic.id] size = "Standard_B1s" admin_username = "adminuser" admin_password = "Password123!" source_image_reference { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2019-Datacenter" version = "latest" } os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" } }
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485