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.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Exported Attributes
When you define a resource in OpenTofu, several attributes are exported after creation. For example, create an AWS key pair:public_key is a required argument. After running tofu apply, inspect the exported values using:
| Attribute | Description |
|---|---|
| arn | Amazon Resource Name for the key pair |
| fingerprint | SHA1 fingerprint of the public key |
| id | Unique identifier (same as key_name) |
| key_name | Name assigned to the key pair |
| public_key | SSH public key supplied by the user |
| tags_all | Combined map of resource and provider-level tags |
You can target a specific resource by running
Learn more in the [OpenTofu CLI Docs].
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:aws_key_pair.alpha.key_name follows the format:
aws_key_pair.alpha is created before aws_instance.cerberus.
Implicit Dependencies in Action
When you runtofu apply, OpenTofu will build the dependency graph and provision resources in the correct order:
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.
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:
aws_instance.web will wait for aws_instance.db to finish creation first.
Overusing
For advanced dependency control, see the [Infrastructure as Code] best practices.
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.