Skip to content

Latest commit

 

History

History
164 lines (105 loc) · 5.09 KB

File metadata and controls

164 lines (105 loc) · 5.09 KB

Lab: Lifecycle Rules

Help for the VSCode editor.

  1. We have a directory created called /root/terraform-projects/project-mysterio. The main.tf file already has a couple of resource blocks.
    Which resource types do they use?
    1. Navigate to the indicated directory in the Explorer pane and open main.tf
    2. Remember which field of the resouce definition is the resource type
  2. Now, create these two resources that have been defined in this configuration file.

    In the terminal run the usual

    cd /root/terraform-projects/project-mysterio
    terraform init
    terraform plan
    terraform apply
  3. Which resource is created first in this case?

    string

    Why? Because the file resource has an implicit dependency on string by way of the value of its content attribute.

  4. We have modified the resource configuration again. Run a terraform plan now. What would happen?
    cd /root/terraform-projects/project-mysterio
    terraform plan

    We see Plan: 2 to add, 0 to change, 2 to destroy.. This means both are being replaced

  5. Why is the string resource being re-created?

    Examine the plan output. There are two properties that say "forces replacement", however one is as a result of the other.

    The value of keepers will be changed

  6. Information only

  7. Let's change the order in which the resource called string is recreated. Update the configuration so that when applied, a new random string is created first before the old one is destroyed.

    This is where lifecycle rules come in. We must add one to the string resource.

    1. Add the lifecycle rule within the string resource

      Reveal
          lifecycle {
              create_before_destroy =  true
          }
      
    2. Run terraform apply

  8. The resource block for the file resource has been updated! This will force the resource to be recreated during the next apply! But, before that, let's also add a lifecycle rule of create_before_destroy to this resource block.
    1. Add the same lifecycle block code to the file resource as you did for the string resource - copy/paste it.
    2. Important: Once the lifecycle rule has been added, only run the apply command once. We will learn why soon.
    3. Run terraform apply
  9. Great! We have now added the lifecycle rule and forced the resources to be created first and then destroyed.
    What is the id of the file resource we just created?
    1. Run terraform show
    2. Inspect the output to find the id of the file resource.
  10. Read the contents of the file /root/random_text manually. (Try opening with Visual Studio Code / cat command or a text editor)
    1. Look in the Explorer pane for this file.
    2. Er, it isn't there!
  11. Information only

  12. We have now wiped out the resources that were created in this configuration directory and updated the main.tf file.
    Now, it only contains a single random_pet resource called super_pet.
    Under which circumstances will a new pet id be created?

    Know that a new id for a resource is only generated if it is replaced, or it does not exist and will be created.

    Since we have input varaibles for both length and prefix, we can experiment with terraform plan. Examine the default values in variables.tf

    1. In terminal, navigate to project directoy.

      cd /root/terraform-projects/project-mysterio
    2. Try length. Use a value different from the default.

      terraform plan -var length=12
      

      Does this replace or create the resource?

    3. Try prefix. Use a value different from the default.

      terraform plan -var prefix=Dr
      

      Does this replace or create the resource?

  13. Now, update the configuration so that the resource super_pet is not destroyed under any circumstances with a terraform apply command.

    Another lifecycle rules here. We must add one to the super_pet resource.

    1. Add the lifecycle rule within the super_pet resource

      Reveal
          lifecycle {
              prevent_destroy =  true
          }
      
    2. Apply the configuration (it hasn't been applied yet), and plan a destroy

      cd /root/terraform-projects/project-mysterio
      terraform apply
      terraform plan -destroy