Hi there, It's Dilan 🤓!
In this third episode of the blog series, after what we saw on the previous episode we can now continue with the deployment of a Configurable Web Server.
But wait, what's first a configurable server?
To put it simple, a configurable server is one that has a configuration file respecting the
DRY
(Don't Repeat Yourself) principle.
In our previous server, if you check the code of the .tf file, you will see that we havve the port number (8080
) at 2 places, that is maybe OK if you file is just 10 lines, but what if you have 100 of lines and you are using it in more than 20 places? , you see that it will be difficult to maintain the code.
Terraform provides the concept of input variables
which allows you to make your code more DRY ensuring maintenability.
the syntax is as follow:
Some Examples of input variables
Enforce the variable passed to be a number, if nothing is passed use 42
Enforce the variable passed to be a list, if nothing is passed use ["1","2","3"]
💡 You can combine type constraints, too.
You can also create more complicated structural types using the object type constraint:
Let's come back to our Web Server
So the variable we are going to create to make our code more DRY
will look something like this :
To use the value from an input variable in your Terraform code, you can use a new type of expression called a variable reference, which has the following syntax:
And ..
You can put any valid reference within the curly braces, and Terraform will convert it to a string.
In addition to input variables, Terraform also provides output
variables (You can think of it as the returned value of an action inside terraform that you are using as debuging information).
The NAME is the name of the output variable, and VALUE can be any Terraform expression that you would like to output.
After that you can run terraform apply
and you will see the output variable printed on the terminal.
It's all for today, and just like that you made you code DRY by using variables!!
It was Dilan, GDGoC ( Google Developer Groups On Campus) Lead. See you soon! 👋