Skip to main content
In this lesson, we’ll explore how OpenTofu records resource attributes and manages dependencies during provisioning. Understanding these concepts is key to writing reliable Infrastructure as Code.

Exported Attributes

When you define a resource in OpenTofu, several attributes are exported after creation. For example, create an AWS key pair:
Here, public_key is a required argument. After running tofu apply, inspect the exported values using:
Example output:
You can target a specific resource by running tofu show aws_key_pair.alpha.
Learn more in the [OpenTofu CLI Docs].

Referencing Exported Attributes

Exported attributes become inputs for other resources. For instance, associate the key pair with an EC2 instance:
The reference aws_key_pair.alpha.key_name follows the format:
This creates an implicit dependency, ensuring aws_key_pair.alpha is created before aws_instance.cerberus.

Implicit Dependencies in Action

When you run tofu apply, OpenTofu will build the dependency graph and provision resources in the correct order:
During tofu destroy, the reverse order is applied: the EC2 instance is terminated before the key pair.
Implicit dependencies eliminate race conditions and ensure resources are created or destroyed in the correct sequence.
Read more about the [AWS Provider] for detailed attribute information.

Explicit Dependencies with depends_on

If resources lack direct attribute references but still require ordering, use the depends_on meta-argument:
Here, aws_instance.web will wait for aws_instance.db to finish creation first.
Overusing depends_on can complicate your configuration. Prefer implicit references whenever possible.
For advanced dependency control, see the [Infrastructure as Code] best practices.

That’s it for this lesson on resource attributes and dependencies in OpenTofu. In the next module, we’ll cover outputs and remote backends to share state across your team.

Watch Video