12

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

Marcin
  • 215,873
  • 14
  • 235
  • 294
cs1193
  • 1,090
  • 1
  • 16
  • 28
  • 4
    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 workspace – Alex Sep 10 '21 at 04:41

1 Answers1

16

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.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • 7
    What @Marcin 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. – Fred Lackey Mar 23 '22 at 21:51
  • 1
    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. – Ashok Koyi Dec 22 '22 at 19:56
  • `this` is also encouraged by https://www.terraform-best-practices.com/naming - however, personally, I find `this` too noisy, and prefer `_` e.g. `resource "aws_vpc" "_"`. – Custard Aug 03 '23 at 13:21