Creating Multiple Virtual Machines in ESXi Using Terraform

Terraform is a powerful tool for automating infrastructure as code (IaC). In this guide, we will introduce Terraform, discuss its usage, features, and benefits, and provide a step-by-step tutorial on using Terraform to create multiple virtual machines (VMs) in VMware ESXi.

Terraform, developed by HashiCorp, is an open-source IaC tool that enables you to define and provision data center infrastructure in human-readable configuration files. It uses a declarative language, meaning users define their desired state, and Terraform handles the rest to achieve that state. Terraform is cloud-agnostic, making it a versatile choice for managing infrastructure across various platforms, including AWS, Azure, Google Cloud, VMware, and more.

Declarative Configuration Language: Written in HashiCorp Configuration Language (HCL), which is easy to understand and helps define resources succinctly.

Multi-Cloud Support: Provides integrations for various cloud providers, including AWS, Azure, Google Cloud, and private clouds like VMware.

Immutable Infrastructure: Ensures that infrastructure is deployed and configured consistently, reducing the risk of configuration drift.

Dependency Management: Automatically determines dependencies between resources, ensuring correct resource provisioning order.

State Management: Keeps track of resources’ current state, allowing it to manage updates, additions, and deletions efficiently.

Consistency and Repeatability: By defining infrastructure as code, you can consistently provision identical environments, eliminating human error.
Scalability: Terraform can handle complex configurations, allowing you to scale resources seamlessly.
Ease of Collaboration: Code-based infrastructure allows teams to work together effectively, as code changes can be versioned and tracked.
Time-Saving: Automating infrastructure deployment significantly reduces manual setup time, accelerating project deployment cycles.

Terraform can be used to manage VMware ESXi infrastructure with the help of the Terraform vSphere Provider. The vSphere Provider allows you to manage resources within your VMware environment, including virtual machines, resource pools, and networks.

Using Terraform to automate the creation and management of VMs in ESXi offers several advantages:

Rapid Provisioning: You can quickly spin up multiple VMs with consistent configurations.

Reduced Manual Configuration: By defining VMs in code, you eliminate repetitive manual setup tasks.

Version Control: VM configurations can be stored in version control, allowing you to track and manage changes.

Before starting, ensure you have:

VMware ESXi: The vSphere API should be available, which is typically supported in vSphere, vCenter Server, and ESXi environments.

Terraform Installed: Download and install Terraform from HashiCorp’s official website.

vSphere Provider Configuration: Access to the vSphere provider, which connects Terraform to VMware.

Below is a step-by-step guide to creating multiple VMs in VMware ESXi using Terraform.

Install Terraform: Ensure that Terraform is installed on your local machine. To verify, run:

terraform -v

Configure the vSphere Provider: Define your VMware ESXi environment settings.

Initialize the Directory: Start by creating a new directory for your Terraform configuration and navigate into it:

mkdir terraform-esxi-multiple-vms
cd terraform-esxi-multiple-vms

Provider Configuration: Create a new file named main.tf to define the vSphere provider and your ESXi connection settings.

provider "vsphere" {
user = "your-username"
password = "your-password"
server = "your-esxi-host"
allow_unverified_ssl = true
}

Define VM Variables: Create a variables.tf file to define reusable variables, such as the number of VMs and their specifications.

variable "vm_count" {
description = "Number of VMs to create"
type = number
default = 3
}

variable "vm_name_prefix" {
description = "Prefix for the VM names"
type = string
default = "test-vm"
}

variable "vm_cpu" {
description = "Number of CPUs for each VM"
type = number
default = 2
}

variable "vm_memory" {
description = "Memory size for each VM in MB"
type = number
default = 2048
}

VM Resource Definition: In main.tf, define the virtual machine resource with a loop to create multiple VMs based on vm_count.

resource "vsphere_virtual_machine" "vm" {
count = var.vm_count
name = "${var.vm_name_prefix}-${count.index + 1}"
resource_pool_id = "resource-pool-id"
datastore_id = "datastore-id"

num_cpus = var.vm_cpu
memory = var.vm_memory

network_interface {
network_id = "network-id"
adapter_type = "vmxnet3"
}

disk {
label = "disk0"
size = 20
eagerly_scrub = false
thin_provisioned = true
}

guest_id = "otherGuest64"
cdrom {
client_device = true
}
}

Replace the placeholders such as resource-pool-id, datastore-id, and network-id with the appropriate values from your ESXi environment.

Output Configuration (optional): To display the VMs created, add an output block in outputs.tf.

output "vm_names" {
value = [for vm in vsphere_virtual_machine.vm : vm.name]
}

Initialize Terraform: Run terraform init to initialize the configuration, download the necessary providers, and set up your environment.

terraform init

Preview the Plan: Run terraform plan to preview the resources that will be created. This command shows the virtual machines, resource pools, and other elements defined in the configuration file.

terraform plan

Apply the Configuration: Use terraform apply to provision the VMs on your ESXi host. Terraform will prompt for confirmation before creating the resources.

terraform apply

Verify the Deployment: After the deployment completes, check your ESXi environment to confirm that the VMs were created successfully. The output block, if defined, will display the names of the VMs in the command line.

Managing VM Changes: If you modify the configuration files (e.g., changing CPU or memory allocation), re-running terraform apply will update the VMs accordingly.
Scaling the Number of VMs: You can increase or decrease the number of VMs by adjusting vm_count in the variables.tf file. Apply the changes with terraform apply.
Resource Management: Remember that Terraform keeps a state file (terraform.tfstate) to track resources. If you delete the VMs manually in ESXi, the state file may become inconsistent.

Using Terraform to create multiple VMs in ESXi streamlines the deployment process, making it easier to manage and scale VM infrastructure. With this approach, you can define configurations, replicate environments, and maintain consistency with ease. Terraform’s ability to manage infrastructure as code saves time and enhances operational efficiency, particularly in VMware environments. This tutorial should help you get started with creating multiple VMs in ESXi, enabling better infrastructure management in your data center.

80%
Awesome
  • Design
DevOpsESXiIaCInfrastructure as a CodeTerraformvirtualizationVMVMware
Comments (0)
Add Comment