Episode 2.3 : Deploying a Configurable Web Server with Terraform

December 5, 2024 (4d ago)

Episode 2.3 : Deploying a Configurable Web Server with Terraform

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:

variable "NAME" {
    [CONFIG...]
}

Some Examples of input variables

Enforce the variable passed to be a number, if nothing is passed use 42

variable "number_check" {
    // Use description to give a little description helping others in your team
    description = "Example of a number variable, this can be a port number or something else"
    // type of the variable
    type = number
 
    // default value of the variable
    default = 42
}

Enforce the variable passed to be a list, if nothing is passed use ["1","2","3"]

   variable "list_example" {
 
    description = "An example of a list in Terraform"
 
    type = list
 
    default = ["1","2","3"]
}

💡 You can combine type constraints, too.

You can also create more complicated structural types using the object type constraint:

  variable "object_example" {
  description = "An example of a structural type in Terraform"
  type = object({
    name    = string
    age     = number
    tags    = list(string)
    enabled = bool
  })
  default = {
    name    = "value1"
    age     = 42
    tags    = ["a", "b", "c"]
    enabled = true
  }
}

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 :

variable "server_port" {
    description = "The port that's going to be used for HTTP requests"
 
    type = number
    // Instead of precising a default , you can do it at runtime like : terraform plan -var "server_port=8080"  or
    default = 8080
}

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:

   var .<VARIABLE_NAME>

Setting the port to be dynamic using variable

And ..

You can put any valid reference within the curly braces, and Terraform will convert it to a string.

Using an Interpolation inside of a string literal

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).

   output "<NAME>" {
     value = <VALUE>
     [CONFIG...]
   }

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.

You can also use the terraform output command to list all outputs without applying any changes:
 $ terraform output
 public_ip = "35.175.242.73"

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! 👋