Skip to content

Instantly share code, notes, and snippets.

@joatmon08
Last active March 8, 2024 04:38
Show Gist options
  • Save joatmon08/35cf686eac27508c56cbbb6bf7084e48 to your computer and use it in GitHub Desktop.
Save joatmon08/35cf686eac27508c56cbbb6bf7084e48 to your computer and use it in GitHub Desktop.
Terraform v0.12.0 Rundown

Terraform v0.12.0 Rundown

Informal list of references, useful links, and examples referencing Terraform 0.12 related patterns and examples.

Links

Commands

  • With v0.11.14, terraform 0.12checklist checks providers in a module for compatibility.

  • Use terraform fmt for formatting files. The HCL extensions on VSCode may not be updated.

Examples

if-else

Reference

Checking Strings

resource "kubernetes_namespace" "example" {
  count = var.namespace == "default" || var.namespace == "kube-system" || var.namespace == null ? 0 : 1
  metadata {
    name = var.namespace
  }
}

Checking Booleans

Snippet in variables.tf

variable "enable" {
  default = false
}

Snippet in main.tf

resource "kubernetes_service" "zk_client" {
  count = var.enable ? 1 : 0

  metadata {
    name      = "${var.name}-client"
    namespace = var.namespace
    labels = {
      app = var.name
    }
  }

  spec {
    selector = {
      app = var.name
    }

    port {
      port = var.ports.client.port
      name = var.ports.client.name
    }
  }
}

Dynamic Blocks + Rich Types

Snippet in variables.tf

variable "containers" {
  type = list(object({
    name  = string,
    image = string,
    port  = number
  }))
}

Snippet in main.tf

dynamic "container" {
  for_each = var.containers
  content {
    name  = container.value.name
    image = container.value.image
    port {
      container_port = container.value.port
    }
    security_context {
      allow_privilege_escalation = false
    }
  }
}

Strings

Create Strings Using Count & Generalized Splat

Reference

resource "null_resource" "hosts" {
  count = var.replicas

  triggers = {
    zk_hosts = format("%s-%d:%d", var.name, count.index, var.ports.client.port)
  }
}

resource "kubernetes_config_map" "zk_connection_string" {
  metadata {
    name      = "${var.name}-connection-string"
    namespace = var.namespace
  }

  data = {
    hosts = join(",", null_resource.hosts.*.triggers.zk_hosts)
  }
}

Create Strings from Template Syntax

Reference

resource "null_resource" "numbered_hosts" {
  count = var.replicas

  triggers = {
    hosts = format("%s-%d", var.name, count.index)
  }
}

resource "kubernetes_config_map" "zk_config" {

  metadata {
    name      = "${var.name}-zoo-cfg"
    namespace = var.namespace
  }

  data = {
    config = <<EOT
tickTime=2000
dataDir=/var/zookeeper
clientPort=${var.ports.client.port}
initLimit=5
syncLimit=2
%{for i, host in null_resource.numbered_hosts.*.triggers.hosts~}
server.${i}=${host}:${var.ports.server.port}:${var.ports.leader_election.port}
%{endfor}
EOT
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment