depends_on and attribute references.
Table of Contents
- Understanding Resource Dependencies
- Generating a TLS Private Key
- Writing the Key to a Local File
- Cleanup
- Explicit Dependency with depends_on
- Links and References
Understanding Resource Dependencies
OpenTofu resources can depend on each other in two ways:| Dependency Type | Definition | Syntax Example |
|---|---|---|
| Explicit | Resource A waits for Resource B without accessing its attributes directly. | depends_on = [local_file.krill] |
| Implicit | Resource A references Resource B’s attribute in its arguments. | content = tls_private_key.pvtkey.private_key_pem |
depends_on argument when Resource A does not reference Resource B’s attributes:


Generating a TLS Private Key
Navigate to your project’skey-generator directory and create key.tf:
Storing private keys in plain text can pose a security risk. Never commit sensitive key material to version control.
Writing the Key to a Local File
Updatekey.tf to include a local_file resource:
/root/key.txt contains your PEM-encoded private key.
Cleanup
When you’re done, destroy both resources:Explicit Dependency with depends_on
Create a new directory (e.g.,/root/explicit-dependency) and add main.tf:
whale will only be created after krill, illustrating an explicit dependency without attribute references.
Apply this configuration: