Terraform Variables
Lesson 4
Now that you know how to use terraform to launch an EC2 instance and a little more about variables and variable types, we are going to combine all that knowledge into launching an EC2 instance with terraform using variables!
Table of contents
- Terraform Variables
- Terraform variable files
- The
variables.tf
file - TODO:
- The
terraform.tfvars
file - Refer to the variables
- Run
terraform apply
- Run
terraform destroy
- Next Steps
Terraform variable files
Now that you know the common variable types, you need to learn how terraform handles variables. In terraform, you will have two separate variable files: variables.tf
and terraform.tfvars
. One file (variables.tf
) will declare
your variables. This means your variables will be created, but not yet have a value
. In the other file (terraform.tfvars
) you will give all of the variables from variables.tf
a value
.
- We are going to continue working in your
ec2-terraform
directory. - Open your
terminal
and make sure you are in the correct directory by runningpwd
. - Create the files
variables.tf
andterraform.tfvars
using thetouch
command.- The
variables.tf
file - This file is where youcreate (or declare) your variables
. It’s like deciding you are going to use the variables,x
,y
, andz
ora
,b
, andc
, but not knowing what they will be equal to yet. You do not actually give the variablesvalue
in this file (iea
does not yet equal5
, or anything else yet). - The
terraform.tfvars
file - This file is where yougive your variables value
. Now that you’ve decided to use the variablesx
,y
, andz
, you now set them equal to avalue
, iex=1
,y="hello"
, andz=true
.
- The
NOTE: These two files are named very specifically so that terraform can find them and use them correctly, so unlike other files you cannot name them however you’d like.
The variables.tf
file
In the variables.tf
file, you will declare
your variables by giving them a type
and a name
. Anything you want to make a variable you will declare here.
TODO:
add comment lines to organize variable declarations and values.
Example: terraform number variable being declared
variable "max_number_of_instances" {
type = number
}
Note that the variable
name
ismax_number_of_instances
and thetype
isnumber
. Also note that while we have created a variable calledmax_number_of_instances
, we have not given that variable avalue
. If this variable were the same as the ones we created earlier, we would only have the first half, iemax_number_of_instances =
.
Example: terraform list of strings variable being declared:
variable "security_group_ids" {
type = list(string)
}
Note that the variable
name
issecurity_group_ids
and thetype
islist(string)
. Also note that while we have created a variable calledsecurity_group_ids
, we have not given that variable avalue
. If this variable were the same as the ones we created earlier, we would only have the first half, iesecurity_group_ids = [ ]
.
Create your variables
You can decide to call you variables anything you’d like, but naming them appropriately for future use (and for anyone else that ends up looking at or using your code) is a good practice.
You need to create four variables, all strings: instance_type
, key_name
, ami
and region
.
Using the examples given above, see if you can do it on your own before viewing the solution below.
Solution
The variables could be called anything, but we recommend: instance_type
, key_name
, ami
, and region
. You should have four variable blocks { }
(see image below). All four blocks should have type = string
.
The terraform.tfvars
file
In the terraform.tfvars
file, you will give the variables you declared in the variables.tf
file a value
. This is where a
now equals something (or, in this case, where instance_type
, ami
, and key_pair
will now equal something). You can only give variables a value here if they already exist in your variables.tf
file.
Example: terraform number variable being given a value
max_number_of_instances = 5
Note that the variable
name
ismax_number_of_instances
and thevalue
is5
. We see that the variable is anumber
based on the input we’ve given it. If you tried to put"5"
here, terraform would not allow you to deploy your instance because it knows (from thevariables.tf
file) that this variables is supposed to be a number, not a string.
Example: terraform list of strings variable being given a value
security_group_ids = ["sg-123456abcD", "sg-98765ZYXW", "sg-1542ijdo2"]
Note that the variable
name
issecurity_group_ids
and thevalue
is["sg-123456abcD", "sg-98765ZYXW", "sg-1542ijdo2"]
. We see that the variable is alist of strings
because of the input. If you tried to input anything but alist of strings
here, terraform would not allow you to deploy because it knows this variable only accepts alist of strings
.
Give you variables value
Now, you need to give your variables value. To do this, open your terraform.tfvars
file and list out the variables you created in the variables.tf
file. Remember, you only need to put: variableName = variableValue
now.
Give your variables the string values of:
ami-0947d2ba12ee1ff75
forami
yourname-us-east-1
(the name of thekey pair
you created in the previous lesson) forkey_pair
t2.micro
forinstance_type
us-east-1
forregion
Using the examples given above, see if you can do it on your own before viewing the solution below.
Solution
You should have variableName = variableValue
four times, with the variableValue
being inside quotes (" "
) because they are all strings. See image below.
Refer to the variables
Now that you have your variables declared and given a value, you need to tell your terraform resources about them, otherwise it will use what you put in there before (although right now they are the same).
- Open your
ec2.tf
file. - Edit your file so that you are referring to the variables you created instead of the values:
resource "aws_instance" "learn-the-cloud-test1" { ami = var.ami instance_type = var.instance_type key_name = var.key_pair }
Note: Whenever you want to refer to a variable in terraform, you always start with
var.
, then thename of the variable
.
Now, in your aws.tf
file, you need to refer to your region variables for the region
parameter. Try to do this on your own first, then expand the Solution
below to check your work.
Solution
You should have region = var.region
in your aws.tf
file.
Run terraform apply
You’ll now need to run terraform apply
from your ec2-terraform
project directory. This will launch a new EC2 instance into your AWS account. For full instructions, you can go back to the first lesson where you launched an EC2 and review.
Go into the AWS Console and find your instance. Check to make sure that the ami
, instance_type
, and region
are all correct.
Run terraform destroy
Run terraform destroy
to delete your instance in AWS.
Next Steps
You now know more about variables in terraform! 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. To do this, cd
into your ec2-terraform
directory.
Run the following commands, one at a time, in order:
rm -rf terraform.tfvars
rm -rf variables.tf
- Open
ec2.tf
and paste in the below resource block (paste over everything currently there):
resource "aws_instance" "learn-the-cloud-test1" {
ami = "ami-0947d2ba12ee1ff75"
instance_type = "t2.micro"
key_name = "kia-us-east-1"
user_data = <<-EOF
#! /bin/bash
yum update -y
yum install httpd -y
cd /var/www/html
echo "<html><h1> Hello! Welcome to Learn the Cloud! </h1></html>" > index.html
service httpd start
EOF
}
- Open
aws.tf
and paste in the below provider block (paste over everything currently there):
provider "aws" {
region = "us-east-1"
}
These commands will get you back to where you started before this lesson, so you can continue practicing from scratch! Go back to the beginning of the lesson by clicking here.
Move On
Now that you feel comfortable with using variables in terraform, you can move on to the next lesson, AMIs!