Gourav Singla
Gourav Singla

Reputation: 1806

Need to understand terraform resource structure

I am reading terraform and found below code:

resource "aws_vpc" "vpc_main" {
  cidr_block       = "10.0.0.0/16"

  tags {
    Name = "Main VPC"
  }
}

Here I could not understand what vpc_main stands for in the resource definition. Could somebody explain?

Upvotes: 0

Views: 56

Answers (2)

Yevgeniy Brikman
Yevgeniy Brikman

Reputation: 9391

Variable types and names in other programming languages are a good analogy. For example, in Java, you can declare a variable as follows:

String foo

The type of the variable is String and the name of the variable is foo. You must specify a name so you can (a) distinguish it from other variables of the same type and (b) refer to it later.

In Terraform, it's essentially the same idea:

resource "aws_instance" "foo" { ... }

Here, the type of the resource is aws_instance and the name is foo. You must specify a name so you can (a) distinguish it from other resources of the same type and (b) refer to it later. For example, to output the public IP address of that Instance, you could do the following:

output "public_ip" {
  value = "${aws_instance.foo.public_ip}"
}

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272822

It's a user-defined name for the resource - without this you wouldn't be able to distinguish multiple instances of the same resource type.

See the docs for more details.

Upvotes: 2

Related Questions