Slide 1

Slide 1 text

Copyright © 2020 HashiCorp Code Quality for Terraform

Slide 2

Slide 2 text

Kerim Satirli (He/Him) Developer Advocate at HashiCorp

Slide 3

Slide 3 text

@ksatirli on GitHub and Twitter Developer Advocate at HashiCorp

Slide 4

Slide 4 text

Agenda Terraform-native terraform fmt and terraform validate TFLint and pre-commit Local options to improve code GitHub Actions Validate code on git push

Slide 5

Slide 5 text

Terraform-native code quality tools

Slide 6

Slide 6 text

Command: terraform fmt TERMINAL > terraform fmt provider.tf terraform.tf

Slide 7

Slide 7 text

Command: terraform validate TERMINAL > terraform validate Success! The configuration is valid.

Slide 8

Slide 8 text

TFLint

Slide 9

Slide 9 text

TFLint .tflint.hcl rule "terraform_required_providers" { enabled = true } rule "terraform_required_version" { enabled = true } rule "terraform_naming_convention" { enabled = true format = "snake_case" }

Slide 10

Slide 10 text

Command: tflint TERMINAL > tflint 1 issue(s) found: Warning: data "google_projects" "projects" is declared but not used (terraform_unused_declarations) on data-sources.tf line 11: 11: data "google_projects" "projects" {

Slide 11

Slide 11 text

pre-commit

Slide 12

Slide 12 text

pre-commit .pre-commit-config.yaml --- fail_fast: true minimum_pre_commit_version: "2.6.0" repos: - repo: https://github.com/antonbabenko/pre-commit-terraform rev: v1.31.0 hooks: - id: terraform_fmt - id: terraform_validate

Slide 13

Slide 13 text

Command: pre-commit run TERMINAL > pre-commit run --all-files Terraform fmt............................................Passed Terraform validate.......................................Passed Check for added large files..............................Passed Check for case conflicts.................................Passed Check for merge conflicts................................Passed Check that executables have shebangs....................Skipped Check JSON..............................................Skipped Check for merge conflicts................................Passed Check for broken symlinks...............................Skipped Check vcs permalinks.....................................Passed markdownlint.............................................Passed

Slide 14

Slide 14 text

GitHub Actions

Slide 15

Slide 15 text

GitHub Actions .github/workflows/terraform.yml --- name: "Code Quality: Terraform" on: push: pull_request: env: # `AWS_REGION` must be specified for `terraform validate` AWS_REGION: "xx-xxxx-0" ...

Slide 16

Slide 16 text

GitHub Actions .github/workflows/terraform.yml ... jobs: terraform: name: Terraform runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v2 with: fetch-depth: 1 ...

Slide 17

Slide 17 text

GitHub Actions .github/workflows/terraform.yml ... - name: Setup Terraform uses: hashicorp/setup-terraform@v1 with: terraform_version: "0.12.29" ...

Slide 18

Slide 18 text

GitHub Actions .github/workflows/terraform.yml ... - name: Run `terraform fmt` run: terraform fmt -diff -check -no-color -recursive - name: Run `terraform init` run: terraform init - name: Run `terraform validate` run: terraform validate -no-color

Slide 19

Slide 19 text

GitHub Actions hashi.co/tf-code-quality-gha

Slide 20

Slide 20 text

Review ▪ built-in options: fmt and validate ▪ local options: TFLint and pre-commit ▪ remote options: GitHub Actions

Slide 21

Slide 21 text

Materials ▪ slides: hashi.co/tf-code-quality ▪ code: hashi.co/tf-code-quality-code ▪ forums: hashi.co/tf-forum

Slide 22

Slide 22 text

Thank You [email protected]