terraform resource creation - this keyword
Asked Answered
T

2

13

I came across a pattern in couple of terraform code in Github.

resource "aws_vpc" "this"

I wanted to know how keyword this provides a particular advantage over a named resource. I can't find a Hashicorp documentation on this keyword.

https://github.com/terraform-aws-modules/terraform-aws-vpc/blob/3210728ee26665fab6b1f07417bcb0e518573a1d/main.tf

https://github.com/cloudposse/terraform-aws-vpn-connection/blob/master/context.tf

Twomey answered 10/9, 2021 at 1:32 Comment(1)
It's just a resource name. Not a very good or descriptive name... You can change it to whatever describes the VPC resource better in your environment. Also, you cannot have two "aws_vpc" resources with same name in same workspaceRinarinaldi
S
17

No, there is nothing special about this in terms of TF syntax or handling. Its just a name that may indicate that you have only one VPC in your setup. But this is not enforced by TF mechanism. Other common names are main or just vpc.

Scullion answered 10/9, 2021 at 2:23 Comment(3)
What @Scullion said. I just wanted to add, to whoever will see this in the future, please don't use the word "this" in Terraform. It's lazy and makes your code less clear to those who will see it in the future. Name your variables properly regardless of what language you're using.Prosaic
Since the type of resource is already implied when anyone accesses a singleton resource (aws_vpc.this), this is reasonable in this special case. Not sure whats a good name when there is only instance of that thing in your app.Nihil
this is also encouraged by terraform-best-practices.com/naming - however, personally, I find this too noisy, and prefer _ e.g. resource "aws_vpc" "_".Mushro
W
0

Terraform naming best practice (https://www.terraform-best-practices.com/naming) is stating -

Resource name should be named this if there is no more descriptive and general name available, or if the resource module creates a single resource of this type

It is good to provide more descriptive name to the cloud infra resources.

Warton answered 6/5 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.