Implicit Resource Dependencies

Learn and execute implicit resource dependencies in Terraform.

Dependencies

The dependencies are the bane of any infrastructure manager’s life. If we switch off the Virtual Machine (VM), who will complain? What about the load balancer?

Stories of switching off servers to see who complains as a way of managing unknown dependencies are incredibly common in the IT industry.

Terraform can help manage that problem by codifying and tracking resource dependencies. However, it’s important to remember that it does not do so by magic and that our application dependencies may still need to be mapped out by whoever’s in charge of them.

Implicit dependency

Initially, we need to set up our credentials. Then, we create a new directory with a Terraform module.

We also create a Terraform module with a defined AWS provider.

Then we add two resources to the dependencies.tf file. The first is an AWS VPC, similar to the one we created before. This time, we give it a name directly in the Terraform module, ltthw-vpc, rather than doing so manually in the AWS console.

The second resource type is aws_subnet. This subnet is attached to the VPC, and is linked by the VPC id.

That raises a problem. We don’t know the VPC’s ID before we create it (it’s given to us by AWS on creation). Yet we need to refer to it to link the subnet to the VPC via the vpc_id.

**Note:**A subnet is a range of IP addresses that we can route network traffic to or set other rules against. If you don’t understand this, don’t worry about it for this course, but if you’ll manage infrastructure on cloud providers, you may want to research it on your own.

In Terraform, every resource has attributes that we can reference with the syntax TYPE.NAME.ATTRIBUTE. Remembering the three-letter acronym TNA might help you remember this pattern of referencing.

**Note:**We assume that your AWS credentials have permissions to create an AWS VPC. If you run into problems, it may be because your account is limited, and you will need to expand its permissions or use a different one.

Now, initialize the Terraform module.

Get hands-on with 1400+ tech skills courses.