Skip to main content


CLOUDIST SESSIONS E4 | Deploy & destroy using Terraform

In this Cloudist Sessions episode, we are demoing the possibilities to build infrastructure through code using Terraform and Cloudist VDC.

Links to documentation:

Code used in the demo is attached below, needs to be changed to reflect customer Organisation, vDC, networks, Edge Gateway, template, vApps, VMs, IP etc.

vApp.tf

# Configure the VMware vCloud Director Provider
provider "vcd" {
  url      = var.vcd_url
  org      = var.vcd_org
  vdc      = var.vcd_vdc
  user     = var.vcd_user
  password = var.vcd_pass
}

# This resource will destroy (potentially immediately) after null_resource.next
resource "null_resource" "previous" {}

resource "time_sleep" "wait" {
  depends_on = [null_resource.previous]
  create_duration = "150s"
}

### NETWORKING ###

  # Edge GW Configuration
  data "vcd_edgegateway" "Test001-esg01" {
    org          = var.vcd_org
    vdc          = var.vcd_vdc
    name         = "Test001-esg01"
  }

  # Create routed org-network
  resource "vcd_network_routed" "MyAppNet" {

    name         = "MyAppNet"
    edge_gateway = "Test001-esg01"
    gateway      = "10.1.0.1"

    dhcp_pool {
      start_address = "10.1.0.15"
      end_address   = "10.1.0.20"
    }
  }

### vApp and VMs ###
  # vApp Name and Metadata
  resource "vcd_vapp" "MyApp" {
    name = "MyApp"

    metadata = {
      TestCycle = "123-A"
    }
  }

  # vApp network connected to routed org-network
  resource "vcd_vapp_org_network" "MyAppNet" {
    vapp_name         = "MyApp"
    org_network_name  = vcd_network_routed.MyAppNet.name
  }

  # vApp VM 1
  resource "vcd_vapp_vm" "WebServer" {
    vapp_name     = vcd_vapp.MyApp.name
    name          = "WebServer"

    catalog_name  = "DeployCatalog"
    template_name = "WebServer"

    memory        = 8192
    cpus          = 2

    network {
      type               = "org"
      name               = vcd_network_routed.MyAppNet.name
      ip_allocation_mode = "DHCP"
    }
  }

  # This resource will create (at least) 500 seconds after null_resource.previous
  resource "null_resource" "next" {
    depends_on = [time_sleep.wait]
  }

  # Create FW access rule
  resource "vcd_nsxv_firewall_rule" "MyApp-HTTP" {
    depends_on = [time_sleep.wait]
    org          = var.vcd_org
    vdc          = var.vcd_vdc
    edge_gateway = "Test001-esg01"

    name = "MyApp-HTTP"

    source {
      ip_addresses = ["any"]
    }

    destination {
      #ip_addresses = ["${vcd_vapp_vm.WebServer.network.0.ip}"]
      ip_addresses = ["${data.vcd_edgegateway.Test001-esg01.default_external_network_ip}"]
    }

    service {
      protocol = "tcp"
      port = "80"
    }
  }

    # Create FW DNAT rule
  #   Port forward TCP/80 to WebServer
  resource "vcd_nsxv_dnat" "MyApp-HTTP" {
    depends_on = [time_sleep.wait]
    org = var.vcd_org
    vdc = var.vcd_vdc
    edge_gateway = "Test001-esg01"

    network_type = "ext"
    network_name = "SEC1-PUBLIC-NET3"

    protocol = "tcp"

    original_address   = data.vcd_edgegateway.Test001-esg01.default_external_network_ip
    original_port = "80"
    
    translated_address = vcd_vapp_vm.WebServer.network.0.ip
    translated_port = "80"
  }

terraform.tfvars

vcd_user = "user"
vcd_pass = "password"
vcd_org = "organization-ID"
vcd_vdc = "vdc-ID"
vcd_url = "https://sec1-vdc.cloudist.solutions/api"
vcd_host = "https://sec1-vdc.cloudist.solutions/api"
vcd_allow_unverified_ssl = "true"

Products covered: VMware Cloud Director, Hashicorp Terraform

#cloudhappens #vmwarecloudproviders

To see all our videos visit our Youtube channel and hit subscribe to get the latest updates.

For more info, please contact: 

sales@cloudistsolutions.com or +46 10 788 20 30

 

 

 

 

 

Cookie Notice

This website uses cookies to ensure you get the best experience on our website More info

Back to top