为什么有些资源有名称和“名称"?属性? [英] Why do some resources have a name and a "name" attribute?

查看:15
本文介绍了为什么有些资源有名称和“名称"?属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Terraform 的新手,正在尝试在 Azure 上创建一些资源.在我看来,资源名称和定义中的属性 name 之间似乎存在一些不必要的重复.

I am new to Terraform and trying to create some resources on Azure. To me it looks like there is some unnecessary duplication between the resource name and the attribute name in the definitions.

resource "azurerm_resource_group" "group_name" {
  name     = "group_name" # <-- repeated!
  location = "${local.location}"
}

有区别吗?我可以以某种方式将它们设置为相同的精神吗:

Is there a difference? Can I somehow set them to be the same in the spirit of this:

resource "azurerm_resource_group" "group_name" {
  name     = "${name}"
  location = "${local.location}"
}

推荐答案

这里的两个名字用途不同,作用域也不同.

The two names here serve different purposes and have different scopes.

块头中出现的名称是在单个 Terraform 模块中使用的本地名称.在将结果从一个资源插入到另一个资源时很有用,例如 ${azurerm_resource_group.group_name}.远程 API 永远不会看到这个名称;它仅用于内部引用.

The name that appears in the block header is a local name used within a single Terraform module. It is useful when interpolating results from one resource into another, like ${azurerm_resource_group.group_name}. The remote API never sees this name; it is used only for internal references.

name within 块是特定于资源类型本身的属性——在本例中为 azurerm_resource_group.此名称将被发送到远程 API,并且将成为 AzureRM 系统本身中描述对象的方式.

The name within the block is an attribute specific to the resource type itself -- azurerm_resource_group in this case. This name will be sent to the remote API and will be how the object is described within the AzureRM system itself.

在小型组织的简单配置中,这两个名称确实有可能匹配.在实践中,这些名称之间的范围差异导致它们经常变化.例如:

In simple configurations within small organizations it is indeed possible that both of these names could match. In practice, the difference in scope between these names causes them to often vary. For example:

  • 如果有多个单独的团队或应用程序共享一个 AzureRM 帐户,则与 API 一起使用的名称可能需要加上前缀以避免与其他团队或应用程序创建的名称发生冲突,而本地名称只需在定义它的模块.
  • 在子模块的更复杂用法中,通常会多次实例化同一个子模块.在这种情况下,所有实例之间的本地名称将是相同的(因为它仅在该实例中很重要),但与 API 一起使用的名称需要针对每个实例进行调整,以免它们发生冲突.

这篇关于为什么有些资源有名称和“名称"?属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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