Create Multiple Instances in a VPC Using Terraform
Tags: cloud, tutorial, terraform, infrastructureascode
Published: 2020-10-15 10:20:02

---
title: Create Multiple Instances in a VPC Using Terraform
published: true
date: 2020-08-06 12:00:00 UTC
tags: cloud,tutorial,terraform,infrastructureascode
canonical_url: 
---

### Learn how to provision multiple virtual server instances (VSIs) in a Virtual Private Cloud (VPC) using Terraform scripts with IBM Cloud Schematics.

Rather than directly diving into the Terraform scripts, let’s quickly learn about what is Terraform and how IBM Cloud Schematics simplifies the Terraform scripting experience on IBM Cloud.

### What is Terraform?

[Terraform](https://www.ibm.com/cloud/learn/terraform) is an open source software, developed by HashiCorp, that enables predictable and consistent provisioning of IBM Cloud platform, classic infrastructure, and VPC infrastructure resources by using a high-level scripting language. You can use Terraform to automate your IBM Cloud resource provisioning, rapidly build complex, multi-tier cloud environments, and enable [Infrastructure as Code (IaC)](https://www.ibm.com/cloud/learn/infrastructure-as-code).

### What is IBM Cloud Schematics?

With [IBM Cloud Schematics](https://cloud.ibm.com/docs/schematics?topic=schematics-getting-started), you can organize your IBM Cloud resources across environments by using workspaces. Every workspace points to a set of Terraform configuration files that build a Terraform template. You can choose to create your own Terraform template or use one of the pre-defined templates that are provided by IBM. Workspaces allow for the separation of concerns for cloud resources and can be individually managed with IBM Cloud Identity and Access Management. To use IBM Cloud Schematics, you don’t need to install the Terraform CLI or the IBM Cloud Provider plug-in for Terraform.

![](https://cdn-images-1.medium.com/max/1024/0*4wcQ70ja7NNLBtov.png)

### Let’s get started

Creating one VSI in a [VPC](https://www.ibm.com/cloud/learn/vpc) is pretty easy using the [Terraform IBM provider](https://github.com/IBM-Cloud/terraform-provider-ibm). If you check the [documentation](https://cloud.ibm.com/docs/terraform?topic=terraform-vpc-gen2-resources#instance-sample), the sample Terraform script goes like this:

```
resource "ibm_is_instance" "testacc_instance" {

name = "testinstance"

image = "7eb4e35b-4257-56f8-d7da-326d85452591"

profile = "bx2-2x8"

primary_network_interface {

subnet = ibm_is_subnet.testacc_subnet.id

}

network_interfaces {

name = "eth1"

subnet = ibm_is_subnet.testacc_subnet.id

}

vpc = ibm_is_vpc.testacc_vpc.id

zone = "us-south-1"

keys = [ibm_is_ssh_key.testacc_sshkey.id]

//User can configure timeouts_

timeouts {

create = "90m"

delete = "30m"

}

}
```

But what is the simplest way to provision multiple VSIs in a VPC?

This question came up while working on [vpc-instance-extension](https://github.com/IBM-Cloud/vpc-instance-extension) code sample to [auto-assign a floating IP as and when a new VSI](https://www.ibm.com/cloud/blog/extend-vpc-resources-with-cloud-functions-activity-tracker-with-logdna-and-schematics) is created.

One of our [companion scripts](https://github.com/IBM-Cloud/vpc-tutorials/tree/master/vpc-one-vsi/tf) for VPC [solution tutorials](https://cloud.ibm.com/docs/solution-tutorials?topic=solution-tutorials-tutorials#tutorials) had this answer, and all we need to do is extend the Terraform script to provision multiple VSIs. Code snippet from vpc-one-vsi script:

```
resource "ibm_is_instance" "instance" {

name = "${var.basename}-instance"

vpc = ibm_is_vpc.vpc.id

zone = var.subnet_zone

profile = module.map_gen1_to_gen2.profile

image = data.ibm_is_image.ds_image.id

keys = [data.ibm_is_ssh_key.ds_key.id]

resource_group = data.ibm_resource_group.group.id

primary_network_interface {

subnet = ibm_is_subnet.subnet.id

}

}
```

Create multiple instances using count

There is a parameter in Terraform called count that can be applied on resources to simplify configurations and let you scale resources by simply incrementing a number.

So, we can add a count to the above Terraform script:

```
resource "ibm_is_instance" "instance" {

count = var.instance_count

name = "${var.basename}-instance-${count.index}"

vpc = ibm_is_vpc.vpc.id

zone = var.subnet_zone

profile = module.map_gen1_to_gen2.profile

image = data.ibm_is_image.ds_image.id

keys = [data.ibm_is_ssh_key.ds_key.id]

resource_group = data.ibm_resource_group.group.id

primary_network_interface {

  subnet = ibm_is_subnet.subnet.id

}

}
```

`var.instance_count` is a Terraform variable, and you can find the complete script under the [vsi branch](https://github.com/IBM-Cloud/vpc-tutorials/tree/vsi/vpc-vsi/tf) of VPC-tutorials repository.

### What’s the role of Schematics?

You can run this Terraform script just by simply pointing to the Git repository with the scripts.

_Note:_You will need an IBM Cloud account with permissions to provision VPC resources. Check the [getting started with VPC](https://cloud.ibm.com/docs/vpc/vpc-getting-started-with-ibm-cloud-virtual-private-cloud-infrastructure) documentation.

- Navigate to [Schematics Workspaces](https://cloud.ibm.com/schematics/workspaces) on IBM Cloud and create a workspace-multiple-vsis-by choosing a resource group and location.
- Under **Settings,** scroll to the **Import your Terraform template** section and provide https://github.com/IBM-Cloud/vpc-tutorials/tree/vsi/vpc-vsi/tf under GitHub or GitLab repository URL.
- Select terraform\_v0.12 as the Terraform version.
- Click **Save template information** :

![](https://cdn-images-1.medium.com/max/744/0*YwvqV5oc8nPNhI7p.png)

- You should see the Terraform variables section. Fill the variables as per your requirement and don’t forget to set the instance\_count to 2 or more:

![](https://cdn-images-1.medium.com/max/1024/0*lxkQlPA-X2pr5ATP.png)

- Click **Save changes** and scroll to the top of the page to **Generate (_terraform plan_)** and **Apply (_terraform apply_)** the changes.
- Click **Apply plan** and click **View log** to check the progress. (Generate plan is optional.):

![](https://cdn-images-1.medium.com/max/1024/0*AJ3ZB2znCxkAUQJV.png)

### Using CLI

You can also provision VPC resources using the IBM Cloud CLI with Schematics plugin. All you need to do is to create a JSON config file and pass it to a command. Follow the instructions provided in the [README.md](https://github.com/IBM-Cloud/vpc-instance-extension/blob/master/README.md)of vpc-instance-extension repository.

### Remove resources

Cleaning up VPC resources is pretty easy. All you need to do is follow these three steps:

1. Click on **Actions** on your Schematics workspace and click  **Delete.**
2. Check the all the Delete options, and enter the workspace name.
3. Click **Delete** , and you are done.

### Learn more

Check the [IBM Cloud Solution tutorials](https://cloud.ibm.com/docs/solution-tutorials?topic=solution-tutorials-tutorials#tutorials) for more VPC-related content and code samples.

_Originally published at_ [_https://www.ibm.com_](https://www.ibm.com/cloud/blog/provision-multiple-instances-in-a-vpc-using-schematics) _on August 6, 2020._

* * *