Terraform, vSphere, Slack & Webhooks


In my continuing journey to become more of an “Automation, APIs and DevOps” kinda architect, I try to find or come up with cool “pet projects” for myself. This time, I did a nice an easy Terraform VMware vSphere Provider and Slack integration using webhooks.

The goal of this integration was to play with Slack webhooks a bit and the fact that I am learning Terraform in the past few months and embraced it in my home lab is just a bonus. You can also read about a previous vROps and Slack integration I did a few months back. Now, one might think “why the hell should do this sort of integration?” but for me, it’s a great way to learn stuff and also I have few customers that might find it cool and useful.

The requirement is very straightforward. Make Slack notify me upon new VM creation in my home lab using Terraform. Now, this is not a Terraform “Walkthrough” and I’m assuming you have some basic knowledge around Terraform, YAML file type and stuff. If not, don’t you worry about it as the guys at Terraform did a very good job on the documentation part of things.

For this integration, I will be using both the VMware vSphere provider to create well, the virtual machine and the local-exec provisioner to run the curl command which will trigger the webhook notification in Slack.

Configuring Slack Webhooks 

Kinda funny but when I started to draft this post, my friend Eric has also written a post around Slack webhooks so for the first part you can go ahead and read Eric’s post are just stay here.

OK, so before configuring webhooks, let’s have a look at my home lab Slack team. As you can see, nothing fancy, just a standard team with my “terraform” channel.

To configure webhooks, you can simply click on “Add an app or custom integration” link in the channel or do it via the “Apps & Integration” menu.

Search for “webhooks” and select the “Incoming WebHooks” app.

Select to add a configuration and choose the channel you want to use. This will generate a unique WebHook URL which we will use later in to publish the Slack notifications.

As mentioned, in my case, every time a VM will get created by Terraform I will want to receive a notification so I choose the #terraform channel.
Later, I will show you how to post notifications to different channels without the need to configure new webhook each time.

Custom Emoji

Let’s add some personal touch and upload Terraform logo PNG file and make an emoji out it so every notification coming from Terraform will also have a nice graphical representation. Browse to https://your_team_name.slack.com/customize/emoji and upload the file which will become handy later on.

Slack Notification json file

In order to make things more intuitive, we need to create a json file (create it in your terraform folder within your client) which will hold the Slack notification variables and will get pass through the POST notification using curl. Let’s go over the variables.

"username": "Terraform",
"channel": "#terraform",
"icon_emoji": ":terraform:",
"text": "VM deployment is all done, good stuff!"

“username” – This is the user name which will be shown in Slack once the notification will pop out.

“channel” – Remember I told you are not bound to a single Slack channel? Well, this is where you configure it. In my case, the channel name is simply #terraform.

“icon_emoji” – self-exploratory. Just add the emoji name created in the previous part.

“text” – The notification text.

As you can see, I have my slack_webhook.json file saved in my terraform folder.

Provisioning a VM and send a Webhook

I will not go over all of Terraform YAML syntax and concepts but let me try to explain what I have in my main.tf terraform file. First, you should also be aware that I have a terraform variables file where I keep my vCenter address, user name and password. Those variables will be used in the main.tf file. You can read more about input variables here.

The is the primary manifest file which will describe what is it you wanna do with Terraform. You can point to other files as well but this is out of scope for this post.

Before touching the actual terraform plan & apply commands, we need to export the webhook URL into a variable.

Note: The export method is not permanent meaning every time your Terraform client machine restart or even the shell session is closed you are losing that variable. Check out this Stack Overflow thread to make it permanent.

export SLACK_WEBHOOK=your_webhook_url

As you can see, in the main.tf file I have two logical parts here. The first part is all about the vSphere provider which is used to create a new VM folder and then provision a Windows 10 VM out of an existing template I have inside this folder. The rest is all standard provisioning parameters which you can read all about in the documentation.

In the second part, I am using the local-exec provisioner in order to run the curl POST command. This will to call the json file and post it’s content to the URL variable we’ve just configured as a Slack notification.

As mentioned in the Terraform docs, the local-exec commands need to be located under the resource you are provisioning, vSphere VM in this case.

# Configure the VMware vSphere Provider
provider "vsphere" {
user = "${var.vsphere_user}"
password = "${var.vsphere_password}"
vsphere_server = "${var.vsphere_server}"

# if you have a self-signed cert
allow_unverified_ssl = true

# Create a folder
resource "vsphere_folder" "Automation_Testing" {
path = "Orchestration, Automation & CM/Automation_Testing"
datacenter = "xLab Datacenter"

# Create a VM
resource "vsphere_virtual_machine" "Win10" {
name = "Desktop-01"
folder = "Orchestration, Automation & CM/Automation_Testing"
vcpu = 2
memory = 4096
datacenter = "xLab Datacenter"
cluster = "xLab Management Cluster"
skip_customization = "false"

network_interface {
label = "VM Network"
ipv4_address = ""
ipv4_prefix_length = "24"
ipv4_gateway = ""

disk {
datastore = "xLab-Synology-NFS"
template = "VM Templates/Windows 10 Enterprise x64"

# Run the POST command to send Slack notification
provisioner "local-exec" {
command = "curl -X POST -H 'Content-type: application/json' -d @slack_webhook.json $SLACK_WEBHOOK"


A simple terraform plan command will show you all that is about to happen. A new VM folder will be created with a new Windows 10 VM called “Desktop-01” in it followed by a couple of command executions which will eventually trigger a Slack notification. The only thing left to do is to execute the terraform apply command and let Terraform, vSphere and Slack automation do its thing.

 So there you have it. A new vSphere VM using Terraform with a notification on Slack, coolness?

vBrownbag API Series

I wanted to take the opportunity and bring your attention to a new API Series the good people of vBrownbag are kicking off after the US VMworld 2017 event. This should be a great series and I also have the honor to have a joint Terraform session with my good buddy Byron Schaller.



Please enter your comment!
Please enter your name here