How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

huangapple go评论80阅读模式
英文:

How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

问题

我刚刚开始在Terraform中编写代码,并且正在进行基础工作。我在任何存储库中找到的每段代码都要求在Azure中部署一个带有负载均衡、可用性集和存储以启用引导诊断的虚拟机。

是否有简单的语法来禁用它们?

我尝试在这些资源前面添加“disabled”,但没有起作用。

英文:

I have just started coding in Terraform and doing the basics. Every code that I find in any repo asks comes with deploying a VM in Azure with LoadBalance, Availability Set and Storage to enable Boot Diagnostic.

Is there any simple syntax to disabled them ?

I tried to add "disabled" in front of those resource but that didnt work.

答案1

得分: 0

请尝试使用以下代码:

## Public_IP - VM1 NIC
resource "azurerm_public_ip" "public_VM1" {
  name                = "public_VM1"
  resource_group_name = 
  location            = 
  allocation_method   = "Static"

  tags = {
    environment = "Migration-Lab"
  }
}

## NIC VM1
resource "azurerm_network_interface" "VM1" {
  name                = "VM1"
  location            = 
  resource_group_name = 

  ip_configuration {
    name                          = "VM1"
    subnet_id                     = 
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = 
  }
}

## Create a Linux VM1 in VNET1
resource "azurerm_linux_virtual_machine" "LinuxVM1" {
  name                = "LinuxVM1"
  resource_group_name = 
  location            = 
  size                = "Standard_B1ms"
  admin_username      = "azureuser"
  network_interface_ids = [azurerm_network_interface.VM1.id]
  computer_name                   = "LinuxVM1"
  disable_password_authentication = true

  admin_ssh_key {
    username   = "azureuser"
    public_key = 
  }

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = var.ubuntu_version
    version   = "latest"
  }
}

填写缺失的数值。

这应该适用于测试环境。

祝好,Nico
英文:

try using the following code:

## Public_IP - VM1 NIC
    resource "azurerm_public_ip" "public_VM1" {
    name                = "public_VM1"
    resource_group_name = 
    location            = 
    allocation_method   = "Static"

    tags = {
    environment = "Migration-Lab"
    }
   }




## NIC VM1
   resource "azurerm_network_interface" "VM1" {
   name                = "VM1"
  location            = 
  resource_group_name = 

  ip_configuration {
    name                          = "VM1"
    subnet_id                     = 
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = 
  }
}

## Create a Linux VM1 in VNET1
resource "azurerm_linux_virtual_machine" "LinuxVM1" {
  name                = "LinuxVM1"
  resource_group_name = 
  location            = 
  size                = "Standard_B1ms"
  admin_username      = "azureuser"
  network_interface_ids = [azurerm_network_interface.VM1.id]
  computer_name                   = "LinuxVM1"
  disable_password_authentication = true

 admin_ssh_key {
    username   = "azureuser"
    public_key = 
  }

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = var.ubuntu_version
    version   = "latest"
  }
}

filling the missing values.

It should be fine for a test environment

Cheers,
Nico

答案2

得分: 0

如何在Azure中使用Terraform部署虚拟机,不包括负载均衡器、可用性集和启动诊断存储

我已经使用Terraform创建了Azure虚拟机,没有包括负载均衡器、可用性集和启动诊断存储。

Terraform 代码:

provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "venkatrg" {
name = "Venkat-resource-group"
location = "East US"
}
resource "azurerm_virtual_network" "vnet" {
name = "venkat-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.venkatrg.location
resource_group_name = azurerm_resource_group.venkatrg.name
}
resource "azurerm_subnet" "subnet" {
name = "vm-subnet"
resource_group_name = azurerm_resource_group.venkatrg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_network_interface" "samplenic" {
name = "VM-nic"
location = azurerm_resource_group.venkatrg.location
resource_group_name = azurerm_resource_group.venkatrg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_windows_virtual_machine" "myvm" {
name = "venkat-vm"
location = azurerm_resource_group.venkatrg.location
resource_group_name = azurerm_resource_group.venkatrg.name
network_interface_ids = [azurerm_network_interface.samplenic.id]
size = "Standard_B1ls"
admin_username = "Venkat"
admin_password = "Password1234!"
os_disk {
name = "VM-osdisk"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
}

Terraform 应用:

How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

一旦运行了上述代码,Azure虚拟机成功创建,不包括可用性集、启动诊断和负载均衡器。

How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

启动诊断

要检查VM中是否启用了启动诊断,您可以按照以下步骤进行操作。

Azure 门户 > 虚拟机 > 选择您的虚拟机 > 启动诊断

How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

要检查VM是否与负载均衡器关联,您可以使用以下命令。

az network nic show-effective-route-table -g "Venkat-resource-group" -n "VM-nic" --query "value[].{Name:virtualMachine.name, LoadBalancer:ipConfigurations[].loadBalancerBackendAddressPools[].id}"

输出
How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

英文:

> How to deploy VM through Terraform in Azure without Loadbalancer,availability set and without storage for boot diagnostic

I have created Azure VM using Terraform without Loadbalancer, Availability Set and without storage for boot diagnostic

Terraform code:

provider  "azurerm"  {
features  {}
}
resource  "azurerm_resource_group"  "venkatrg"  {
name = "Venkat-resource-group"
location = "East US"
}
resource  "azurerm_virtual_network"  "vnet"  {
name = "venkat-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.venkatrg.location
resource_group_name = azurerm_resource_group.venkatrg.name
}
resource  "azurerm_subnet"  "subnet"  {
name = "vm-subnet"
resource_group_name = azurerm_resource_group.venkatrg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
resource  "azurerm_network_interface"  "samplenic"  {
name = "VM-nic"
location = azurerm_resource_group.venkatrg.location
resource_group_name = azurerm_resource_group.venkatrg.name
ip_configuration  {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
}
}
resource  "azurerm_windows_virtual_machine"  "myvm"  {
name = "venkat-vm"
location = azurerm_resource_group.venkatrg.location
resource_group_name = azurerm_resource_group.venkatrg.name
network_interface_ids = [azurerm_network_interface.samplenic.id]
size = "Standard_B1ls"
admin_username = "Venkat"
admin_password = "Password1234!"
os_disk  {
name = "VM-osdisk"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference  {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
}

Terraform apply:

How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

Once ran the above code,Azure VM got created without Availability Set , Boot diagnostic and LoadBalancer successfully.

How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

Boot diagnostic

To check the boot diagnostic is enabled in VM, you can follow the below steps.

Azure Portal > Virtual Machines > Select your VM > Boot diagnostic

How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

To check the Loadbalancer is associated with VM, you can use the below command.

az network nic show-effective-route-table -g "Venkat-resource-group" -n "VM-nic" --query "value[].{Name:virtualMachine.name, LoadBalancer:ipConfigurations[].loadBalancerBackendAddressPools[].id}"

Output
How to deploy VM through Terraform in Azure without Loadbalancer, availability set and without storage for boot diagnostic

huangapple
  • 本文由 发表于 2023年4月11日 15:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定