dushkin
dushkin

Reputation: 2101

Terraform .id explanation

I'm new to Terraform and am confused by the id attribute returned by resources.

Given:

data "azurerm_subnet" "aas_mng_subnet" {
  name                 = "${var.prefix}-${var.env_type}-subnet-${var.site_octet}.50.x"
  virtual_network_name = "${data.azurerm_virtual_network.main.name}"
  resource_group_name  = "${var.prefix}-${var.env_type}"
}

and

subnet_id = "${data.azurerm_subnet.aas_mng_subnet.id}"

What will the subnet_id value be? I mean in general - not specific to this example. What is the value of the .id attribute in every place it appears?

The Terraform documentation attempts to explain this but I'm not sure I understand it still:

Attributes of a data source

The syntax is data.TYPE.NAME.ATTRIBUTE. For example. ${data.aws_ami.ubuntu.id} will interpolate the id attribute from the aws_ami data source named ubuntu. If the data source has a count attribute set, you can access individual attributes with a zero-based index, such as ${data.aws_subnet.example.0.cidr_block}. You can also use the splat syntax to get a list of all the attributes: ${data.aws_subnet.example.*.cidr_block}.

I'll be grateful if someone can explain it to me with other words and\or examples.

Upvotes: 6

Views: 8241

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56849

This is the subnet ID returned by Azure so should look something like /subscriptions/subid/resourceGroups/subnet-test/providers/Microsoft.Network/virtualNetworks/vnetname/subnets/subnet1.

Other resources and other providers will return other things for the ID but in general the principle is that the ID for a resource is something that is unique for the provider (so the account and anything else that might differentiate it such as the region).

I don't know the Azure provider that well but for example the aws_instance resource will return an ID that is the instance ID which looks something like i-abcdef1234567890 and the aws_lb resource that creates Application/Network Load Balancers has an ID that is the ARN (Amazon Resource Name) and looks something like arn:aws:elasticloadbalancing:$REGION_NAME:$ACCOUNT_NUMBER:loadbalancer/app/my-example-alb/abcdef1234567890.

Upvotes: 7

Related Questions