Getting Started with Terraform in a vSphere Environment – Part 1
In this series, we will explore how to use Terraform to automate the creation and configuration of a virtual datacenter in a vSphere environment. In part one, we will focus on setting up Terraform and creating the basic definitions for our infrastructure.
To follow along with this series, you will need to have the following already set up and configured:
* The basic folder structure for this series (as shown below)
* Terraform downloaded and installed on your system (available for Windows, Linux, and macOS)
* The Windows x64 version of Terraform will be used in this series
Once you have the basic setup complete, open a command prompt and navigate into the deploy_datacenter folder. Type “terraform version” and hit enter to verify that Terraform is installed correctly and to see the version you are running.
Next, we will create some stub files to define our infrastructure. We will create the following files, then edit each one in turn to provide connection details, credentials, and our infrastructure (defined in code):
* provider.tf
* provider_variables.tf
* main.tf
* variables.tf
In the provider.tf file, we will define the provider type we wish to use, along with the information that it requires. The provider_variables.tf file will declare variables that will be used to pass in our connection details and credentials. The main.tf file will tell Terraform what object we are going to manipulate (either create, amend or destroy). In this case, we will specify a ‘vsphere_datacenter‘ resource to create. The variables.tf file will describe the value we wish to pass into the main.tf file; in this case, the name of our datacenter.
Here is an overview of each file and what it contains:
* provider.tf: This file defines the provider type we wish to use and the information that it requires.
* provider_variables.tf: This file declares variables that will be used to pass in our connection details and credentials.
* main.tf: This file tells Terraform what object we are going to manipulate (either create, amend or destroy). In this case, we will specify a ‘vsphere_datacenter‘ resource to create.
* variables.tf: This file describes the value we wish to pass into the main.tf file; in this case, the name of our datacenter.
Once you have created all of the files, let’s start by editing our provider.tf file. The provider.tf file is going to define the provider type we wish to use, along with the information that it requires. You may be wondering where we are going to find this information out from. It just so happens that the documentation Terraform provides is very detailed and everything we need to know can be found here.
We will continue our journey in part two of this series, where we will generate a plan, apply the plan, and create our datacenter object. Stay tuned!
Note: This article was written by Paul Davey, CIO at Sonar, Automation Practice Lead at Xtravirt, and guitarist in The Waders. He loves IT, automation, programming, music, and is passionate about helping others learn and grow. Copyright AutomationPro 2018.