Referencing a resource attribute in another resource block
- We have seen until now how to create resources with Terraform without any dependencies between them, which rarely happens in real world resource provisioning solutions
- Here for example, we have a configuration file for creating two resources: a random_integer and a local file
- The two resources are independent of each other that's why they can be created in any order when running the terraform apply command
- in real world scenario, a resource may depend on another resource or a group of resources
- for example in azure, we have what we call a resource group, which is a logical container for grouping and managing azure resources
- it serves as a way, to organize and manage resources, based on their lifecycles, their applications or their environments.
- a set of resources within a resource group, are deployed, updated and deleted together as a single management unit.
- So for provisioning a group of resources, related to each other in azure with terraform, first of all we need to create a resource group and then provision the remaining resources that should belong to the resource group that we have created: so that an example of resource dependencies
- one way to make a resource depends on another resource in terraform, is to reference a resource attribute in the resource block of another resource, for setting one of its arguments
- we will see how this work in terraform with simple resources, such a local file and a random integer and then you can apply that in real world examples when provisioning resources for example in azure, aws, google cloud or wherever you want
Example: a local file that depends on a random integer
- let's say that we want to create two resources with terraform: a random integer and a local file
- we want terraform first of all to generate a random integer using the random provider and then store the generated integer in a file called count.txt
- we can achieve that by referencing the result attribute of the random integer resource, in the resource bock used to create a local file, for setting the content argument of the file
- So in this example, the file count.txt depends on an other resource, which is the random integer generated by terraform, because the content of the file will be the integer that will be generated
- let's see how the configuration file will look like?
- to create a random integer, we declare a resource block of type random_integer, and call it for example rand_int
- then inside the resource block, we will set the required argument, wich are the min and max argument
- we will assign the value 3 to the min argument, and the value 10 to the max argument
- then to create a file, we will declare an other resource block, and we will set the resource type to local_file, and call it for example count_file.
- Inside the resource block, we will set the filename argument and the content argument
- We will give the value count.txt to the filename argument
- and for the content argument, it will be set to the integer generated using the random provider.
- The integer can be accessed using an attribute called result of the rand_int resource, and we can reference it in the resource block of the local file like that: so first of all, we mention the resource type, which is random_integer, followed by the resource name which is rand_int, and finally the attribute name which is result
- and this will make the file depends on the random integer
- Now by running the terraform apply command, terraform, will first of all generate the random integer and then create the local file
- An important point to know here: Terraform will always create the random integer first, and then create the local_file, regadless of the declaration order
- So even if the local_file is declared first, and then the random_integer, the random_integer will still generated before the local_file, because terraform will find in the configuration file, that the local_file depends on the random integer
Example in Azure: an aks cluster that belongs to a resource group
- Le's go back to azure and see how to provision an azure container registry, that belongs to a specific resource group
- to create a resource group with terraform, we have to declare a resource block in the configuration file, of type azurerm_resource_group
- we give a name to this resource, for example dev_rg
- and inside the resource block, we have to set the required arguments which are:
- the name argument: we set it for example to dev_resource_group and for the location argument, we can set it for example to West Europe
- Then we need an other resource block, of type azurerm_container_registry, for creating a container registry in azure
- we give the name "dev_acr" to our resource,
- and inside the resource block, we will set the required arguments
- First of all, we will set the name to dev_container_registry
- then for the resource groupe name, we will reference the name attribute, of the resource groupe declared previously, and this is what will make the container registry depends on another resource, which is the resource groupe
- and finally for the location argument, it will be set to west europe
- Because here, we have referenced the name attribute of the resource group, in the resource block used to create the container registry, terraform will know that the container registry depends on another resource which is the resource group, So it will first of all create the resource group and then create the container registry
- This type of dependency in terraform is known as implicit dependency, in the next lecturen we will see how to work with, another type of resource dependencies in terraform, known as explicit dependency
You can learn more about resource attribute reference in terraform from this video
- So that's it for now, i hope that every thing is clear and see you next time
Comments
Post a Comment