Terraform [SOLVED]: Why do ressources have a name and a "name" attribute?

Terraform [SOLVED]: Why do ressources have a name and a "name" attribute?

Home Forums Automation Tools Terraform Terraform [SOLVED]: Why do ressources have a name and a "name" attribute?

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #205610

    Cloudy Point
    Keymaster

    QuestionQuestion

    I am new to terraform and trying to create some resources on azure. To me it looks like there is some unneccessary 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}"
    }
    

    #205611

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

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

    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.

    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:

    • If there are multiple separate teams or applications sharing an AzureRM account, the name used with the API may need to be prefixed to avoid collisions with names created by other teams or applications, while the local name needs to be unique only within the module where it’s defined.
    • In more complex usage with child modules, it’s common to instantiate the same child module multiple times. In this case, the local name will be the same between all of the instances (because it’s significant only within that instance) but the name used with the API will need to be adjusted for each instances so that they don’t collide.

    Source: https://stackoverflow.com/questions/46716154/why-do-ressources-have-a-name-and-a-name-attribute
    Author: Martin Atkins
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.