How to use Terraform with VMware Cloud on AWS

In this article I will show you how to use Terrafom to provision a VM to VMware cloud on AWS. Using Terraform with the vSphere Provider is not new. Because VMware Cloud on AWS is a managed service there are some gotchas with using a tool like Terraform. Because of the limited rights model you need to make some adjustments to your Terraform plan to accommodate this, then your on your way to developer Nirvana.

Updated 2/26/18

Pre-Reqs

  • A Linux template in VMware Cloud on AWS that can be cloned with guest customization
  • At least one logical network in VMware Cloud on AWS
  • A Linux workstation that can access the vCenter Serer and VM’s on the Logical Network and Github,com
  • Terraform installed and configured in the workstation VM see here

Before we begin let’s explore the limited rights model a bit. In VMware Cloud on AWS. no matter what ACL you give your users you will not have access to write to the root of the data center and you are limited to a certain resource pool

You can only create VM’s and folders in the workloads folder

You can only provision new VM’s under the Compute-Resources resource pool

Login to your workstation machine and change to the /home directory

Clone my git repository locally

Change to the vmwonaws/terrafom directory

cd vmwonaws/terraform/

edit the my vm file with the correct information from your environment

Press i to change the values see below for the values I highlighted them in red

Note: Some values may actually be the same in your SDDC.

data "vsphere_datacenter" "dc" {
  name = "SDDC-Datacenter"}

data "vsphere_datastore" "datastore" {
  name          = "WorkloadDatastore"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_resource_pool" "pool" {
  name          = "Compute-ResourcePool"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_network" "network" {
  name          = "Management Networks/set-cgw-network-1"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_virtual_machine" "template" {
  name          = "Templates/RHEL"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

resource "vsphere_virtual_machine" "vm" {
  name             = "terraform-test"
  resource_pool_id = "${data.vsphere_resource_pool.pool.id}"
  datastore_id     = "${data.vsphere_datastore.datastore.id}"
  folder            = "Workloads"

  num_cpus = 2
  memory   = 1024
  guest_id = "${data.vsphere_virtual_machine.template.guest_id}"

  scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}"

  network_interface {
    network_id   = "${data.vsphere_network.network.id}"
    adapter_type = "${data.vsphere_virtual_machine.template.network_interface_types[0]}"
  }

  disk {
    label            = "disk0"
    size             = "${data.vsphere_virtual_machine.template.disks.0.size}"
    eagerly_scrub    = "${data.vsphere_virtual_machine.template.disks.0.eagerly_scrub}"
    thin_provisioned = "${data.vsphere_virtual_machine.template.disks.0.thin_provisioned}"
  }

  clone {
    template_uuid = "${data.vsphere_virtual_machine.template.id}"

    customize {
      linux_options {
        host_name = "terraform-test"
        domain    = "test.internal"
      }

      network_interface {
        ipv4_address = "10.46.159.160"
        ipv4_netmask = 24
      }

      ipv4_gateway = "10.46.159.1"
    }
  }
}

Now create a passwords file named terraform.tfvars with the following

vsphere_user= "cloudadmin@vmc.local"
vsphere_password= "thepassword"
vsphere_server= "ipaddress"

Now initialize terraform with terraform init

now type terraform plan

this will test your plan before doing anything in vCenter

If you did not get errors type terraform apply

Notice you should see the VM create in vCenter as well as a bunch of infor in the command line interface

To clean everything up type terraform destroy

Enjoy!

Remember sharing is caring!

One Reply to “How to use Terraform with VMware Cloud on AWS”

  1. Pingback: Infrastructure-As-Code with Terraform, VMware and VMware Cloud on AWS - VMware Cloud Community

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.