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
- Variables
- Variable types
- Types of variables:
- Practice with variable types
- 1. Create a variable called
name
that has astring
value of your name. - 2. Create a variable called
ec2_name
that has astring
value of your choice. - 3. Create a variable called
instance_max
that has anumber
value of your choice. - 4. Create a variable called
terminate_instances
that has abool
value of your choice. - 5. Create a variable called
ec2_instance
that is anobject
with threekeys
:name
,instance_max
, andterminate_instances
. - 6. Create a variable called
instance_ids
that is alist
ofstrings
with threeids
(you can create your ownids
). - 7. Create a variable called
instance_ports
that is alist
ofnumbers
with three ports:80
,22
, and8080
. - 8. Challenge: Create a variable called
ec2_instances
that is alist
of twoobjects
. Eachobject
should be information about
- 1. Create a variable called
- Next Steps
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 calledbools
,objects
do not have commas betweenkey-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 astring
. Astring
is any value (word, sentence, id, alpha-numeric passcode, etc) enclosed inquotes
(ie" "
). If you give a variable avalue
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 alwaysstrings
(you will begin to work with theids
of resources later. For now, just think of anid
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 inquotes
. When you give a variable anumber
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 the5
is enclosed inquotes
, making it a string.
bool
- abool
can only have two values:true
orfalse
. In terraform, you’ll often need to enter abool
for questions with a yes or no (true or false) answer.Bools
are not enclosed inquotes
.- Some examples are:
x = true
y = false
- The following is not an example of a
bool
variable:z = "false"
, because thefalse
is enclosed inquotes
, making it a string.
object
- anobject
is aset of key-value pairs
, enclosed incurly brackets
:{ }
. Akey-value
pair is something you will get used to seeing a lot of. All it means is that you have akey
=value
.
The key can be anything:
name
,age
,time
,day
,favorite_pet
,id
,it_is_raining_outside
, etc (literally, the key can be anything).
The
key
is mapped to, orequal to
, avalue
, which can also be anything that matches the type thekey
expects:"Kia"
,50
,"Saturday"
,"dog"
,6549828
,"kia@gmail.com"
,true
(respectively).
To create an
object
out of thekeys
andvalues
we just listed, we place them incurly brackets
({ }
), with eachpair
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 sametype
, ie you can have alist(number)
, alist(string)
, alist(object)
, etc.
For example, if you wanted to say that the variable
x
could be"hello"
or"hi"
, you could makex
alist
ofstrings
.
If you wanted the variable
y
to be equal to5
or10
, you could makey
alist
ofnumbers
.
If you wanted to make
z
equal to two differentobjects
, you could makez
alist
ofobjects
.
We denote a
list
of variable types withsquare brackets
:[ ]
.Items
inlists
are separated bycommas
.
- 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
(" "
).
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
(" "
).
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.
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.
5. Create a variable called ec2_instance
that is an object
with three keys
: name
, instance_max
, and terminate_instances
.
name
- should be astring
of your choiceinstance_max
- should be anumber
of your choiceterminate_instances
- should be abool
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 }
.
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 ]
.
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 ]
.
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 astring
of your choiceinstance_max
- should be anumber
of your choiceterminate_instances
- should be abool
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).
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.