Link Search Menu Expand Document

Variables

Lesson 3

Now that you know how to use terraform to launch an EC2 instance, we’re going to dive a little bit deeper into the functionality terraform offers, namely, variables. Before getting into Terraform variables specifically, we are going to work with regular programming variables.

Variables, just like in math, are placeholders for values you want to be able to change. They are not required when working with terraform, but they make using terraform reusable and scalable on a whole new level.

Table of contents

  1. Variables
  2. Variable types
    1. Types of variables:
    2. Practice with variable types
      1. 1. Create a variable called name that has a string value of your name.
      2. 2. Create a variable called ec2_name that has a string value of your choice.
      3. 3. Create a variable called instance_max that has a number value of your choice.
      4. 4. Create a variable called terminate_instances that has a bool value of your choice.
      5. 5. Create a variable called ec2_instance that is an object with three keys: name, instance_max, and terminate_instances.
      6. 6. Create a variable called instance_ids that is a list of strings with three ids (you can create your own ids).
      7. 7. Create a variable called instance_ports that is a list of numbers with three ports: 80, 22, and 8080.
      8. 8. Challenge: Create a variable called ec2_instances that is a list of two objects. Each object should be information about
  3. Next Steps
    1. Redo the lesson (optional)
    2. Move On

Variable types

In order to use variables in terraform, you should first understand the different variable types. These variable types are standardized across most programming languages, and terraform has mirrored them fairly accurately.

  • Important Note: If you are new to terraform variables but not new to variables in a programming (any) language, you might notice that some of the below variables look a little odd. This is because terraform has a slightly different structure to many of its variables. A couple examples are: booleans are called bools, objects do not have commas between key-value pairs, etc. If you are already comfortable with the idea of and creating variables you can probably complete this section quickly and with ease.

Types of variables:

  • strings - one of the most common variable types in terraform is a string. A string is any value (word, sentence, id, alpha-numeric passcode, etc) enclosed in quotes (ie " "). If you give a variable a value that is enclosed in quotes, it is a string.
    • Some examples are:
    • x = "hello"
    • y = "I am doing well today. How about you?"
    • z = "12345ABCDE"
    • The ids of resources are always strings (you will begin to work with the ids of resources later. For now, just think of an id like your driver’s license id - it’s unique to you and allows for identifying you, specifically, among everyone else who might share your name).
  • number - another common variable type is a number. A number is just that - a number! A number is not enclosed in quotes. When you give a variable a number value, you put the number you want directly after the =.
    • Some examples are:
    • x = 5
    • y = 123456
    • z = 75
    • The following is not an example of a number variable: x = "5", because the 5 is enclosed in quotes, making it a string.
  • bool - a bool can only have two values: true or false. In terraform, you’ll often need to enter a bool for questions with a yes or no (true or false) answer. Bools are not enclosed in quotes.
    • Some examples are:
    • x = true
    • y = false
    • The following is not an example of a bool variable: z = "false", because the false is enclosed in quotes, making it a string.
  • object - an object is a set of key-value pairs, enclosed in curly brackets: { }. A key-value pair is something you will get used to seeing a lot of. All it means is that you have a key = value.

The key can be anything: name, age, time, day, favorite_pet, id, email, it_is_raining_outside, etc (literally, the key can be anything).

The key is mapped to, or equal to, a value, which can also be anything that matches the type the key expects: "Kia", 50, "Saturday", "dog", 6549828, "kia@gmail.com", true (respectively).

To create an object out of the keys and values we just listed, we place them in curly brackets ({ }), with each pair on a new line.

  • Here’s what our above example would look like:
    x = {
        name = "Kia"
        age = 50
        day = "Saturday"
        favorite_pet = "dog"
        id = 6549828
        email = "kia@gmail.com"
        it_is_raining_outside = true
    }
  • list(<type>) - a list is when you need a variable where you can enter more than one value. For example, if you wanted your EC2 servers to be protected by more than one security group (like a firewall) than you would need to have a list so that you could put both of them in it. The values in a list variable need to be the same type, ie you can have a list(number), a list(string), a list(object), etc.

