Working with implicit and explicit dependency in Terraform
Implicit dependency
- We saw previously, that When we reference an attribute of a resource in an other resource block for setting one of its arguments, Terrafrom will detect automatically that this resource depends on the other
- For example, if a resource A uses in its resource block one of the attributes of a resource B, Terraform will notice that the resource A depends on the resource B, So it will create first of all the resource B, and then the resource A
- When destroying them, Terraform destroys them in the reverse order: So the resource A will be deleted first, and then the resource B will be deleted
- The type of dependencies is known as implicit dependency, because here, Terraform will know implicitly that the resource B depends on the resource A
Explicit dependency
- But in some cases a resource may depend on another resource even if it does not access any of its attributes
- So the question that we may ask here, how does Terraform know what resource depends on the other?
- In a situation like this, we have to explicitly tell Terraform, which resource depends on the other, and that why this type of dependencies is known as explicit dependency
- and that's done using the depends_on meta argument
The depends_on meta argument
- Let's first of all understand what are meta arguments in Terraform?
- Meta arguments are special kind of arguments in Terraform, that can be used in any resource block, regardless of the type of the resource, or the provider to which the resource belongs to.
- So they are not limited to a specific resource type, or a specific provider
- The depends_on meta_argument is one of the meta arguments that we have in Terraform, and it's used to set the dependency when a resource relies on another resource, but does not access any of the resource's data
Example: a file that depends on a random integer
- We will discuss now how to use the depends_on meta argument, to tell Terraform, that a resource depends on another resource, or a group of resources.
- let's say that we want to generated a random integer, using the random interger resource type which belongs to random provider
- then we want Terraform to create a file, called confirmation.txt, containing a confirmation message, indicating that the integer was generated successfully, and telling the user to check the value, that was generated.
- in this case the file resource depend,s on the random integer resource, because the file, must be created only after the generation of the random integer is done successfully
- So Terraform must first of all generate the random integer, and then create the file confirmation.txt to confirm that the integer was generated and to tell the user how to view the generated value
- Here the local_file resource depends on the random integer resource, even if it does not access any of its attributes
- let's move now to the configuration file
- as we saw several times, to generate a random integer, we add a resource block of type random_integer, and between the curly braces, we will set the required arguments, which are the min and the max arguments
- the same for creating the file, we will add another resource block, this time it will be of type local_file, and we will call this resouce confirmation_file
- inside the resource block, we will set the file name to confirmation.txt,
- and for the content argument, it will be set to the confirmation message, the message can be for example: "The integer was generated successfully. You can view it using the terraform show command"
- Now the configuration needed to create the file and the random integer is ready, but there is no sign that tells terraform that the file depends on the random integer , which makes terraform knows in which order they must be created.
- to set the dependency between the two resources, we will add the depends_on meta argument to the local_file resource block, which will accept a list of resources
- here the local_file depends only on the random_integer, that's why we will mention here only the random integer resource that we have declared previously
- You have to know that always to reference or to access a resource, we start by the resource type followed dot followed by the resource name
- now, when we run the terrafom apply command, terraform will know that the local_file depends on the random integer, that why terraform will first of all generate the random integer and then create the file
- When we destroy them, terraform will destroy them in the reverse order, so it will first of all destroy the local file, and then delete the random integer.
-So that's it for now, See you next time and thank you very much
Comments
Post a Comment