Help for the VSCode editor.
-
Which argument should be used to explicitly set dependencies for a resource?
depends_on
-
Resource A relies on another Resource B but doesn't access any of its attributes in its own arguments. What is this type of dependency called?
explicit dependency
This is so, because we haven't referred to any of Resource B's attributes. A dependency is explict when we make it so by use of
depends_on
. -
How do we make use of implicit dependency?
reference expressions
Referencing another resource's attributes creates an implict dependency. This means that the other resource must be created first so that its attributes are known and can be read.
-
In the configuration directory /root/terraform-projects/key-generator, create a file called key.tf ...
- Resource Type:
tls_private_key
- Resource Name:
pvtkey
- algorithm:
RSA
- rsa_bits:
4096
-
Navigate to the given directory in Explorer pane and add
key.tf
-
Add the resource as requested
Reveal
resource "tls_private_key" "pvtkey" { algorithm = "RSA" rsa_bits = 4096 }
-
Deploy
cd /root/terraform-projects/key-generator terraform init terraform plan terraform apply
- Resource Type:
-
Information only
-
Now, let's use the private key created by this resource in another resource of type local file. Update the key.tf file
- Resource Name:
key_details
- File Name:
/root/key.txt
Content: use a reference expression to use the attribute calledprivate_key_pem
of thepvtkey
resource.
-
Add the resource as requested
Reveal
resource "local_file" "key_details" { filename = "/root/key.txt" content = tls_private_key.pvtkey.private_key_pem }
-
Deploy
cd /root/terraform-projects/key-generator terraform init terraform plan terraform apply
- Resource Name:
-
Now destroy these two resources.
terraform destroy
-
Information only - navigate to indicated directory in Explorer pane.
-
Within this directory, create two local_file type resources in main.tf file.
- Resource 1:
- Resource Name:
whale
- File Name:
/root/whale
- content:
whale
- Resource Name:
- Resource 2:
- Resource Name:
krill
- File Name:
/root/krill
- content:
krill
- Resource Name:
Resource called whale should depend on krill but do not use reference expressions.
This means we need to create an explicit dependency on
whale
tokrill
which will ensure thatkrill
is created beforewhale
-
Add file
main.tf
-
Declare resources
Reveal
resource "local_file" "whale" { filename = "/root/whale" content = "whale" depends_on = [ local_file.krill ] } resource "local_file" "krill" { filename = "/root/krill" content = "krill" }
Note that I have deliberately put
whale
first in the file. The ordering of the resources in the file does not matter. The dependencies, either implicit or explicit is what determines the order of creation.Note also that
depends_on
is a list argument, as a resource can depend on one or more other resources. -
Deploy
cd /root/terraform-projects//explicit-dependency terraform init terraform plan terraform apply
- Resource 1: