Terraform Cloud Project Bootcamp with Andrew Brown — 2.2.0 Provider Block for Custom Terraform Provider

Gwen Leigh
4 min readOct 16, 2023

This article is part of my Terraform journey with Terraform Bootcamp by Andrew Brown and Andrew Bayko with Chris Williams and Shala.

My wild career adventure into Cloud Computing continues with Andrews, and I highly, highly recommend you to come join us if you are interested. Check out Andrews’ free youtube learning contents. You can also buy some paid courses here to support their good cause.

Agenda

Video here: 2 2 0 Provider Block for Custom Terraform Provider

Issue #47 Goals

  • ✅ 1) We configure the provider in main.tf in hcl using our plugin.

Workflow

provider in terraform

Following are the bits of information we need to know (from Terraform Historical Guide).

In Terraform, a “provider” is the logical abstraction of an upstream API.

Terraform supports a plugin model, and all providers are actually plugins. Plugins are distributed as Go binaries. Although technically possible to write a plugin in another language, almost all Terraform plugins are written in Go.

1. Update build_provider script

Run ./bin/builder_provider to test the path and if the binaries are generated correctly.

In the middle of the process, Andrew modifies the path ~ to /home/gitpod to make sure the files are not generated elsewhere. Be sure to check your path is correct.

- PLUGIN_DIR="~/.terraform.d/local.providers/local/terratowns/1.0.0"
+ PLUGIN_DIR="/home/gitpod/.terraform.d/local.providers/local/terratowns/1.0.0"

The library is now ready.

2. Configure our custom provider

Now that we built out our library, we want to configure our terraform.

  • source attribute: we tell terraform where the source of the compiled provider binary is, which is the path where we generated our binaries with the build_provider script (in 2.1.0).
  • Update main.tf at root as below:
# main.tf (at root)
terraform {
required_providers {
terratowns = {
source = "local.providers/local/terratowns"
version = "1.0.0"
}
}
}

provider "terratowns" {
endpoint = "http://localhost:4567"
user_uuid = "e328f4ab-..."
token = "9b49b3fb-..."
}
  • 7:40 Andrew, bless you! 😃

What we are going to do is to writing the details inside the Provider() function block in ./terraform-provider-terratowns/main.go.

Conceptually, this is what we write looks like.

Also have a look at the side-by-side comparison of how we write the plugin under the hood (below, right) and how we use it in the main.tf in HCL (Hashicorp Configuration Language) (below, left).

Programmatically, this is how we actually “configure”.

Then we run the following commands to test our provider:

./bin/build_provider
terraform init

Then we run into two errors:

  • Andrew's error: Error parsing /home/gitpod/.terraformrc
  • My error: Duplicate required providers configuration

2–1. Troubleshoot: Error parsing /home/gitpod/.terraformrc

There was an equal sign (=) missing in the terraformrc file at root.

provider_installation {
filesystem_mirror {
path = "/home/gitpod/.terraform.d/plugins"
+ include = ["local.providers/*/*"]
}
direct {
exclude = ["local.providers/*/*"]
}
}

2–2. Troubleshoot: duplicate required providers

It turns out that earlier at some point, Andrew decided to delete the provider.tf file as we start using the custom “terratowns” provider. If you run into this error, check if you still have this file in your project directory.

For me, simply deleting provider.tf got rid of the issue.

3. Debugging

Terraform provides debugging logs and configuringlog levels will make writing a custom provider a lot easier as it provides much more details for troubleshooting. You can print the debug logs by using the TF_LOG tag:

  • TF_LOG=DEBUG terraform init
More detailed debug logs precede the terraform operation log.

Andrew wants this logging mode to persist so we do some environment configuration:

  • Update terraform configuration in gitpod.yml
  • Export TF_LOG=DEBUG as an env variable.
tasks:
- name: terraform
env:
+ TF_LOG=DEBUG
before: |
cd $PROJECT_ROOT
source ./bin/install_terraform_cli
source ./bin/generate_tfrc_credentials
source ./bin/set_tf_alias
export TF_LOG=DEBUG

Now the DEBUG mode persists. Try running terraform init without the debug variable and the terminal still prints out the debug information.

Q&A

  • 8:40: does the build_provider automatically overwrite the existing file if we run it again without deleting the exiting bin files?

--

--

Gwen Leigh

Cloud Engineer to be. Actively building my profile, network and experience in the cloud space. .