简体   繁体   English

使用 Terraform 在 Azure 中创建完整的 Linux 虚拟机基础架构

[英]Create a complete Linux virtual machine infrastructure in Azure with Terraform

So I'm very new creating virtual machines and Microsoft Azure.所以我是创建虚拟机和 Microsoft Azure 的新手。 I'm trying to create one in terraform and link to my Azure account.我正在尝试在 terraform 中创建一个并链接到我的 Azure 帐户。 I have been following this documentation: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-create-complete-vm .我一直在关注这个文档: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-create-complete-vm For some background;对于一些背景; my goal is to create the VM, log in via ssh on a vpn and log the logins我的目标是创建虚拟机,在 vpn 上通过 ssh 登录并记录登录信息

Using the sample code they provide however, I get this error:但是,使用他们提供的示例代码,我得到了这个错误:

azurerm_virtual_machine.myterraformvm: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The value of parameter linuxConfiguration.ssh.publicKeys.keyData is invalid." Target="linuxConfiguration.ssh.publicKeys.keyData"

This is my code: terraform_azure.tf这是我的代码: terraform_azure.tf

# Configure the Microsoft Azure Provider
provider "azurerm" {
  tenant_id       = "myid"
  client_id       = "myclientid"
  client_secret   = "mysecret"
  subscription_id = "mysubscr"
}

# Create a resource group if it doesn’t exist
resource "azurerm_resource_group" "myterraformgroup" {
  name     = "myResourceGroup"
  location = "eastus"

  tags {
    environment = "Terraform Demo"
  }
}

# Create virtual network
resource "azurerm_virtual_network" "myterraformnetwork" {
  name                = "myVnet"
  address_space       = ["10.0.0.0/16"]
  location            = "eastus"
  resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"

  tags {
    environment = "Terraform Demo"
  }
}

# Create subnet
resource "azurerm_subnet" "myterraformsubnet" {
  name                 = "mySubnet"
  resource_group_name  = "${azurerm_resource_group.myterraformgroup.name}"
  virtual_network_name = "${azurerm_virtual_network.myterraformnetwork.name}"
  address_prefix       = "10.0.1.0/24"
}

# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
  name                = "myPublicIP"
  location            = "eastus"
  resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"
  allocation_method   = "Dynamic"

  tags {
    environment = "Terraform Demo"
  }
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "myterraformnsg" {
  name                = "myNetworkSecurityGroup"
  location            = "eastus"
  resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"

  security_rule {
    name                       = "SSH"
    priority                   = 1001
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }

  tags {
    environment = "Terraform Demo"
  }
}

# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
  name                      = "myNIC"
  location                  = "eastus"
  resource_group_name       = "${azurerm_resource_group.myterraformgroup.name}"
  network_security_group_id = "${azurerm_network_security_group.myterraformnsg.id}"

  ip_configuration {
    name                          = "myNicConfiguration"
    subnet_id                     = "${azurerm_subnet.myterraformsubnet.id}"
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = "${azurerm_public_ip.myterraformpublicip.id}"
  }

  tags {
    environment = "Terraform Demo"
  }
}

# Generate random text for a unique storage account name
resource "random_id" "randomId" {
  keepers = {
    # Generate a new ID only when a new resource group is defined
    resource_group = "${azurerm_resource_group.myterraformgroup.name}"
  }

  byte_length = 8
}

# Create storage account for boot diagnostics
resource "azurerm_storage_account" "mystorageaccount" {
  name                     = "diag${random_id.randomId.hex}"
  resource_group_name      = "${azurerm_resource_group.myterraformgroup.name}"
  location                 = "eastus"
  account_tier             = "Standard"
  account_replication_type = "LRS"

  tags {
    environment = "Terraform Demo"
  }
}

