帮我用terraform版本123在azure上面创建一个带vnet的虚拟机vnet地址:19216810024虚拟机ip19216810100nsg名称:vm-nsgos操作系统:centos80代码完成请回复完成
以下是使用Terraform版本1.2.3在Azure上创建带VNET的虚拟机的代码:
# Provider Configuration
provider "azurerm" {
features {}
}
# Resource Group
resource "azurerm_resource_group" "rg" {
name = "my-resource-group"
location = "East US"
}
# Virtual Network
resource "azurerm_virtual_network" "vnet" {
name = "my-vnet"
address_space = ["192.168.10.0/24"]
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
}
# Subnet
resource "azurerm_subnet" "subnet" {
name = "my-subnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefix = "192.168.10.0/24"
}
# Network Security Group
resource "azurerm_network_security_group" "nsg" {
name = "vm-nsg"
resource_group_name = azurerm_resource_group.rg.name
security_rule {
name = "allow_ssh"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
# Network Interface
resource "azurerm_network_interface" "nic" {
name = "my-nic"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "my-nic-ip"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Static"
private_ip_address = "192.168.10.100"
}
}
# Virtual Machine
resource "azurerm_virtual_machine" "vm" {
name = "my-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_B2ms"
storage_os_disk {
name = "my-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
storage_image_reference {
publisher = "OpenLogic"
offer = "CentOS"
sku = "8_0"
version = "latest"
}
os_profile {
computer_name = "my-vm"
admin_username = "adminuser"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
boot_diagnostics {
enabled = true
storage_uri = "https://mystorageaccount.blob.core.windows.net/"
}
}
请注意,此代码假定您已经设置了Azure凭据并将其配置为Terraform提供程序。如果您需要帮助设置Azure凭据,请参阅Terraform文档。
原文地址: https://www.cveoy.top/t/topic/bibb 著作权归作者所有。请勿转载和采集!