How to solve implicit dependency problems via Terraform Graph?

This Terraform feature comes as a Saviour! On a daily basis is not frequently used, but in case of a problem with dependencies can save your time. Terraform Graph command generates a visual representation of dependencies to help you debug easier.

May 17, 2020
share
How to solve implicit dependency problems via Terraform Graph? Photo by Volodymyr Hryshchenko on Unsplash

The Implicit Dependency problem

The problem with dependencies most often occurs during provisioning and destroying cloud resources, because sometimes terraform misidentified dependencies. The primary way how Terraform resolves dependencies is by implicit dependency, it is when one block of code uses a reference to another resource value, see the example below.

resource "aws_eip" "ip" {
    vpc = true

    # Terraform assumes that this Elastic IP must be created only after the
    # EC2 has been created.
    instance = aws_instance.example.id
}

The Explicit Dependency solution

The most common solution is to add an option depends_on, which can you directly set up in the resource block in terraform code. This param creates an explicit relation. See the example below.

resource "aws_instance" "example" {
  ami           = "ami-2757f631"
  instance_type = "t2.micro"

  # Tells Terraform that this EC2 instance must be created only after the
  # S3 bucket has been created.
  depends_on = [aws_s3_bucket.example]
}

Debugging

There are multiple solutions to debug a terraform. You can read about all of them in the tutorial: How to Debug Terraform Projects, but the easiest way to get to know which resource is dependent on which one is to use terraform graph. The output from that command will be a visual representation of dependencies in DOT format, which can be used by GraphViz to generate charts.

Graphviz is open source graph visualization software. Graph visualization is a way of representing structural information as diagrams of abstract graphs and networks.

This can help you understand and see all connections between resources that Terraform assume to determine the proper order of modifying cloud resources. Let's see how to do that, but first of all, you have to install a graphviz package on your system.

Mac's

brew install graphviz

Linux

sudo apt-get install graphviz

Windows

The necessary windows installer, you can download from WinGraphviz website.

Create a Dependency Graph

To create a chart, run the following command:

terraform graph | dot -Tsvg > graph.svg

then you will create a graph similar to that one:

![/images/content/posts/how-to-create-dependency-graph.png](Dependency Graph in Terraform)

comments

This website uses cookies to provide services at the highest level. By continuing to use the site, you agree to their use.