Recommendation

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

Previously, we deployed a single instance to Jetstream2 using Terraform. Here, we will augment our configuration to deploy multiple similar instances. We will specify a "count" for some of the resources we are creating and then update the syntax of a few argument assignments so they work with those counts. To work with this example, download followers.tf into your configuration directory.

Specifying Counts

To specify the number of copies of a resource that will be deployed, we set its "count" argument to a whole number (from a literal or a variable). Count is a "meta-argument" that Terraform includes with every resource, so you won't find it listed in any provider documentation. Here are the resources for a set of "follower" instances and their floating IP addresses:


resource "openstack_compute_instance_v2" "followers" {
  count           = var.num_followers
  name            = "terratest_follower_${count.index}"
  image_id        = var.image_id
  flavor_name     = var.follower_flavor
  key_pair        = openstack_compute_keypair_v2.this.name
  security_groups = [openstack_compute_secgroup_v2.ssh_ping.name]

  network {
    name = openstack_networking_network_v2.private.name
  }
}

In both resource blocks, the count argument is set from the same variable, "num_followers". This variable is defined and set in other configuration files. We also use each instance's "index" when assigning its name by including ${count.index} within the value string of the "name" argument.

Referencing Indexed Resources

The next two blocks are able to reference all of the instances and IP addresses:


resource "openstack_networking_floatingip_v2" "followers" {
  count = var.num_followers
  pool  = "public"
}

resource "openstack_compute_floatingip_associate_v2" "followers" {
  count       = var.num_followers
  floating_ip = openstack_networking_floatingip_v2.followers[count.index].address
  instance_id = openstack_compute_instance_v2.followers[count.index].id
}

output "follower_floatingips" {
  value = openstack_compute_floatingip_associate_v2.followers.*.floating_ip
}

The multiple associations between floating IP addresses and instances are also made using the count argument. The syntax for referencing the properties of resources created in other configuration blocks changes only slightly when there are several copies of those resources. After the resource's type and name we now insert [count.index] to identify the individual resource that corresponds to our current index. In the output block, we wish to output all of the IP address that were created, so .* is inserted instead of the index for a single IP address.

The example configuration only specifies a single follower, and you can deploy it with terraform apply -var-file=terratest.tfvars. If the deployment is successful, you can view your new instance ("terratest_follower_0") in the Horizon web interface. Next, edit the file terratest.tfvars and change the value of "num_followers" to 2. Rerun the deployment and notice that Terraform leaves the first follower as is and only creates a second follower and its floating IP address, although it still outputs the full list of follower IP addresses. If you change the number of followers back to 1 and redeploy again, Terraform will be similarly efficient, only tearing down the second instance and its IP address.

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement