Recommendation

It is strongly recommended that you visit the pages in Examples of Terraform for Jetstream2 in order

In our configuration for the security group, we assigned most of the argument values using literals (strings and integers). But there will be times when you want to define a value only once and then reference it in multiple other places. For those situations, you can use Terraform's input variables.

We will learn about Terraform variables while creating a key pair in your Jetstream2 project. This example assumes you have a key pair stored on your computer in the default location: ~/.ssh/id_rsa and ~/.ssh/ida_rsa.pub. If you have a key pair stored elsewhere on your computer, adjust the example file accordingly. If you do not have a key pair on your computer, you should create one now. Note that while it is possible to have Terraform create a key pair for you on Jetstream2 and then download it to your computer, we are uploading an existing one to simplify this example. To continue, download the keyypair.tf, variables.tf and terratest.tfvars files into your configuration folder.

Defining a Key Pair

The file keypair.tf contains only one resource block. However, the argument assignments in that block use variables rather than literal values:


resource "openstack_compute_keypair_v2" "this" {
  name       = var.keypair_name
  public_key = file(var.public_key_file)
}

Notice that the "name" argument will take the value of a variable called "keypair_name" (line 2). To get the value for the "public_key" argument (line 3), the value of the variable "public_key_file"" is passed to the "file" function, which loads the text contents of the named file. "File" is one of the many built-in Terraform functions that can make your work much easier.

Defining Variables

Both of these variables must be defined in one of the .tf files in your configuration. In this example, we have defined all of the configuration's variables in a single file, variables.tf:


variable "keypair_name"     { }
variable "public_key_file"  { default = "~/.ssh/id_rsa.pub" }
variable "private_key_file" { default = "~/.ssh/id_rsa" }

variable "gateway_id"       { }
variable "subnet_cidr"      { default = "10.1.1.0/24" }

variable "image_id"         { }
variable "leader_flavor"    { default = "m1.small" }
variable "follower_flavor"  { default = "m1.small" }
variable "num_followers"    { default = 1 }

In this page we are only concerned with the first two of those variables, but the others will be used in later pages. These variables are defined using the "variable" block type, which has the form:


variable VARIABLE_NAME { OTHER_CONTENT }

You can see that the variable "public_key_file" has been assigned a default value (line 2), so it is up to you whether to override that value or not. The variable "keypair_name" does not have a default value (line 1) and so you must provide a value for it or Terraform will not process the configuration.

Setting Variables

Perhaps the most common way to provide (or override) variable values in a configuration is to use a .tfvars file that is then passed as an argument to your Terraform commands. We are using the file terratest.tfvars for this configuration.


keypair_name    = "terratest_keypair"
gateway_id      = "3fe22c05-6206-4db2-9a13-44f04b6796e6" # ID of "public" network in project
image_id        = "ac298b8a-2667-4c76-8510-c286c3183e09" # Ubuntu 22
leader_flavor   = "m3.small"
follower_flavor = "m3.small"
num_followers   = 1

Only the first assignment in this file is relevant to this page, but the others will be used later. Line 1 provides the missing value for the "keypair_name" variable. As written, the configuration will use the default value for the "public_key_file" and "private_key_file" variables. If you would like to supply either key from a different file than the default, edit terratest.tfvars at this time to add a line specifying your file's location. To pass a variable file as an argument to a Terraform CLI command, add the "-var-file" argument:


terraform plan -var-file=terratest.tfvars

If you run the command above and all of the variable assignments are suitable, you should see that Terraform knows it doesn't need to update the existing security group and that it only plans to perform actions necessary for creating the new key pair. You can go ahead and run terraform apply -var-file=terratest.tfvars to create the key pair, then check in the Horizon web interface to see the new key pair in your Jetstream2 project.

If your configuration references an undefined variable, you will get an error when running Terraform. For defined variables, you have several options for providing their value:

  • Specify a default value in the variable's definition.
  • Assign a value to the variable in a .tfvars file and pass that file as a -var-file argument to the CLI. If your file is named exactly terraform.tfvars, it will automatically be loaded without having to pass it as an argument.
  • Pass a value for an individual variable as a CLI argument using the -var argument, having the form -var="var_name=var_value". This argument may be used more than once to set values for multiple variables.
  • Define an environment variable with "TF_VAR_" prepended to the Terraform variable name to set its value. An environment variable named "TF_VAR_foo" will specify the value for a Terraform variable named "foo".
  • If a referenced variable still has no value at runtime, the CLI will prompt you to enter one on the command line.
 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement