为什么我不能顺利地使用 Terraform 创建 Azure VM
Why can't I create Azure VMs with Terraform Smoothly
大约 17 分钟后,我从命令提示符处收到以下错误消息。执行时间似乎也太长了?还是预期的持续时间?
azurerm_virtual_machine.vm: Still creating... (17m30s elapsed)
Releasing state lock. This may take a few moments...
Error: Error applying plan:
1 error(s) occurred:
azurerm_virtual_machine.vm: 1 error(s) occurred:
azurerm_virtual_machine.vm: unexpected EOF
Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with any
resources that successfully completed. Please address the error above
and apply again to incrementally change your infrastructure.
在门户内部检查后,它仍显示 "Creating..." 状态。我使用了下面的 .tf 文件。有什么问题吗?
provider "azurerm" {}
variable "location" {
default = "East US"
}
variable "username" {
default = "..."
}
variable "password" {
default = "..."
}
resource "azurerm_resource_group" "resourceGroup" {
name = "TerraformResearchResourceGroup"
location = "${var.location}"
}
resource "azurerm_public_ip" "publicip" {
name = "terraformresearchpublicip"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
public_ip_address_allocation = "Dynamic"
idle_timeout_in_minutes = 30
tags {
environment = "test"
}
}
resource "azurerm_virtual_network" "vnet" {
name = "terraformresearchnetwork"
address_space = ["10.0.0.0/16"]
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
}
resource "azurerm_subnet" "subnet" {
name = "terraformresearchsubnet"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "nic" {
name = "terraformresearchnic"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
ip_configuration {
name = "terraformresearchconfiguration"
subnet_id = "${azurerm_subnet.subnet.id}"
private_ip_address_allocation = "dynamic"
public_ip_address_id = "${azurerm_public_ip.publicip.id}"
}
}
resource "azurerm_storage_account" "storageacc" {
name = "terraformresearchstoacc"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
location = "${var.location}"
account_tier = "Standard"
account_replication_type = "GRS"
}
resource "azurerm_storage_container" "storagecont" {
name = "terraformresearchstoragecont"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
storage_account_name = "${azurerm_storage_account.storageacc.name}"
container_access_type = "private"
}
resource "azurerm_managed_disk" "datadisk" {
name = "terraformresearchdatadisk"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "1023"
}
resource "azurerm_virtual_machine" "vm" {
name = "terraformrvm"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
network_interface_ids = ["${azurerm_network_interface.nic.id}"]
vm_size = "Standard_A0"
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
storage_os_disk {
name = "terraformresearhosdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
storage_data_disk {
name = "${azurerm_managed_disk.datadisk.name}"
managed_disk_id = "${azurerm_managed_disk.datadisk.id}"
create_option = "Attach"
lun = 1
disk_size_gb = "${azurerm_managed_disk.datadisk.disk_size_gb}"
}
os_profile {
computer_name = "terraformrvm"
admin_username = "${var.username}"
admin_password = "${var.password}"
}
os_profile_windows_config {
enable_automatic_upgrades = false
provision_vm_agent = true
}
}
针对您的问题,我进行了测试,您的 .tf 文件中需要更改一些内容。
首先,存储账户名需要小于等于20个长度。
其次,您的虚拟机容量太小,创建虚拟机需要很长时间。如果换大一号,时间会缩短。
最后一件事是用户名和密码应该在变量中定义。我想你知道。
大约 17 分钟后,我从命令提示符处收到以下错误消息。执行时间似乎也太长了?还是预期的持续时间?
azurerm_virtual_machine.vm: Still creating... (17m30s elapsed) Releasing state lock. This may take a few moments...
Error: Error applying plan:
1 error(s) occurred:
azurerm_virtual_machine.vm: 1 error(s) occurred:
azurerm_virtual_machine.vm: unexpected EOF
Terraform does not automatically rollback in the face of errors. Instead, your Terraform state file has been partially updated with any resources that successfully completed. Please address the error above and apply again to incrementally change your infrastructure.
在门户内部检查后,它仍显示 "Creating..." 状态。我使用了下面的 .tf 文件。有什么问题吗?
provider "azurerm" {}
variable "location" {
default = "East US"
}
variable "username" {
default = "..."
}
variable "password" {
default = "..."
}
resource "azurerm_resource_group" "resourceGroup" {
name = "TerraformResearchResourceGroup"
location = "${var.location}"
}
resource "azurerm_public_ip" "publicip" {
name = "terraformresearchpublicip"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
public_ip_address_allocation = "Dynamic"
idle_timeout_in_minutes = 30
tags {
environment = "test"
}
}
resource "azurerm_virtual_network" "vnet" {
name = "terraformresearchnetwork"
address_space = ["10.0.0.0/16"]
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
}
resource "azurerm_subnet" "subnet" {
name = "terraformresearchsubnet"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "nic" {
name = "terraformresearchnic"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
ip_configuration {
name = "terraformresearchconfiguration"
subnet_id = "${azurerm_subnet.subnet.id}"
private_ip_address_allocation = "dynamic"
public_ip_address_id = "${azurerm_public_ip.publicip.id}"
}
}
resource "azurerm_storage_account" "storageacc" {
name = "terraformresearchstoacc"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
location = "${var.location}"
account_tier = "Standard"
account_replication_type = "GRS"
}
resource "azurerm_storage_container" "storagecont" {
name = "terraformresearchstoragecont"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
storage_account_name = "${azurerm_storage_account.storageacc.name}"
container_access_type = "private"
}
resource "azurerm_managed_disk" "datadisk" {
name = "terraformresearchdatadisk"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "1023"
}
resource "azurerm_virtual_machine" "vm" {
name = "terraformrvm"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
network_interface_ids = ["${azurerm_network_interface.nic.id}"]
vm_size = "Standard_A0"
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
storage_os_disk {
name = "terraformresearhosdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
storage_data_disk {
name = "${azurerm_managed_disk.datadisk.name}"
managed_disk_id = "${azurerm_managed_disk.datadisk.id}"
create_option = "Attach"
lun = 1
disk_size_gb = "${azurerm_managed_disk.datadisk.disk_size_gb}"
}
os_profile {
computer_name = "terraformrvm"
admin_username = "${var.username}"
admin_password = "${var.password}"
}
os_profile_windows_config {
enable_automatic_upgrades = false
provision_vm_agent = true
}
}
针对您的问题,我进行了测试,您的 .tf 文件中需要更改一些内容。
首先,存储账户名需要小于等于20个长度。
其次,您的虚拟机容量太小,创建虚拟机需要很长时间。如果换大一号,时间会缩短。
最后一件事是用户名和密码应该在变量中定义。我想你知道。