Deploy Kong Mesh using Terraform and Konnect

Incompatible with
on-prem
Related Documentation
Minimum Version
Kong Mesh - 3.0
Previous Versions of this page
TL;DR

Use the konnect-beta provider with version = “v3” to create the global control plane, the mesh, and its MeshIdentity. Use the konnect provider for the zone’s system account, the helm and kubernetes providers to install the zone, and a kubernetes_manifest to apply the traffic permission to the zone.

Prerequisites

If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.

  1. The following Konnect items are required to complete this tutorial:
    • Personal access token (PAT): Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  2. Set the personal access token as an environment variable:

    export KONNECT_TOKEN='YOUR KONNECT TOKEN'

This tutorial uses kongctl to manage Konnect resources programmatically. We recommend keeping kongctl up to date with the latest version (1.29.0).

  1. Install kongctl from developer.konghq.com/kongctl.
  2. Verify the installation:

    kongctl version

This how-to requires you to install Terraform.

This guide uses Terraform to create a Kong Mesh 3.x global control plane in Konnect, connect a Kubernetes zone to it, and create a mesh with a traffic permission. It then deploys two test applications and verifies identity-based traffic permissions.

Configure Terraform

  1. Create a working directory and change to it:

    export MESH_TERRAFORM_DIR="$PWD/mesh-terraform"
    mkdir -p "$MESH_TERRAFORM_DIR"
    cd "$MESH_TERRAFORM_DIR"
  2. Set the region and pass your Konnect credentials to Terraform:

    export KONNECT_REGION='us'
    export TF_VAR_konnect_token="$KONNECT_TOKEN"
    export TF_VAR_region="$KONNECT_REGION"
  3. Create providers.tf, which declares the variables and configures every provider this guide uses:

    cat <<'EOF' > providers.tf
    terraform {
      required_providers {
        konnect-beta = {
          source  = "kong/konnect-beta"
          version = "0.22.0"
        }
        konnect = {
          source  = "kong/konnect"
          version = "3.26.0"
        }
        kubernetes = {
          source  = "hashicorp/kubernetes"
          version = "3.2.1"
        }
        helm = {
          source  = "hashicorp/helm"
          version = "3.1.2"
        }
      }
    }
    
    variable "konnect_token" {
      type      = string
      sensitive = true
    }
    
    variable "region" {
      type = string
    }
    
    provider "konnect-beta" {
      personal_access_token = var.konnect_token
      server_url            = "https://${var.region}.api.konghq.com"
    }
    
    provider "konnect" {
      personal_access_token = var.konnect_token
      server_url            = "https://${var.region}.api.konghq.com"
    }
    
    provider "kubernetes" {
      config_path = pathexpand("~/.kube/config")
    }
    
    provider "helm" {
      kubernetes = {
        config_path = pathexpand("~/.kube/config")
      }
    }
    EOF

    Mesh resources are only available in the konnect-beta provider. The konnect provider manages the zone’s system account.

  4. Initialize the working directory:

    terraform init

Protect Terraform state and don’t commit credentials: the state holds the zone token. Keep this directory for later cleanup.

Create the global control plane

  1. Create control-plane.tf:

    cat <<'EOF' > control-plane.tf
    resource "konnect_mesh_control_plane" "demo" {
      provider    = konnect-beta
      name        = "example-cp"
      description = "Mesh 3 installation example"
      version     = "v3"
    }
    
    output "control_plane_id" {
      value = konnect_mesh_control_plane.demo.id
    }
    EOF

    The provider’s control plane resource defaults to v2, so set version = "v3" explicitly.

  2. Review the plan:

    terraform plan
  3. Apply the plan:

    terraform apply
  4. Export the control plane ID from the Terraform output:

    export CONTROL_PLANE_ID=$(terraform output -raw control_plane_id)

Give the zone credentials to connect

The zone uses a system account access token with the Connector role on this control plane.

  1. Set the token to expire in 30 days. Change the number of days if your organization’s token policy requires a shorter lifetime:

    export TF_VAR_zone_token_expires_at=$(jq -nr 'now + 30*24*60*60 | todate')
  2. Create zone-credentials.tf:

    cat <<'EOF' > zone-credentials.tf
    variable "zone_token_expires_at" {
      type = string
    }
    
    resource "konnect_system_account" "zone" {
      name            = "mesh-zone-1"
      description     = "Connect zone-1"
      konnect_managed = false
    }
    
    resource "konnect_system_account_role" "zone_connector" {
      account_id       = konnect_system_account.zone.id
      role_name        = "Connector"
      entity_type_name = "Mesh Control Planes"
      entity_id        = konnect_mesh_control_plane.demo.id
      entity_region    = var.region
    }
    
    resource "konnect_system_account_access_token" "zone" {
      account_id = konnect_system_account.zone.id
      name       = "zone-1"
      expires_at = var.zone_token_expires_at
    }
    EOF
  3. Apply the configuration:

    terraform apply