For example, if you wanted to say that the variable x could be "hello" or "hi", you could make x a list of strings.

If you wanted the variable y to be equal to 5 or 10, you could make y a list of numbers.

If you wanted to make z equal to two different objects, you could make z a list of objects.

We denote a list of variable types with square brackets: [ ]. Items in lists are separated by commas.

  • Examples of the above are:
x = ["hello", "hi"]
y = [5, 6, 92]
z = [{
      name = "Kia"
      age = 26
    },
    {
      name = "yourName"
      age = 30
    }]

When you create a variable in terraform, you have to designate the variable type. Then, when you are ready to give the variable a value, you must only give it the type you told terraform that variable is. For example, if you created a variable called a and told terraform the variable was a string, you could not then give a the value of 10, because 10 is a number, not a string.

  • Important note: You can only give a variable the variable type it accepts, ie number, string, list(string) etc.

Practice with variable types

To make sure you understand variables types, try out the following challenges. You can type them into the textbox provided, then click Solution to compare your answers. Currently your answers will not be automatically checked for correctness. You’ll need to manually inspect the Solution and verify you got it right.

1. Create a variable called name that has a string value of your name.

Solution

The variable should be called name and should be before the =. After the = should be your name, enclosed in quotes (" ").

string_name

2. Create a variable called ec2_name that has a string value of your choice.

Solution

The variable should be called ec2_name and should be before the =. After the = should be any name for the EC2 instance you choose, enclosed in quotes (" ").

ec2-name-var

3. Create a variable called instance_max that has a number value of your choice.

Solution

The variable should be called instance_max and should be before the =. After the = should any number you choose, not enclosed in quotes.

string_name

4. Create a variable called terminate_instances that has a bool value of your choice.

Solution

The variable should be called terminate_instances and should be before the =. After the = should either true or false, not enclosed in quotes.

string_name

5. Create a variable called ec2_instance that is an object with three keys: name, instance_max, and terminate_instances.

  • name - should be a string of your choice
  • instance_max - should be a number of your choice
  • terminate_instances - should be a bool of your choice
Solution

The variable should be called ec2_instance and should be before the =. After the = should an open curly bracket {, then three new lines, like in the image below, and a last line of a closed curly bracket }.

string_name

6. Create a variable called instance_ids that is a list of strings with three ids (you can create your own ids).

Solution

The variable should be called instance_ids and should be before the =. After the = should an open square bracket [, then three string values, in quotes (" "), separated by commas. Lastly, your variable should end with a closed square bracket ].

string_name

7. Create a variable called instance_ports that is a list of numbers with three ports: 80, 22, and 8080.

Solution

The variable should be called instance_ports and should be before the =. After the = should an open square bracket [, then three number values, not in quotes, separated by commas. Lastly, your variable should end with a closed square bracket ].

string_name

8. Challenge: Create a variable called ec2_instances that is a list of two objects. Each object should be information about

a different EC2 instance (similar to the object you created earlier). Include the below key-value pairs in each object:

  • name - should be a string of your choice
  • instance_max - should be a number of your choice
  • terminate_instances - should be a bool of your choice
Solution

The variable should be called ec2_instances and should be before the =. After the = should an open square bracket [ followed by an open curly bracket { (to denote your first object). Then, you should have three new lines, each with one of: name, instance_max, and terminate_instances followed by an equal sign = and a correct variable type. Next, you sould have a closed curly bracket } (to denote the end of your first object) followed by a comma. You should then have another open curly bracket { (to denote the starte of your second object), followed again by the three lines of name, instance_max, and terminate_instances (with different values). Lastly, you should have a closed curly bracket } and a closed square bracket ] (one to close your second object and the other to close the list).

string_name

Next Steps

You now know more about variables in programming! How you feel about the creating them on your own determines what you should do next.

Redo the lesson (optional)

If you don’t feel as comfortable as you’d like with the above lesson, you can start over. While the problems will not change, you can easily collapse the Solutions and try them all again. You can also think of your own to try! Go back to the beginning of the lesson by clicking here

Move On

Now that you feel comfortable with variables and variable types, you can move on to the next lesson, EC2: Variables.