Using Terraform to automate infrastructure in VMware Cloud Director
You can use Terraform to automate your infrastructure in VMware Cloud Director.
Here, we'll show you how to automate your infrastructure deployments using Terraform and cloud-init in a VMware Cloud Director environment.
In this guide, we will use Terraform to:
Create a network segment with subnet
172.16.1.1/24Create a 1:1 NAT rule mapping
x.x.x.xto172.16.1.200wherex.x.x.xis a public IP on your Edge GW.Create a firewall rule allowing ICMP, SSH, and HTTP(S) traffic to
172.16.1.200Create a VM with a static IP of
172.16.1.200Configure cloud-init to bootstrap WordPress on the VM
Prerequisites
We have created the guide specifically for customers deploying virtual machines from GleSYS templates in VMware Cloud Director. To complete this tutorial, you will need the following:
VMware Cloud Director API token. Refer to the official documentation for creating a VMware Cloud Director API token.
NSX Edge Gateway. This tutorial assumes that your VMware Cloud Director environment is configured with an NSX Edge Gateway with a public IP address.
Ensuring that your NSX Edge Gateway has no prior configuration is essential. Any configuration, such as firewall rules, will be overwritten.
If you want to follow this guide without impacting your production environment, email [email protected], and we will configure a temporary environment for testing.
DNS. Set up a DNS record for the FQDN of your WordPress site wp.example.com to point to the public IP address of your NSX Edge Gateway.
Deploying WordPress using Terraform and cloud-init
Step 1 – Preparing cloud-init configuration
Before delving into the Terraform configuration, let's first create the cloud-init configuration we will use to install and configure WordPress when our VM boots for the first time.
In your working directory, create a file called metadata.yaml and paste the following configuration into it:
Create a file called userdata.yaml and paste the following configuration into it. Ensure you replace all instances of the following:
glesyswith your preferred usernamewp.example.comwith the FQDN of your WordPress site[email protected]with a valid email address for the Let's Encrypt certificateecdsa-sha2-nistp256 AAAA...with your public SSH key
Step 2 – Initializing Terraform
In your working directory, create a file called main.tf and paste the following configuration into it:
Next, define the variables your project will use to make the code easier to reuse across environments.
Create a file called variables.tf and paste the following configuration:
Run terraform init to initialize the project and install the required providers:
This will output something like this:
Step 3 – Defining network resources
In your working directory, create a file called network.tf and paste the following configuration:
Step 4 – Defining server resources
In your working directory, create a file called server.tf and paste the following configuration:
Step 5 – Applying Terraform configuration
Ensure that your working directory resembles this layout by running ls -lh:
Run terraform apply to apply your configuration and provision your infrastructure:
Open your web browser and browse to the FQDN of your site to complete the WordPress installation:

Step 6 – Destroying Terraform configuration (optional)
Although not commonly used in production environments, Terraform can destroy the infrastructure that it has provisioned. It is particularly useful in lab scenarios such as this when we want to deploy infrastructure for testing or learning purposes and then destroy it as it is no longer needed.
The destroy command may fail when removing the vcd_network_routed_v2 resource, so you may need to run it twice.
Conclusion
In this how-to, you have used Terraform to build the infrastructure for running a WordPress server in VMware Cloud Director.
Furthermore, you have used cloud-init to initialize your virtual machine and automate the WordPress installation and configuration.
Now that you understand how Terraform and cloud-init work, you can extend this example to meet your production needs.
Here is a list of some suggestions:
Create a WordPress cluster and use Terraform to create an NSX load balancer to balance the traffic between multiple backend servers.
Modify cloud-init to deploy your web application on the virtual machine instead of WordPress.
Modify cloud-init to install Docker on the virtual machine and deploy Docker containers on the virtual machine.
The possibilities are endless.
Last updated
Was this helpful?