GithubHelp home page GithubHelp logo

build-on-aws / custom-provider-with-terraform-plugin-framework Goto Github PK

View Code? Open in Web Editor NEW
8.0 24.0 2.0 312 KB

This repository contains a complete implementation of a custom provider built using HashiCorp's latest SDK called Terraform plugin framework.

License: MIT No Attribution

Makefile 2.89% Go 97.11%
hashicorp hashicorp-terraform opensearch plugin-framework terraform opensearch-go

custom-provider-with-terraform-plugin-framework's Introduction

Custom Provider with Terraform Plugin Framework

This repository contains a complete implementation of a custom provider built using the Terraform plugin framework. It is used to teach, educate, and show the internals of a provider built with the latest SDK from HashiCorp. Even if you are not looking to learn how to build custom providers, you may dial your troubleshooting skills to an expert level if you learn how one works behind the scenes. Plus, this provider is lots of fun to play with. The provider is called buildonaws and it allows you to maintain characters from comic books such as heros, super-heros, and villains.

Requirements

⚙️ Building the provider

The first thing you need to do to use this connector is to build it.

  1. Install the following dependencies:
  1. Execute the build file of the provider.
make install

💡 A file named ~/.terraform.d/plugins/aws.amazon.com/terraform/buildonaws/1.0/${OS_ARCH}/terraform-provider-buildonaws will be created. This is your custom provider.

⬆️ Starting the provider backend

The provider uses OpenSearch as the backend service to store and search for characters. Therefore, before playing with the provider, you first need to get OpenSearch up-and-running. For simplicity, this repository contains a docker-compose.yml file that you can use to execute OpenSearch as a container.

  1. Install the following dependencies:
  1. Start the containers using Docker Compose.
docker compose up -d

Wait until the container opensearch is started and healthy.

⏯ Playing with the provider

  1. Install the following dependencies:
  1. Enter the examples directory.
cd examples
  1. Initialize the provider plugins.
terraform init
  1. Check the execution plan.
terraform plan

Once the command completes, you should see the following:

Terraform will perform the following actions:

  buildonaws_character.daredevil will be created
  + resource "buildonaws_character" "daredevil" {
      + fullname     = "Daredevil"
      + id           = (known after apply)
      + identity     = "Matt Murdock"
      + knownas      = "The man without fear"
      + last_updated = (known after apply)
      + type         = "super-hero"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + daredevil_secret_identity = "The secret identity of Daredevil is 'Matt Murdock'"
  + deadpool_is_knownas       = " is also known as ''"
╷
│ Warning: Datasource was not loaded
│ 
│   with data.buildonaws_character.deadpool,
│   on example.tf line 13, in data "buildonaws_character" "deadpool":
│   13: data "buildonaws_character" "deadpool" {
│ 
│ Reason: no character with the identity 'Wade Wilson'.

Note the warning in the end. This means that the data-source from the provider was not able to find any characters stored in OpenSearch whose identity is Wade Wilson. To create this character in the backend, execute the following command:

sh deadpool.sh

Check again:

terraform plan

Once the command completes, you should have no more warnings.

Terraform will perform the following actions:

  buildonaws_character.daredevil will be created
  + resource "buildonaws_character" "daredevil" {
      + fullname     = "Daredevil"
      + id           = (known after apply)
      + identity     = "Matt Murdock"
      + knownas      = "The man without fear"
      + last_updated = (known after apply)
      + type         = "super-hero"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + daredevil_secret_identity = "The secret identity of Daredevil is 'Matt Murdock'"
  + deadpool_is_knownas       = "Deadpool is also known as 'Merch with a mouth'"
  1. Apply the configuration
terraform apply -auto-approve

Once the command completes, you should see the outputs:

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

daredevil_secret_identity = "The secret identity of Daredevil is 'Matt Murdock'"
deadpool_is_knownas = "Deadpool is also known as 'Merch with a mouth'"

Once you are done playing with the provider, you can destroy the resources created using:

terraform destroy -auto-approve

🪲 Debugging the provider

This is actually an optional step, but if you wish to debug the connector code to learn its behavior by watching the code executing line by line, you can do so by using delve.

  1. Create a file named .vscode/launch.json with the following content:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Provider",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceFolder}",
            "env": {},
            "args": [
                "-debug",
            ]
        }
    ]
}
  1. Set one or multiple breakpoints throughout the code.
  2. Launch a new debugging session in Visual Studio Code.
  3. In the debug console output you will see the following:
Provider started. To attach Terraform CLI, set the TF_REATTACH_PROVIDERS environment variable with the following:

	TF_REATTACH_PROVIDERS='{"aws.amazon.com/terraform/buildonaws":{"Protocol":"grpc","ProtocolVersion":6,"Pid":00000,"Test":true,"Addr":{"Network":"unix","String":"/var/folders/jp/8jhflhbx6fj9_br8dwlfxn780000gr/T/plugin1062046873"}}}'

💡 Please note that your TF_REATTACH_PROVIDERS environment variable will differ from what is shown here. Please don't copy the example above. You need to copy the one generated in your machine.

  1. Attach the Terraform CLI to the debugger
export TF_REATTACH_PROVIDERS='{"aws.amazon.com/terraform/buildonaws":{"Protocol":"grpc","ProtocolVersion":6,"Pid":00000,"Test":true,"Addr":{"Network":"unix","String":"/var/folders/jp/8jhflhbx6fj9_br8dwlfxn780000gr/T/plugin1062046873"}}}'
  1. Run any Terraform commands (plan, apply, destroy) that may trigger the breakpoints.

⬇️ Stopping the provider backend

  1. Stop the containers using Docker Compose.
docker compose down

Security

See CONTRIBUTING for more information.

License

This project is licensed under the MIT-0 License. See the LICENSE file.

custom-provider-with-terraform-plugin-framework's People

Contributors

amazon-auto avatar chrismarget avatar riferrei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.