Hacknite

Share on:

Instructions and an example of how to build a custom Terraform provider in a Docker. Comes with instructions to install a sample web application that Terraform will interact with.

The walk-through process below will let you compile a custom Terraform provider and interact with a sample web application.

Assumptions and Format

The format used below assumes your provider is leveraging the following syntax.

Obviously if you want to use your build your own custom Terraform provider, you can replace "vmware.com", "edu" and "ctoa" as per your preference.

For more details, check out the details on the HashiCorp website.

1terraform {
2  required_providers {
3    ctoa = {
4      source  = "vmware.com/edu/ctoa"
5    }
6  }
7}

Requirements:

Docker installed on your machine

0. Start the container

1docker run -p 5901:5901 -p 6901:6901 -p 80:80 --user 0 adeleporte/terraform-hacknite:latest

and open a desktop in a browser

1http://127.0.0.1:6901/?password=vncpassword in a browser to get a full desktop with all requirements set (Visual Studio, GO, Terraform, Git)

docker

or you can use the vnc client embedded in Mac

  • go to Finder
  • click Go, then Connect to server
  • enter vnc://127.0.0.1:5901
  • password is vncpassword

finder connect vnc

1. Cloning the repo:

From a terminal:

Clone the following repo with the following command:

1git clone https://github.com/adeleporte/ctoa-hacknite.git

Navigate to the Terraform folder:

1cd ctoa-hacknite/terraform-provider-ctoa

Create a folder where the compiled provider will be moved to.

1mkdir -p ~/.terraform.d/plugins/vmware.com/edu/ctoa/0.1/linux_amd64

2. Compiling the provider and moving to the correct folder:

Compile the provider:

1go build -o terraform-provider-ctoa

Move the provider to the correct location:

1mv terraform-provider-ctoa ~/.terraform.d/plugins/vmware.com/edu/ctoa/0.1/linux_amd64/terraform-provider-ctoa

3. Start the webserver

To start the webserver, go to the ctoa-web folder:

1cd ../frontend/dist/ctoa-web

And start the web server:

1./cto-api-linux

web server

Don't close the windows above.

Go to your browser on 127.0.0.1 and you should see a basic webserver.

web_server_pre_apply

4. Deploy and manager resources with your custom Terraform provider

From Visual Studio Code, navigate back to the Terraform folder:

1cd ctoa-hacknite/terraform-provider-ctoa`

Assuming you're in terraform-provider-ctoa and in the same folder as the main.tf file, the initialization should work:

1terraform init

Update the main.tf file with more resources. With this Terraform provider, every 'resource' you will create is a user, with a first name and a last name. For example:

1resource "ctoa_people" "adeleporte" {
2  first_name = "Antoine"
3  last_name = "Deleporte"
4}
5
6resource "ctoa_people" "nvibert" {
7  first_name = "Nico"
8  last_name = "Vibert"
9}

In the main.tf file, we refer to the host as the webserver (127.0.0.1 is the client itself). That's the webserver currently running.

In your Terraform terminal, once the main.tf file is updated with the resources you are creating, do a:

1terraform plan

And a:

1terraform apply

And you should see new entries added to the table on the webserver.

web_server_apply

A terraform destroy will remove all entries from the table.

Try to modify the resources by changing the first name or the second name of the resources. When you run a terraform plan, you can see that the resource is not deleted and re-created by the change, but updated-in-place.

Well done for building your own custom Terraform provider!