Terraform for VMware Cloud Foundation: where to start
An overview of the Terraform providers for VCF 9.1, what each one manages, and how to set up a project that stays maintainable.
Most VMware Cloud Foundation environments are still built by clicking through wizards. That works fine the first time. It works less well the third time, when you’re trying to remember which VLAN you used for vMotion in the second workload domain, or why one cluster is configured slightly differently from the others.
Terraform fixes that by turning the configuration into code. The desired state lives in Git, changes are reviewed before they’re applied, and building a second environment means running the same code with different variables.
This article is the first part of the VCF as Code series. It covers which providers exist, what each one manages, and how to set up a project that’s still easy to work with a year from now.
Why Terraform for VCF?
VCF already automates a lot: SDDC Manager deploys and patches the whole stack for you. What it doesn’t give you is a declarative description of your environment. Terraform adds that:
- Repeatable. The same code builds the lab, test and production environments.
- Reviewable. A
terraform planshows what will change before anything happens. - Documented. The code describes the environment, so it can’t drift from the docs.
- Recoverable. If something is removed by accident, the code knows what it should look like.
Terraform doesn’t replace SDDC Manager. The providers talk to the same APIs that the UI uses, so VCF still does the actual work: workflows, validations and lifecycle management.
The provider landscape
There isn’t one “VCF provider”. A VCF 9.1 environment has several layers, and each layer has its own API and its own Terraform provider:
| Provider | Talks to | Typical use |
|---|---|---|
vmware/vcf |
VCF Installer and SDDC Manager | Bring-up, network pools, hosts, workload domains, clusters, edge clusters, certificates, credentials |
vmware/vcfa |
VCF Automation | Organizations, regions, quotas, Supervisor namespaces, content libraries, VKS clusters |
vmware/vsphere |
vCenter | VM classes, tags, folders, content inside a cluster |
vmware/nsxt |
NSX Manager | Segments, gateways, firewall rules |
A useful way to think about it:
- Platform (Day 0 and Day 1).
vcfbuilds the infrastructure: from the first management domain to extra workload domains and clusters. - Consumption (Day 2).
vcfaturns that infrastructure into something tenants can use: organizations, quotas and self-service Kubernetes. - Fine-tuning.
vsphereandnsxthandle the details that the other two don’t cover.
VCF on Dell VxRail is not supported by the vcf provider. Check the support matrix in
the provider documentation before you start.
Setting up the project
Pin your provider versions
The vcf provider is still below version 1.0, so a minor release can contain breaking
changes. Pin both providers to a minor version, and upgrade on purpose instead of by accident:
terraform { required_version = ">= 1.9.0"
required_providers { vcf = { source = "vmware/vcf" version = "~> 0.19.0" # 0.19.0 adds support for VCF 9.1.1 } vcfa = { source = "vmware/vcfa" version = "~> 1.2.0" } }}Commit the .terraform.lock.hcl file that terraform init creates. It records the exact
provider versions and checksums, so everyone (and every pipeline) uses the same build.
Keep credentials out of the code
Both providers need credentials. Never put them in a .tf file or in a terraform.tfvars
that’s committed to Git. Declare them as sensitive variables:
variable "sddc_manager_host" { description = "FQDN of SDDC Manager" type = string}
variable "sddc_manager_username" { type = string}
variable "sddc_manager_password" { type = string sensitive = true}provider "vcf" { sddc_manager_host = var.sddc_manager_host sddc_manager_username = var.sddc_manager_username sddc_manager_password = var.sddc_manager_password}
# The vcfa provider reads VCFA_URL, VCFA_USER, VCFA_PASSWORD# and VCFA_ORG from the environment.provider "vcfa" { auth_type = "integrated"}Then supply the values from your shell, a password manager or your CI/CD secrets.
Terraform picks up any environment variable that starts with TF_VAR_:
# SDDC Manager (vcf provider)export TF_VAR_sddc_manager_host="sddc-manager.vcf.example.com"export TF_VAR_sddc_manager_username="administrator@vsphere.local"export TF_VAR_sddc_manager_password="$(pass show vcf/sddc-manager)"
# VCF Automation (vcfa provider)export VCFA_URL="https://automation.vcf.example.com"export VCFA_USER="serviceadministrator"export VCFA_PASSWORD="$(pass show vcf/automation)"export VCFA_ORG="System"Marking a variable as sensitive only hides it in the output. The value is still stored
in plain text in the state file. Treat the state as a secret: store it in a remote
backend with encryption and access control, and never commit it to Git.
Also use at least version 0.18.2 of the vcf provider: older versions could print SDDC Manager,
NSX and edge node passwords in clear text in terraform plan output.
A first example: hosts and a network pool
A good place to start is the step before every new workload domain: creating a network pool and commissioning hosts. It’s simple, it’s done often, and it’s easy to get wrong by hand.
resource "vcf_network_pool" "wld01" { name = "np-wld01"
network { type = "VSAN" vlan_id = 1611 mtu = 9000 subnet = "198.51.100.0" mask = "255.255.255.0" gateway = "198.51.100.1"
ip_pools { start = "198.51.100.10" end = "198.51.100.60" } }
network { type = "VMOTION" vlan_id = 1612 mtu = 9000 subnet = "203.0.113.0" mask = "255.255.255.0" gateway = "203.0.113.1"
ip_pools { start = "203.0.113.10" end = "203.0.113.60" } }}With the network pool in place, commissioning hosts is one resource with for_each. Adding a
host to the environment now means adding one line to a list:
variable "wld01_hosts" { description = "ESXi hosts to commission for workload domain 01" type = set(string) default = [ "esx01-wld01.vcf.example.com", "esx02-wld01.vcf.example.com", "esx03-wld01.vcf.example.com", ]}
variable "esxi_root_password" { type = string sensitive = true}
resource "vcf_host" "wld01" { for_each = var.wld01_hosts
fqdn = each.value username = "root" password = var.esxi_root_password storage_type = "VSAN" network_pool_id = vcf_network_pool.wld01.id}Set TF_LOG_PROVIDER_VCF=INFO before running Terraform to see the SDDC Manager task
updates. That’s very useful for long-running operations like commissioning hosts.
The workflow
Day to day, the work is always the same three steps:
terraform initterraform plan -out=wld01.tfplanterraform apply wld01.tfplanSaving the plan to a file and applying exactly that file means Terraform does what you reviewed, even if something changed in between.
Things to watch out for
- Long-running operations. Deploying a workload domain can take hours. Run Terraform from
a machine or pipeline that stays up, and check the
timeoutsblock on resources that support it. - Changes outside Terraform. If someone changes a resource in the UI, the next
planshows a difference. Decide as a team which resources are managed by Terraform, and stick to it. - Split your state. Don’t put the whole environment in one state. Split it at least into
platform (
vcf) and consumption (vcfa). That keeps plans fast and limits the impact of a mistake. - Import existing resources. For an environment that already exists, use
importblocks to bring resources under Terraform management before you change anything. Not every resource supports import yet, so check the provider documentation per resource.
Next up
In the next part we’ll look at the vcfa provider: creating an organization, assigning a region
quota, and giving a team its own Supervisor namespace to deploy VKS clusters in.