How to develop a custom Terraform provider@Merpay SRE Tech Talk

46fdd2ebc85d68659b83d5eb5c6a49aa?s=47 Keke
March 19, 2020

How to develop a custom Terraform provider@Merpay SRE Tech Talk

46fdd2ebc85d68659b83d5eb5c6a49aa?s=128

Keke

March 19, 2020
Tweet

Transcript

  1. Merpay SRE keke “How to develop a custom terraform provider”

  2. 1. What’s custom Terraform provider 2. How to develop your

    first Terraform provider 3. Making it better “Content”
  3. Whoever listens this presentation can 1. Understand how the Terraform

    is using Terraform provider to manage resources 2. Develop custom Terraform provider “Goal”
  4. “What’s custom Terraform provider”

  5. When $ terraform apply… Terraform Deep dive !

  6. When $ terraform apply… Terraform Deep dive !

  7. Terraform Terraform Core Terraform Plugin When $ terraform apply…

  8. Terraform Terraform Core Terraform Provider When $ terraform apply… Terraform

    Provisioner
  9. Terraform Terraform Core Terraform Provider When $ terraform apply… Terraform

    Provisioner “Basic logic” “Logic between the service and how to manage” “Logic of how to provision”
  10. Example: Terraform Provider 3 kinds of Terraform provider

  11. Example: Terraform Provider ɾNull resource ɾSecret ɾ… 3 kinds of

    Terraform provider 1. Built-in provider
  12. Example: Terraform Provider ɾNull resource ɾSecret ɾ… 3 kinds of

    Terraform provider 1. Built-in provider ɾterraform-provider-aws ɾterraform-provider-google ɾterraform-provider-azure ɾterraform-provider-datadog ɾ… 2. Distributed provider by Hashicorp Needs review by Hashicorp
  13. Example: Terraform Provider ɾNull resource ɾSecret ɾ… 3 kinds of

    Terraform provider 1. Built-in provider ɾterraform-provider-aws ɾterraform-provider-google ɾterraform-provider-azure ɾterraform-provider-datadog ɾ… 2. Distributed provider by Hashicorp 3. Third party provider ɾterraform-provider-keke ɾ… ɾterraform-provider-xxxx Needs review by Hashicorp You can built your own one!!! = Custom Terraform Provider
  14. Example: Terraform Provisioner ɾLocal-exec: Execute command

  15. Example: Terraform Provisioner ɾchef: Install chef agent

  16. Example: Terraform Provisioner ɾMore!!!

  17. Example: Terraform Provisioner ɾMore!!! Note You can not build a

    custom Terraform Provisioner
  18. Terraform Terraform Core When $ terraform apply… Terraform Provisioner Terraform

    Provider
  19. Terraform Terraform Core When $ terraform apply… 1.Request PRC 2.

    Actual PRC Implemented in the Provider 3. Result 4. Store 5. Retrieve 6. Result tfstate Terraform Provisioner Terraform Provider
  20. When $ terraform apply… Any API Terraform Terraform Core Terraform

    Provider 1.Request PRC 2. Actual PRC Implemented in the Provider 3. Result 4. Store 5. Retrieve 6. Result tfstate Terraform Provisioner Custom
  21. Whoever listens this presentation can 1. Understand how the Terraform

    is using Terraform provider to manage resources 2. Develop custom Terraform provider “Goal”
  22. “How to develop your first terraform provider”

  23. “Understand the rules”

  24. “Understand the Provider’s principles”

  25. 1. One provider should focus on one service A custom

    provider (terraform-provider-god) API A API B API C
  26. A custom provider (terraform-provider-god) API A API B API C

    1. One provider should focus on one service
  27. A custom provider (terraform-provider-api-a) API A API B API C

    A custom provider (terraform-provider-api-b) A custom provider (terraform-provider-api-c) 1. One provider should focus on one service
  28. 2. Seperate API Client package A custom provider (terraform-provider-god) API

    B API Client package No unit tests or PRC logic in the provider
  29. “Understand the Resource’s principles”

  30. 1. One resource should focus on one object in API

    A resource API for Object A API for Object B API for Object C .tf
  31. 1. One resource should focus on one object in API

    A resource API for Object A API for Object B API for Object C .tf
  32. 1. One resource should focus on one object in API

    resource_a .tf API for Object A API for Object B API for Object C resource_b .tf resource_c .tf
  33. 2. Follow schema to the actual API object API for

    xxxxxx { “name”: “xxxx”, “email”: “xxxx” } API field
  34. “Directory Style”

  35. “All providers are same directory structure” ← Define this custom

    Terraform provider so that Terraform core can import ← Define the provider ← Define the resource
  36. “Implement Provider”

  37. “Minimum provider”

  38. “Minimum provider: main.go”

  39. “Minimum provider: {NAME}/provider.go”

  40. “Minimum provider: build & use” 1. Run $ go build

    2. Create tf file for your provider 3. Run terraform init
  41. “Minimum provider: build & use” 1. Run $ go build

    2. Create tf file for your provider 3. Run terraform init
  42. “Implement Resource”

  43. “Implement Resouce: Define Schema”

  44. “Implement Resouce: Define Schema”

  45. “Implement Resouce: Define CRUD”

  46. “Implement Resouce: Define CRUD” CRUD syncs with tfstate by ID

    Every resource must have ID
  47. “Minimum provider: build & use” 1. Run $ go build

    2. Create tf file for your provider 3. Run terraform init
  48. “How can we create diffs?”

  49. “How can we create diffs?” Store in the tfstate by

    d.Set()
  50. “Minimum provider: build & use” 1. Run $ go build

    2. Create tf file for your provider 3. Run terraform init
  51. “Minimum provider: build & use” 1. Run $ go build

    2. Create tf file for your provider 3. Run terraform init
  52. Whoever listens this presentation can 1. Understand how the Terraform

    is using Terraform provider to manage resources 2. Develop custom Terraform provider “Goal”
  53. “Making it better”

  54. “Validating attributes” Add ValidateFunc

  55. “Sensitive data” Add Sensitive

  56. “Releasing” Use goreleaser

  57. “Thank you”