Resource Dependencies
1. Resource Dependencies
Next up are resource dependencies. While building an infrastructure, you may prefer to have a visual representation of how your infrastructure is connected and interdependent. A dependency graph helps you understand your infrastructure before deploying it. Terraform builds a dependency graph from your configurations to generate plans and refresh state. The attributes are interpolated during run time, and primitives such as variables, output values, and providers are connected in a dependency tree. Terraform creates a dependency graph to determine the correct order of operations. In more complicated cases with multiple resources, Terraform will perform operations in parallel when it's safe to do so. Terraform can handle two kinds of dependencies: implicit and explicit. Implicit dependencies are known to Terraform, whereas explicit dependencies are unknown. Sometimes, one resource creation depends on the information generated from another. For example, you cannot create a compute instance unless the network is created. Similarly, you cannot assign a static IP address for a Compute Engine instance until a static IP is reserved. These dependencies are implicit. There are other scenarios where you induce a dependency. A given resource can only be created upon creation of another resource. In such cases, you would want to explicitly mention dependencies that Terraform cannot see. For example, let’s say you use a specific Cloud Storage bucket to run an application. That dependency is configured inside the application code and not visible to Terraform. In this scenario, you can use depends_on to explicitly declare the dependency. Let’s take a closer look at how Terraform handles implicit dependencies using another example. Here, the compute instance my_instance is created in a custom VPC called my_network. In Google Cloud, you cannot create a compute instance without a network. Terraform is informed of these relationships by interpolation expressions. Interpolation expressions should be used whenever possible. The reference to my_network in the network argument creates an implicit dependency on the network mentioned in the google_compute_network block. When you run terraform apply, you can view the order in which the resources are created. Terraform by default knows this order. Because of implicit dependency, Terraform is able to infer a dependency and knows it must create the network before creating the instance. In this example, Terraform creates the VPC my_network before creating the compute instance named my_instance. When Terraform reads the configuration, it will: First ensure that my_network is created before my_instance. Then save the properties of my_network in the state. And set the network argument in google_compute_instance to the value of the name argument in the google_compute_network block. Next, let’s examine explicit dependencies, which are not visible to Terraform. Explicit dependencies are defined with the depends_on argument within the dependent resource block. The depends_on argument gives you the flexibility to control the order in which Terraform processes the resources in a configuration. Depends_on can be used within the module block regardless of the resource type. The value can be an expression that directs to the resource. For example, let’s say you need to create two VMs—server and client —and want the client VM to only be created upon the successful creation of the server VM. This dependency is not visible to Terraform and has to be explicitly mentioned. You can use depends_on to explicitly declare the dependency of the client VM on the server VM. So, where in your configuration should these resource dependencies be defined? The order in which the resources are defined has no effect on how Terraform applies your changes, so organize your configuration files in a way that makes the most sense for you and your team. Upon executing terraform apply, you will notice that the client VM is created after the server VM. During interpolation of expression, Terraform processes the resource specified in the meta argument depends_on, and creates the server VM before creating the client VM.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.