Link Search Menu Expand Document

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

  1. Terraform Variables
  2. Terraform variable files
  3. The variables.tf file
  4. TODO:
    1. Example: terraform number variable being declared
    2. Example: terraform list of strings variable being declared:
    3. Create your variables
  5. The terraform.tfvars file
    1. Example: terraform number variable being given a value
    2. Example: terraform list of strings variable being given a value
    3. Give you variables value
  6. Refer to the variables
  7. Run terraform apply
  8. Run terraform destroy
  9. Next Steps
    1. Redo the lesson (optional)
    2. Move On

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 running pwd.
  • Create the files variables.tf and terraform.tfvars using the touch command.
    • The variables.tf file - This file is where you create (or declare) your variables. It’s like deciding you are going to use the variables, x, y, and z or a, b, and c, but not knowing what they will be equal to yet. You do not actually give the variables value in this file (ie a does not yet equal 5, or anything else yet).
    • The terraform.tfvars file - This file is where you give your variables value. Now that you’ve decided to use the variables x, y, and z, you now set them equal to a value, ie x=1, y="hello", and z=true.

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 is max_number_of_instances and the type is number. Also note that while we have created a variable called max_number_of_instances, we have not given that variable a value. If this variable were the same as the ones we created earlier, we would only have the first half, ie max_number_of_instances = .

Example: terraform list of strings variable being declared:

variable "security_group_ids" {
  type = list(string)
}

Note that the variable name is security_group_ids and the type is list(string). Also note that while we have created a variable called security_group_ids, we have not given that variable a value. If this variable were the same as the ones we created earlier, we would only have the first half, ie security_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.

string_name

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 is max_number_of_instances and the value is 5. We see that the variable is a number 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 the variables.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 is security_group_ids and the value is ["sg-123456abcD", "sg-98765ZYXW", "sg-1542ijdo2"]. We see that the variable is a list of strings because of the input. If you tried to input anything but a list of strings here, terraform would not allow you to deploy because it knows this variable only accepts a list 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 for ami
  • yourname-us-east-1 (the name of the key pair you created in the previous lesson) for key_pair
  • t2.micro for instance_type
  • us-east-1 for region

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.

string_name

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 the name 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.

string_name

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!