0
votes

Looking at the definition of a Resource here: https://www.terraform.io/docs/configuration/resources.html, you see examples like this:

resource "aws_iam_role" "example" {
  name = "example"

  # assume_role_policy is omitted for brevity in this example. See the
  # documentation for aws_iam_role for a complete example.
  assume_role_policy = "..."
}

The first example in the top line is called the "name" and can be used to reference, eg aws_iam_role.example. However, there is no discussion of 1) why there is also a key called "name" and why it is redundantly set to the same string "example". What is the purpose of this inner key name = example, and what is the best practices here?

EDIT: this seems related: https://github.com/hashicorp/terraform/issues/16394

1
The Terraform resource name and the provider resource name are two different entities.Matt Schuchard

1 Answers

0
votes

"aws_iam_role" "example" - you reference this resource in your terraform config (e.g. aws_iam_role.example, must be unique at least within one module)

name = "example" - is the name of your resource on the cloud side (must be unique on the cloud side, e.g. within one project or region, depends on the provider)

At the same time they could be different in your terraform config, for example:

resource "aws_iam_role" "my_role" {
  name = "${terraform.workspace}-example-role"

  # assume_role_policy is omitted for brevity in this example. See the
  # documentation for aws_iam_role for a complete example.
  assume_role_policy = "..."
}

PS: in this case (with aws_iam_role resource) the name argument is optional. If you skip it, you will see a randomly generated resource name in your AWS console.