Terraform and vSphere - mk1
This article details the basics of getting terraform to deploy a VM clone from a vsphere template.
Introduction
Terraform is pretty awesome. If you are trying to find your way in the world of DevOps or Infrastructure Automation, you will probably hear about Terraform, or Infrastructure as Code.
I decided to write this series as a way for someone who might be more familiar with vSphere and wants to use Terraform with vSphere.
We will be using the vSphere provider and several others during the next few posts. We will start with a basic VM deployment and add layers of composability and terraform magic.
Terraform offers a nifty way to connect to your infrastructure, through the use of a term called “Providers”. Terraform relies on plugins called “providers” to interact with cloud providers, SaaS providers, and other APIs.
The VMware vSphere provider gives Terraform the capability to integrate with VMware vSphere. This provider can manage multiple elements of a VMware vSphere deployment, including VMs, networks, datastores, and more.
Prerequisites
To follow along, I will be assuming a few things:
You have a general understanding of vSphere, VMs, or have deployed VMs before.
You have a lab or non-production virtual environment to work with and test deployments.
You have a basic understanding of what Terraform is or have gone through some getting started tutorials.
If not, check out Hashicorp’s Terraform tutorials here: Intro to IaC with Terraform
Getting Started with Mk1
Let’s get into it! You can find the public repo containing the code for this tutorial here: Link
I’ve also linked the vSphere provider documentation here: Link
We will go through each section, detailing the components and how to use them.
Terraform Configuration Section
The first section of our main.tf
file will have the terraform global
configuration section.
This block identifies our required providers and any versions we want to specify.
It’s a good practice to call out the version that runs your code successfully. This way, if any
future versions change functions, you can trace back to the working plugin version.
|
|
Provider
The following section is the provider
block.
This block is for provider-specific configurations, like accessing VCenter and the account and
password information.
Placing secrets like passwords in your provider block is NOT SECURE!
It’s good practice to use a secrets engine like Vault or environment variables to handle passwords. Using one of these methods helps keep passwords out of configuration and state files. I’ll try and cover that soon in another series.
|
|
Data
The following section is the data
block.
This block is for accessing data sources defined in the provider. Using this block allows us to
grab component details necessary to feed into the resource block.
The data commands are capturing details from the named objects in datacenter, datastore, cluster, network, and template.
|
|
Resource
In the resource block, we are defining how terraform will create the VM resource.
I’ve separated the block in 2 sections for readability. The first section details VM hardware specifics, such as compute cluster, networking interface, and storage devices.
|
|
The second section below details VM clone specifics, such as the template to clone from and guest
customization settings. The lifecycle
section prevents VMs created from the template from being
modified if the template is changed or removed in the future.
|
|
Output
The output block details the IP and hostname of the VM created from the terraform run.
|
|