Help for the VSCode editor.
-
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?- Navigate to the indicated directory in the Explorer pane and open
main.tf
- Remember which field of the
resouce
definition is the resource type
- Navigate to the indicated directory in the Explorer pane and open
-
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
-
Which resource is created first in this case?
string
Why? Because the
file
resource has an implicit dependency onstring
by way of the value of itscontent
attribute. -
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 -
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 -
Information only
-
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.
-
Add the lifecycle rule within the
string
resourceReveal
lifecycle { create_before_destroy = true }
-
Run
terraform apply
-
-
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.
- Add the same lifecycle block code to the
file
resource as you did for thestring
resource - copy/paste it. - Important: Once the lifecycle rule has been added, only run the apply command once. We will learn why soon.
- Run
terraform apply
- Add the same lifecycle block code to the
-
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?- Run
terraform show
- Inspect the output to find the
id
of thefile
resource.
- Run
-
Read the contents of the file /root/random_text manually. (Try opening with Visual Studio Code / cat command or a text editor)
- Look in the Explorer pane for this file.
- Er, it isn't there!
-
Information only
-
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
andprefix
, we can experiment withterraform plan
. Examine the default values invariables.tf
-
In terminal, navigate to project directoy.
cd /root/terraform-projects/project-mysterio
-
Try
length
. Use a value different from the default.terraform plan -var length=12
Does this replace or create the resource?
-
Try
prefix
. Use a value different from the default.terraform plan -var prefix=Dr
Does this replace or create the resource?
-
-
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.-
Add the lifecycle rule within the
super_pet
resourceReveal
lifecycle { prevent_destroy = true }
-
Apply the configuration (it hasn't been applied yet), and plan a destroy
cd /root/terraform-projects/project-mysterio terraform apply terraform plan -destroy
-