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:
First, we set an explicit dependency using the
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: