Introduction - A Case for Using Terraform Outputs
There are a few things that are incorrect in the last blog. Specifically, the way I chopped up some parts of code necessitated that I actually create Terraform OUTPUTS. Let’s take a look again at the original post’s EC2 Instance creation module and compare it to the newly updated (and correct) version.
Old EC2 Module
New EC2 Module
See the difference in the two pictures? In the second picture, the section I have highlighted makes use of Terraform’s variable option for the network interface. That is because I have a VPC module that provisions the network interface outside of the EC2 creation module, which makes it necessary to create a Terraform outputs file in order to be able to reference the VPC module’s values outside of the VPC module itself. Important Sidenote: It is also necessary in the EC2 Module to make an additional variable to pass the network interface the information it needs from the VPC module. In the second picture above, that variable is called interface_ID and, of course, is also declared in the variables.tf file. When calling the EC2 Module in the master main file, as you are about to see, you will need to reference the VPC module, since you are pulling an output file declared within that separate VPC module. More to be shown below.
Let’s take a look at the VPC module’s main file as well as its outputs file.
The VPC Creation Module and its Outputs.tf Folder
You will need to use Terraform outputs when you 1: want to print information generated to the screen / a log for recordkeeping purpose, or 2: when you want a module to be able to gain access to a resource provisioned within a separate module. In my case, I want the EC2 creation module to be able to access the network interface ID created within the VPC module. Since the outputs.tf file is actually only going to reference values that have already been provisioned within the module, we don’t actually need to change anything about the VPC creation module’s main.tf file. Instead, we simply create an outputs.tf file in the same directory as the main.tf file, and thankfully in my example, we only need one resource to be exported from the VPC module: the network interface’s ID.
The VPC Module’s Outputs.tf File
The syntax is pretty similar to declaring resources with the syntax, except it is output. Pretty basic, however its useage is fairly greater than that. If you wanted to access this variable, “network_int” from the CLI, or simply view it to see what Terraform provisioned for you, you can now use this ouptut value in the command line to output it should you wish to see it. I will be showing you instead how to use this value as a parameter of another function when called.
Using a Terraform Output as a Variable in a Module Call
In the above photo you can clearly see that in the blue highlighted text, interface_id (the variable added to the EC2 instance creation module) is accepting a value specified to be “module.Create_VPC_and_Subnet1.network_int,” and if you remember, network_int is the name of the outputs.tf value I specified earlier in our VPC instance. Using this method of referencing the fact that the resource I need comes from another module’s output file, alongside specifying that the module depends on (depends_on = etc at the bottom of the last module call) the VPC module ensures that the EC2 Instance’s dependencies are created before Terraform attempts to create the EC2 Instance itself.
This level of granularity is necessary when creating Terraform projects with multiple modules that rely on one another. However a good rule of thumb to know whether you need to make an output value for a certain resource is as follows: if a resource you need in a subsequent/secondary module is declared / provisioned in a primary / different module, make an output for that primary module so that subsequent modules that depend on that resource can use the output variable instead of referencing the resource directly, since there is no way that a module can know of a resource it needs existing if it has not been declared within itself already.
Hopefully this all made sense. It is important to know how Terraform outputs, dependencies, and modules all work together to form cohesive infrastructure as code platforms.
Happy Terraform Outputting!