Testing Terraform Providers
If there’s one piece of technology I’ve come to love and depend upon these last years it definitely is Terraform. Sadly, the only provider that seems to be complete is the AWS provider, but others seems to be missing some useful resources or data sources. As an example, these past days at work I had to work with the Azure provider and found that I was really missing the ability to query Azure for Virtual Machine IDs, but there wasn’t a data source for this, and I didn’t want to import the virtual machines we’ve already created (insert long story reasons here).
But then I remember that Terraform and its providers are written in Go, so I took it to add the resource myself.
This post isn’t about how to write providers, the folks at Hashicorp already wrote a guide to write custom providers which is pretty useful. But I’ve found that there was something missing, or that I didn’t fully understood, and that was how can I test my changes are working?.
Most if not all providers have unit and acceptance tests that allow to test that the changes we introduced work as expected, which are great for once you want to send a pull request, but I am the type of programmer that likes to try things in the Real World™ before going into the TDD workflow, so I fired up my editor and start hacking on adding a new data source that would allow me to query a virtual machine ID by name. After a few tries, I’ve got something that looked about right, but then the question came: how do I test it? If I run terraform init
it uses the published provider, which obviously doesn’t have the changes I made. I’ve found in the documentation some references as to where can you place third-party providers on your machine for testing, but that didn’t really work as it was missing, IMO, some information. Luckily, I’ve found some pointers in another document that explains how Terraform works, but that was still missing some information. After a while, I’ve found the solution, and here it is:
- Git clone Azure Terraform provider source code.
- Make the changes.
- Run
go build
. This will generate aterraform-provider-azurerm
executable in your current directory. - Move the executable to the discovery folder:
mv terraform-provider-azurerm ~/.terraform.d/plugins/darwin_amd64/
- Go to a folder with some Terraform configuration that uses the Azure provider.
- Remove the cached version:
rm -rv .terraform/plugins/
(this will remove all plugins, but don’t worry) - Run
terraform init
- Profit!
Now I can test my changes with a live Terraform configuration.