在 azure 中使用 terraform 创建资源组:创建后无法直接找到资源组 [英] Creating a resource group with terraform in azure: Cannot find resource group directly after creating it

查看:21
本文介绍了在 azure 中使用 terraform 创建资源组:创建后无法直接找到资源组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将在 azure 中创建一个带有 terraform 的小型 VM,但遇到了一个奇怪的问题.Terraform 创建了我的资源组,但在创建属于资源组的下一个对象(VNet)时立即失败:

I'm about to create a small VM with terraform in azure and have come across a curious problem. Terraform creates my resource group but immediately fails when creating the next object (a VNet) that is part of the resource group:

resource "azurerm_resource_group" "simple_vm" {
  name     = "simple_vm"
  location = "westeurope"
}

resource "azurerm_virtual_network" "main" {
  name                = "main"
  address_space       = ["10.0.0.0/16"]
  location            = "westeurope"
  resource_group_name = "simple_vm"
}

调用 terraform apply 会导致:

* azurerm_virtual_network.main: 1 error(s) occurred:

* azurerm_virtual_network.main: Error Creating/Updating Virtual Network "main" (Resource Group "simple_vm"): network.VirtualNetworksClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceGroupNotFound" Message="Resource group 'simple_vm' could not be found."

查看网页界面显示,资源组已创建.再次调用 terraform apply 可以正确找到它并在资源组内创建 VNet.

Looking at the web interface shows, the resource group has been created. Calling terraform apply a second time correctly finds it and creates the VNet inside the resource group.

在我看来,这看起来像是 terraform 尝试在资源组中创建对象,而它尚未在 azure 中完全实例化.我观察到公共 IP 的类似行为:我创建了一个具有公共 IP 的 VM,并包含一个 output ... 以打印 VM 的公共 IP.第一次运行时,输出为空(但没有错误消息).在 terraform refresh 之后,输出中会填充 IP.

To me this looks like terraform tries to create objects in the resource group while it is not yet fully instantiated in azure. I've observed a similar behavior with public IPs: I've created a VM with a public IP and included an output ... to print the public ip of the VM. On the first run, the output is empty (no error message though). After terraform refresh the output is filled with the IP.

我做错了吗?这是 terraform 中的错误吗?

Am I doing something wrong? Is this a bug in terraform?

推荐答案

你需要像这样创建一个隐式依赖:

You need to create an implicit dependency like this:

resource "azurerm_virtual_network" "main" {
  name                = "main"
  address_space       = ["10.0.0.0/16"]
  location            = "westeurope"
  resource_group_name = "${azurerm_resource_group.simple_vm.name}"
}

通过这种方式,Terraform 知道它需要先创建资源组,然后才能创建 vNet.

In this way, Terraform knows that it will need to create the Resource Group first before it can create the vNet.

从 Terraform 的角度来看,您的原始代码只有 2 个独立的资源.

From Terraform's perspective, your original code simply has 2 independent resources.

Terraform 的 文档 很有用.一般来说,你不想设置显式 (depends_on) 依赖项,除非你绝对必须这样做.

Terraform's documentation on this is useful. In general, you don't want to set up explicit (depends_on) dependencies unless you absolutely have to.

这篇关于在 azure 中使用 terraform 创建资源组:创建后无法直接找到资源组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