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.

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

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:

Dependency Graph in Terraform
Dependency Graph in Terraform

comments