Skip to content

Latest commit

 

History

History
165 lines (112 loc) · 4.42 KB

06-resource-dependencies.md

File metadata and controls

165 lines (112 loc) · 4.42 KB

Lab: Resource Dependencies

Help for the VSCode editor.

  1. Which argument should be used to explicitly set dependencies for a resource?

    depends_on

  2. 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.

  3. 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.

  4. 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
    1. Navigate to the given directory in Explorer pane and add key.tf

    2. Add the resource as requested

      Documentation

      Reveal
      resource "tls_private_key" "pvtkey" {
          algorithm = "RSA"
          rsa_bits = 4096
      }
      
    3. Deploy

      cd /root/terraform-projects/key-generator
      terraform init
      terraform plan
      terraform apply
  5. Information only

  6. 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 called private_key_pem of the pvtkey resource.
    1. Add the resource as requested

      Documentation

      Reveal
      resource "local_file" "key_details" {
          filename = "/root/key.txt"
          content = tls_private_key.pvtkey.private_key_pem
      }
      
    2. Deploy

      cd /root/terraform-projects/key-generator
      terraform init
      terraform plan
      terraform apply
  7. Now destroy these two resources.
    terraform destroy
  8. Information only - navigate to indicated directory in Explorer pane.

  9. 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 2:
      • Resource Name: krill
      • File Name: /root/krill
      • content: krill

    Resource called whale should depend on krill but do not use reference expressions.

    This means we need to create an explicit dependency on whale to krill which will ensure that krill is created before whale

    1. Add file main.tf

    2. 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.

    3. Deploy

      cd /root/terraform-projects//explicit-dependency
      terraform init
      terraform plan
      terraform apply