gpt4 book ai didi

Azure 终结点 'resourceTargetId' 的 'vm1-TF' 属性无效或丢失

转载 作者:行者123 更新时间:2023-12-03 02:39:12 26 4
gpt4 key购买 nike

我想在来自不同位置(西欧和北欧)的两个虚拟机之间在 Terraform 中实现流量管理器。我附上了我的代码,但我不知道如何为每个虚拟机配置“target_resource_id”,因为虚拟机是在 for 循环中创建的,网络也是如此。如果第一个虚拟机出现故障,流量管理器将切换到辅助虚拟机。有什么想法吗?

我的代码:

variable "subscription_id" {}
variable "tenant_id" {}
variable "environment" {}
variable "azurerm_resource_group_name" {}
variable "locations" {
type = map(string)
default = {
vm1 = "North Europe"
vm2 = "West Europe"
}
}

# Configure the Azure Provider
provider "azurerm" {
subscription_id = var.subscription_id
tenant_id = var.tenant_id
version = "=2.10.0"
features {}
}

resource "azurerm_virtual_network" "main" {
for_each = var.locations
name = "${each.key}-network"
address_space = ["10.0.0.0/16"]
location = each.value
resource_group_name = var.azurerm_resource_group_name
}

resource "azurerm_subnet" "internal" {
for_each = var.locations
name = "${each.key}-subnet"
resource_group_name = var.azurerm_resource_group_name
virtual_network_name = azurerm_virtual_network.main[each.key].name
address_prefixes = ["10.0.2.0/24"]
}

resource "azurerm_public_ip" "example" {
for_each = var.locations
name = "${each.key}-pip"
location = each.value
resource_group_name = var.azurerm_resource_group_name
allocation_method = "Static"
idle_timeout_in_minutes = 30

tags = {
environment = "dev01"
}
}

resource "azurerm_network_interface" "main" {
for_each = var.locations
name = "${each.key}-nic"
location = each.value
resource_group_name = var.azurerm_resource_group_name

ip_configuration {
name = "testconfiguration1"
subnet_id = azurerm_subnet.internal[each.key].id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.example[each.key].id
}
}

resource "random_password" "password" {
length = 16
special = true
override_special = "_%@"
}

resource "azurerm_virtual_machine" "main" {
for_each = var.locations
name = "${each.key}t-vm"
location = each.value
resource_group_name = var.azurerm_resource_group_name
network_interface_ids = [azurerm_network_interface.main[each.key].id]
vm_size = "Standard_D2s_v3"


storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
storage_os_disk {
name = "${each.key}-myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "${each.key}-hostname"
admin_username = "testadmin"
admin_password = random_password.password.result
}
os_profile_linux_config {
disable_password_authentication = false
}
tags = {
environment = "dev01"
}

}

resource "random_id" "server" {
keepers = {
azi_id = 1
}

byte_length = 8
}

resource "azurerm_traffic_manager_profile" "example" {
name = random_id.server.hex
resource_group_name = var.azurerm_resource_group_name
traffic_routing_method = "Priority"

dns_config {
relative_name = random_id.server.hex
ttl = 100
}

monitor_config {
protocol = "http"
port = 80
path = "/"
interval_in_seconds = 30
timeout_in_seconds = 9
tolerated_number_of_failures = 3
}

tags = {
environment = "dev01"
}
}

resource "azurerm_traffic_manager_endpoint" "first-vm" {
for_each = var.locations
name = "${each.key}-TF"
resource_group_name = var.azurerm_resource_group_name
profile_name = "${azurerm_traffic_manager_profile.example.name}"
target_resource_id = "[azurerm_network_interface.main[each.key].id]"
type = "azureEndpoints"
priority = "${[each.key] == "vm1" ? 1 : 2}"
}

我的错误:

Error: trafficmanager.EndpointsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. 
Status=400 Code="BadRequest" Message="The 'resourceTargetId' property of endpoint 'vm1-TF' is invalid or missing.
The property must be specified only for the following endpoint types: AzureEndpoints, NestedEndpoints.
You must have read access to the resource to which it refers."

最佳答案

根据这个documentation :

Azure 终结点用于流量管理器中基于 Azure 的服务。支持以下 Azure 资源类型:

  1. PaaS 云服务
  2. 网络应用
  3. 网络应用程序插槽
  4. PublicIPAddress 资源(可以直接或通过 Azure 负载均衡器连接到 VM)。 publicIpAddress 必须分配一个 DNS 名称才能在流量管理器配置文件中使用。

在代码中,您需要将Target ResourceId更改为PublicIPAddress。您不应传递网络接口(interface) ID。

resource "azurerm_traffic_manager_endpoint" "first-vm" {
for_each = var.locations
name = "${each.key}-TF"
resource_group_name = var.azurerm_resource_group_name
profile_name = "${azurerm_traffic_manager_profile.example.name}"
target_resource_id = azurerm_public_ip.example[each.key].id
type = "azureEndpoints"
priority = "${[each.key] == "vm1" ? 1 : 2}"
}

请注意:publicIpAddress 必须分配一个 DNS 名称才能在流量管理器配置文件中使用。

您也可以查看这个ARM Template这相当于这个 terraform 模板。

希望这有帮助!

关于Azure 终结点 'resourceTargetId' 的 'vm1-TF' 属性无效或丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61930952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com