Deploy the zone control plane

  1. Create zone.tf, which stores the zone token in a Secret and installs the zone control plane with Helm:

    cat <<'EOF' > zone.tf
    resource "kubernetes_namespace_v1" "kong_mesh_system" {
      metadata {
        name = "kong-mesh-system"
      }
    }
    
    resource "kubernetes_secret_v1" "cp_token" {
      metadata {
        name      = "cp-token"
        namespace = kubernetes_namespace_v1.kong_mesh_system.metadata[0].name
      }
    
      data = {
        token = konnect_system_account_access_token.zone.token
      }
    }
    
    resource "helm_release" "kong_mesh" {
      name       = "kong-mesh"
      repository = "https://kong.github.io/kong-mesh-charts"
      chart      = "kong-mesh"
      version    = "3.0.0"
      namespace  = kubernetes_namespace_v1.kong_mesh_system.metadata[0].name
      timeout    = 300
    
      values = [yamlencode({
        kuma = {
          controlPlane = {
            mode             = "zone"
            zone             = "zone-1"
            kdsGlobalAddress = "grpcs://${var.region}.mesh.sync.konghq.com:443"
            konnect = {
              cpId = konnect_mesh_control_plane.demo.id
            }
            zoneToken = {
              secretName = kubernetes_secret_v1.cp_token.metadata[0].name
              key        = "token"
            }
          }
        }
      })]
    }
    EOF

    The zone reads its token from the cp-token Secret through zoneToken. This single-zone test doesn’t need zone proxies; add mesh-scoped zone proxies when connecting zones or using MeshExternalService.

  2. Apply the configuration. The helm_release waits for the zone control plane to be ready, which can take a few minutes:

    terraform apply

Verify the zone connection

Confirm the zone is connected to your global control plane:

kongctl inspect mesh zones \
  --control-plane-id "$CONTROL_PLANE_ID" --region "$KONNECT_REGION" \
  --pat "$KONNECT_TOKEN"

The zone should report an Online status:

NAME    STATUS
zone-1  Online

If a zone is missing or not Online, check:

  • The zone’s control plane logs
  • Outbound access to the KDS address
  • The control plane ID
  • The system account’s Connector role and token

Create a mesh and secure the test traffic

  1. Create mesh.tf, which creates the konnect-demo mesh and a demo identity provider on the global control plane:

    cat <<'EOF' > mesh.tf
    resource "konnect_mesh" "demo" {
      provider = konnect-beta
      cp_id    = konnect_mesh_control_plane.demo.id
      name     = "konnect-demo"
      type     = "Mesh"
    }
    
    resource "konnect_mesh_identity" "demo" {
      provider = konnect-beta
      cp_id    = konnect_mesh_control_plane.demo.id
      mesh     = konnect_mesh.demo.name
      name     = "demo-identity"
      type     = "MeshIdentity"
    
      spec = {
        selector = {
          dataplane = {}
        }
        spiffe_id = {
          trust_domain = "konnect-demo.mesh.local"
        }
        provider = {
          type = "Bundled"
          bundled = {
            autogenerate = {
              enabled = true
            }
            insecure_allow_self_signed = true
            mesh_trust_creation        = "Enabled"
          }
        }
      }
    }
    EOF

    MeshIdentity issues certificates and publishes their CA in MeshTrust. This example accepts a generated self-signed CA for the demo; review your production PKI requirements before reusing it.

  2. Apply the configuration:

    terraform apply
  3. Wait for the identity to be ready and for the trust to be generated:

    kongctl get mesh meshidentities demo-identity --mesh konnect-demo -o yaml \
      --control-plane-id "$CONTROL_PLANE_ID" --region "$KONNECT_REGION" \
      --pat "$KONNECT_TOKEN"
    kongctl get mesh meshtrusts --mesh konnect-demo \
      --control-plane-id "$CONTROL_PLANE_ID" --region "$KONNECT_REGION" \
      --pat "$KONNECT_TOKEN"
  4. Confirm that the new mesh has synced to the Kubernetes zone:

    kubectl get mesh konnect-demo
  5. Create traffic-permission.tf, which applies a MeshTrafficPermission to the zone to allow the client ServiceAccount to call the server workload:

    cat <<'EOF' > traffic-permission.tf
    resource "kubernetes_manifest" "client_to_server" {
      manifest = {
        apiVersion = "kuma.io/v1alpha1"
        kind       = "MeshTrafficPermission"
        metadata = {
          name      = "client-to-server"
          namespace = kubernetes_namespace_v1.kong_mesh_system.metadata[0].name
          labels = {
            "kuma.io/mesh"   = konnect_mesh.demo.name
            "kuma.io/origin" = "zone"
          }
        }
        spec = {
          targetRef = {
            kind = "Dataplane"
            labels = {
              app = "server"
            }
          }
          rules = [
            {
              default = {
                allow = [
                  {
                    spiffeID = {
                      type  = "Exact"
                      value = "spiffe://konnect-demo.mesh.local/ns/kong-mesh-demo/sa/client"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    
      depends_on = [helm_release.kong_mesh]
    }
    EOF

    kubernetes_manifest reads the MeshTrafficPermission CRD from the cluster when Terraform plans, so it can only be applied once the zone control plane from Deploy the zone control plane has installed the CRDs.

  6. Apply the configuration:

    terraform apply

Deploy the test applications

Deploy an nginx server and a curl client into a namespace that joins the mesh. The client’s own ServiceAccount gives it the identity that the traffic permission allows. The client also lists server as a reachable backend, because a proxy can only call the destinations it declares. For more information, see MeshService.

  1. Create the namespace, the client ServiceAccount, and the server and client workloads:

    cat <<'EOF' | kubectl apply -f -
    apiVersion: v1
    kind: Namespace
    metadata:
      name: kong-mesh-demo
      labels:
        kuma.io/mesh: konnect-demo
        kuma.io/sidecar-injection: enabled
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: client
      namespace: kong-mesh-demo
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: server
      namespace: kong-mesh-demo
    spec:
      selector:
        app: server
      ports:
        - port: 80
          targetPort: 80
          appProtocol: http
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: server
      namespace: kong-mesh-demo
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: server
      template:
        metadata:
          labels:
            app: server
        spec:
          containers:
            - name: server
              image: nginx:1.27.4
              ports:
                - containerPort: 80
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: client
      namespace: kong-mesh-demo
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: client
      template:
        metadata:
          labels:
            app: client
          annotations:
            kuma.io/reachable-backends: |
              refs:
              - kind: MeshService
                labels:
                  kuma.io/display-name: server
                  k8s.kuma.io/namespace: kong-mesh-demo
        spec:
          serviceAccountName: client
          containers:
            - name: client
              image: curlimages/curl:8.12.1
              command: ["sleep", "86400"]
    EOF
  2. Wait for the server and client to roll out:

    kubectl -n kong-mesh-demo rollout status deployment/server --timeout=180s
    kubectl -n kong-mesh-demo rollout status deployment/client --timeout=180s

Validate

  1. Check that both workloads have ready mesh sidecars:

    kubectl -n kong-mesh-demo get pods
  2. Check that server has a generated MeshService:

    kongctl get mesh meshservices --mesh konnect-demo \
      --control-plane-id "$CONTROL_PLANE_ID" --region "$KONNECT_REGION" \
      --pat "$KONNECT_TOKEN"
  3. Confirm the workload identities in the proxy inspection output:

    kongctl inspect mesh dataplanes --mesh konnect-demo \
      --control-plane-id "$CONTROL_PLANE_ID" --region "$KONNECT_REGION" \
      --pat "$KONNECT_TOKEN"
  4. Send a request from the client to the server:

    kubectl -n kong-mesh-demo exec deployment/client -c client -- \
      curl --fail --max-time 10 http://server.kong-mesh-demo.svc.cluster.local/

    You should get the nginx welcome page.

  5. For a negative check, change the client’s ServiceAccount to default and wait for the replacement Pod:

    kubectl -n kong-mesh-demo patch deployment client --type=merge \
      -p '{"spec":{"template":{"spec":{"serviceAccountName":"default"}}}}'
    kubectl -n kong-mesh-demo rollout status deployment/client --timeout=180s
  6. Repeat the same request:

    kubectl -n kong-mesh-demo exec deployment/client -c client -- \
      curl --fail --max-time 10 http://server.kong-mesh-demo.svc.cluster.local/

    The request fails, because the permission doesn’t allow the /sa/default identity.

  7. Restore the allowed identity and wait for the replacement Pod:

    kubectl -n kong-mesh-demo patch deployment client --type=merge \
      -p '{"spec":{"template":{"spec":{"serviceAccountName":"client"}}}}'
    kubectl -n kong-mesh-demo rollout status deployment/client --timeout=180s
  8. Confirm the request succeeds again:

    kubectl -n kong-mesh-demo exec deployment/client -c client -- \
      curl --fail --max-time 10 http://server.kong-mesh-demo.svc.cluster.local/

If the results differ, see the MeshTrafficPermission reference.

Cleanup

Delete the test namespace:

kubectl delete namespace kong-mesh-demo

Review the destroy plan:

terraform plan -destroy

Destroy every resource this guide created with Terraform: the traffic permission, the zone control plane and its namespace, the mesh and its identity, the global control plane, and the zone’s system account:

terraform destroy

Leave the Terraform working directory and delete it:

cd "$(dirname "$MESH_TERRAFORM_DIR")"
rm -rf "$MESH_TERRAFORM_DIR"

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!