chapter eleven

11 Extending Terraform by Writing a Custom Provider

 

This chapter covers:

  • Developing a custom Terraform provider from scratch
  • Implementing CRUD operations for managed resources
  • Writing acceptance tests for the provider schema and resource files
  • Deploying a serverless API to listen to requests from the provider
  • Building and installing third-party providers

Extending Terraform by writing your own provider is one of the most satisfying things you can do with the technology. It demonstrates a high-level proficiency and grants you the power to bend Terraform to your will. Nevertheless, even the simplest provider is still a considerable investment in time and effort. When might it be worth it to write your own Terraform provider?

Two excellent reasons are:

  1. Wrapping a remote API so you can manage your infrastructure as code
  2. Exposing utility functions to Terraform

Almost all Terraform providers wrap remote API’s, because this is what they were designed to do. Recall from chapter 2 that Terraform Core is essentially a glorified state management engine. Without Terraform providers, Terraform would not know how to provision cloud-based infrastructure. By creating a custom provider, you enable Terraform to manage more and new kinds of resources.

11.1  Blueprints for a Terraform Provider

11.1.1    Terraform Provider Basics

11.1.2    Petstore Provider Architecture

11.2  Writing the Petstore Provider

11.2.1    Setting up the Go Project

11.2.2    Configuring the Provider Schema

11.3  Creating a Pet Resource

11.3.1    Defining Create()

11.3.2    Defining Read()

11.3.3    Defining Update()

11.3.4    Defining Delete()

11.4  Writing Acceptance Tests

11.4.1    Testing the Provider Schema

11.4.2    Testing the Pet Resource

11.5  Build, Test, Deploy

11.5.1    Deploying the Petstore API

11.5.2    Testing and Building the Provider

11.5.3    Installing the Provider

11.5.4    Pets as Code

11.6  Fireside Chat

11.6.1    FAQ

11.7  Summary