Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

1. What’s custom Terraform provider 2. How to develop your first Terraform provider 3. Making it better “Content”

Slide 3

Slide 3 text

Whoever listens this presentation can 1. Understand how the Terraform is using Terraform provider to manage resources 2. Develop custom Terraform provider “Goal”

Slide 4

Slide 4 text

“What’s custom Terraform provider”

Slide 5

Slide 5 text

When $ terraform apply… Terraform Deep dive !

Slide 6

Slide 6 text

When $ terraform apply… Terraform Deep dive !

Slide 7

Slide 7 text

Terraform Terraform Core Terraform Plugin When $ terraform apply…

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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”

Slide 10

Slide 10 text

Example: Terraform Provider 3 kinds of Terraform provider

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Example: Terraform Provisioner ɾLocal-exec: Execute command

Slide 15

Slide 15 text

Example: Terraform Provisioner ɾchef: Install chef agent

Slide 16

Slide 16 text

Example: Terraform Provisioner ɾMore!!!

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Whoever listens this presentation can 1. Understand how the Terraform is using Terraform provider to manage resources 2. Develop custom Terraform provider “Goal”

Slide 22

Slide 22 text

“How to develop your first terraform provider”

Slide 23

Slide 23 text

“Understand the rules”

Slide 24

Slide 24 text

“Understand the Provider’s principles”

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

“Understand the Resource’s principles”

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

2. Follow schema to the actual API object API for xxxxxx { “name”: “xxxx”, “email”: “xxxx” } API field

Slide 34

Slide 34 text

“Directory Style”

Slide 35

Slide 35 text

“All providers are same directory structure” ← Define this custom Terraform provider so that Terraform core can import ← Define the provider ← Define the resource

Slide 36

Slide 36 text

“Implement Provider”

Slide 37

Slide 37 text

“Minimum provider”

Slide 38

Slide 38 text

“Minimum provider: main.go”

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

“Implement Resource”

Slide 43

Slide 43 text

“Implement Resouce: Define Schema”

Slide 44

Slide 44 text

“Implement Resouce: Define Schema”

Slide 45

Slide 45 text

“Implement Resouce: Define CRUD”

Slide 46

Slide 46 text

“Implement Resouce: Define CRUD” CRUD syncs with tfstate by ID Every resource must have ID

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

“How can we create diffs?”

Slide 49

Slide 49 text

“How can we create diffs?” Store in the tfstate by d.Set()

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Whoever listens this presentation can 1. Understand how the Terraform is using Terraform provider to manage resources 2. Develop custom Terraform provider “Goal”

Slide 53

Slide 53 text

“Making it better”

Slide 54

Slide 54 text

“Validating attributes” Add ValidateFunc

Slide 55

Slide 55 text

“Sensitive data” Add Sensitive

Slide 56

Slide 56 text

“Releasing” Use goreleaser

Slide 57

Slide 57 text

“Thank you”