# Create virtual machine
resource "azurerm_virtual_machine" "myterraformvm" {
  name                  = "myVM"
  location              = "eastus"
  resource_group_name   = "${azurerm_resource_group.myterraformgroup.name}"
  network_interface_ids = ["${azurerm_network_interface.myterraformnic.id}"]
  vm_size               = "Standard_DS1_v2"

  storage_os_disk {
    name              = "myOsDisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
  }

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04.0-LTS"
    version   = "latest"
  }

  os_profile {
    computer_name  = "myvm"
    admin_username = "azureuser"
  }

  os_profile_linux_config {
    disable_password_authentication = true

    ssh_keys {
      path     = "/home/azureuser/.ssh/authorized_keys"
      key_data = "ssh-rsa AAAAB3Nz{snip}hwhqT9h"
    }
  }

  boot_diagnostics {
    enabled     = "true"
    storage_uri = "${azurerm_storage_account.mystorageaccount.primary_blob_endpoint}"
  }

  tags {
    environment = "Terraform Demo"
  }
}

I'm wondering why this is.我想知道这是为什么。 In the error The value of parameter linuxConfiguration.ssh.publicKeys.keyData is invalid says something about my public key?在错误The value of parameter linuxConfiguration.ssh.publicKeys.keyData is invalid说明了我的公钥? Where am I/should I be setting this?我/应该在哪里设置这个? Thanks谢谢

you need to pass in the key content, not path to the key: 您需要传递密钥内容,而不是密钥的路径:

NOTE: Rather than defining this in-line you can source this from a local file using the file interpolation function - for example key_data = "${file("~/.ssh/id_rsa.pub")}". 注意:您可以使用文件插值功能从本地文件中获取该信息,而不是内联定义-例如key_data =“ $ {file(”〜/ .ssh / id_rsa.pub“)}”。

https://www.terraform.io/docs/providers/azurerm/r/virtual_machine.html#key_data https://www.terraform.io/docs/providers/azurerm/r/virtual_machine.html#key_data

Had the same error, In my case I was creating 3 VMs using terraform.有同样的错误,在我的例子中,我使用 terraform 创建了 3 个虚拟机。 and I fixed it by changing the admin_username in the azurerm_linux_virtual_machine我通过更改azurerm_linux_virtual_machine中的admin_username来修复它

and the username in the admin_ssh_keyadmin_ssh_key中的用户名

here is my case ()这是我的情况()

username         = "${var.username}${count.index}"

admin_username   = "${var.username}${count.index}"

the use of variables is because I'm using the count to make more than 1 vm.变量的使用是因为我使用计数来制作超过 1 个 vm。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Azure 中的 terraform 创建 UIPathRobot 虚拟机? - How to create a UIPathRobot Virtual Machine using terraform in Azure? 创建Azure虚拟机 - Create Azure Virtual Machine Terraform - 从快照启动 Azure 虚拟机 - Terraform - launching an Azure virtual machine from a snapshot 使用 Terraform 导入 Azure Windows 虚拟机时出错 - Error importing Azure Windows Virtual Machine with Terraform Terraform Azure 提供程序 - 只能使用 azurerm_linux_virtual_machine 加密操作系统磁盘吗? - Terraform Azure provider - can OS disk be encrypted only with azurerm_linux_virtual_machine? 使用terraform脚本在azure上旋转linux vm的azurerm_image.source_virtual_machine_id应该是什么? - What should be azurerm_image.source_virtual_machine_id to spun linux vm on azure using terraform script? Azure 虚拟机扩展文件Uris 路径与Terraform - Azure Virtual Machine Extension fileUris path with Terraform 如何使用Terraform在Azure虚拟机上创建SSH密钥? - How do I create a ssh key on a azure virtual machine using terraform? 有没有办法使用 terraform 为 azure 日志分析中的虚拟机、存储帐户资源创建警报 - is there a way to create alerts for virtual machine, storage account resources in azure log analytics using terraform 在巴西创建 azure 虚拟机 - Create azure virtual machine on Brazil
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM