Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Cloud Examples

Terraform with Kubernetes, Helm, DNS and SaaS Providers

Use Terraform beyond the big clouds: the Kubernetes and Helm providers, Cloudflare DNS, GitHub repositories, provider chaining and the cluster-then-workload pattern.

Terraform manages anything with an API. Besides virtual machines and buckets, that includes Kubernetes objects, DNS records, GitHub repositories, monitoring dashboards and database users. Thousands of providers exist, and the skill is the same each time: read the provider docs, write the resource, let the graph order things. This lesson shows four common non-cloud use cases and one important pattern: creating a cluster and then deploying onto it.

These examples need real services, so the HCL is read-only. The runnable programs model dependency handling and drift between a desired set of objects and what exists.

Kubernetes provider

The kubernetes provider talks to a cluster API server and manages objects such as namespaces, deployments and services:

HCL
provider "kubernetes" {
  config_path = "~/.kube/config"
}

resource "kubernetes_namespace_v1" "app" {
  metadata { name = "shop" }
}

resource "kubernetes_deployment_v1" "web" {
  metadata {
    name      = "web"
    namespace = kubernetes_namespace_v1.app.metadata[0].name
  }
  spec {
    replicas = 3
    selector { match_labels = { app = "web" } }
    template {
      metadata { labels = { app = "web" } }
      spec {
        container {
          name  = "web"
          image = "ghcr.io/acme/web:1.4.2"
          port { container_port = 8080 }
        }
      }
    }
  }
}

Terraform fits infrastructure objects (namespaces, quotas, service accounts, RBAC bindings). For fast-changing application manifests many teams prefer GitOps tools such as Argo CD or Flux; the two approaches combine well, with Terraform creating the cluster and bootstrapping the GitOps controller.

Helm provider

The helm provider installs charts, which is handy for cluster add-ons such as ingress controllers and cert-manager:

HCL
provider "helm" {
  kubernetes {
    config_path = "~/.kube/config"
  }
}

resource "helm_release" "ingress" {
  name             = "ingress-nginx"
  repository       = "https://kubernetes.github.io/ingress-nginx"
  chart            = "ingress-nginx"
  version          = "4.11.2"
  namespace        = "ingress"
  create_namespace = true

  set {
    name  = "controller.replicaCount"
    value = "2"
  }
}

Always pin the chart version; otherwise each plan could pick up a newer chart.

Chaining providers: cluster first, workloads second

A provider can be configured from another resource's attributes, for example the endpoint of a cluster you create in the same run:

HCL
resource "aws_eks_cluster" "main" { /* ... */ }

data "aws_eks_cluster_auth" "main" {
  name = aws_eks_cluster.main.name
}

provider "kubernetes" {
  host                   = aws_eks_cluster.main.endpoint
  cluster_ca_certificate = base64decode(aws_eks_cluster.main.certificate_authority[0].data)
  token                  = data.aws_eks_cluster_auth.main.token
}

This works on the first apply only because Terraform creates the cluster before configuring the provider, but it is fragile: if the cluster must be replaced, the Kubernetes provider is configured with values that are not known yet. The robust pattern is two states: one configuration creates the cluster, a second one reads its outputs (via terraform_remote_state or a data source) and deploys the workloads. A change to the cluster then cannot break the workload plan halfway.

flowchart LR
  subgraph S1["State 1: platform"]
    VPC["VPC"] --> EKS["Cluster"]
  end
  subgraph S2["State 2: workloads"]
    NS["Namespace"] --> DEP["Deployment"]
    NS --> HELM["Helm releases"]
  end
  EKS -.->|"outputs: endpoint, CA"| S2

Separate states keep the provider configuration stable.

Python Two-stage apply with provider readiness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

DNS with Cloudflare

DNS is a perfect fit for Terraform: records are small, versionable and often shared between teams.

HCL
provider "cloudflare" {
  api_token = var.cloudflare_token   # scoped to Zone.DNS edit
}

data "cloudflare_zone" "main" {
  name = "acme.example"
}

resource "cloudflare_record" "www" {
  zone_id = data.cloudflare_zone.main.id
  name    = "www"
  type    = "CNAME"
  content = aws_lb.web.dns_name
  proxied = true
}

Because the record refers to the load balancer's DNS name, one apply creates the infrastructure and points the domain at it. Other DNS providers (Route 53, Azure DNS, Google Cloud DNS) follow the same shape.

GitHub provider

Repositories, branch protection, teams and secrets can be code too, which makes organization-wide settings reviewable:

HCL
provider "github" {
  owner = "acme"
}

resource "github_repository" "infra" {
  name       = "infra"
  visibility = "private"
  has_issues = true
}

resource "github_branch_protection" "main" {
  repository_id          = github_repository.infra.node_id
  pattern                = "main"
  required_linear_history = true
  required_pull_request_reviews { required_approving_review_count = 1 }
}

Managing many similar objects

SaaS providers shine with for_each: create a repository per team, a DNS record per service, a monitor per endpoint.

Python Reconcile desired objects with what exists

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Credentials for SaaS providers

  • Create tokens with the narrowest scope possible and read them from environment variables (CLOUDFLARE_API_TOKEN, GITHUB_TOKEN) instead of tfvars files.
  • Mark provider variables sensitive = true.
  • Rotate tokens and keep them in your CI secret store.
  • Remember: provider attributes in state may hold sensitive data (State Basics).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the safest pattern for creating a cluster and deploying onto it?

    Separate states keep provider configuration stable even if the cluster is replaced.
  2. 2. Why pin the chart version in helm_release?

    Unpinned charts make your plan depend on whatever is newest.
  3. 3. How does one apply point a DNS record at a new load balancer?

    An attribute reference orders creation and passes the value.
  4. 4. Where should a SaaS API token usually come from?

    Environment variables and CI secrets keep tokens out of Git.

Summary

  • Terraform manages any API: Kubernetes objects, Helm charts, DNS records, repositories and more.
  • Use Terraform for infrastructure-like Kubernetes objects and add-ons; consider GitOps for fast-moving app manifests.
  • Create clusters and deploy workloads in separate states to keep provider configuration stable.
  • References between providers' resources (load balancer to DNS) give one-apply wiring.
  • Scope tokens narrowly, read them from the environment and pin chart and provider versions.

Next: Project: Static Website Hosting