Terraform EKS onboarding

This Terraform configuration deploys a Kubernetes Mavvrik appliance using Helm, with configuration fetched from a remote Mavvrik API.

## Prerequisites

- Terraform (>= 1.0.0)
- kubectl configured with access to your target Kubernetes cluster
- Valid kubeconfig file at `~/.kube/config`
- Helm (>= 3.0.0)


Required Terraform providers

- Helm (~> 2.17.0)

- Kubernetes (~> 2.35.1)

- HTTP (~> 3.4.0)

The following steps are happening in the terraform template

  1. Making An HTTPS call Mavvrik to get the required input to set parameters to deploy helm using terraform, which inputs such as DX API URL, DX tenant ID, provider, and Cloud resource ID for the Kubernetes resource.

    GET API
    URL : "${var.api_url}/${var.tenant_id}/k8s/init/${var.dx_provider}?cloudResourceId=${local.encoded_resource_id}"
    Request Headers : `x-api-key = "${api_key}"`
    Output: {
      "id": "dx cluster id",
      "url": "dx api url",
      "secret": "dx appliance secret"
    }
    
  2. use the output from API to set the helm parameters with other required input parameters such as

Prometheus URL, provider


Sample terraform files

  1. terraform.tfvars

api_key = "--API_KEY--"
tenant_id = "TENANT_ID"
prometheus_endpoint = "http://prometheus-server.prometheus:80"
dx_provider = "gcp"
cloud_resource_id = "//container.googleapis.com/projects/gke-gpu-poc-424311/locations/us-central1-c/clusters/k8s-with-cpu-qa"
  1. variables.tf

variable "tenant_id" {
  description = "tenant ID"
  type        = string
}

variable "cloud_resource_id" {
  description = " cloud resource ID"
  type        = string
}

variable "prometheus_endpoint" {
  description = "Prometheus endpoint to fetch configuration"
  type        = string
}


variable "dx_provider" {
  description = "provider we want to onboard in Mavvrik eg: aws/azure/gcp"
  type        = string
}


variable "api_url" {
  description = "API endpoint to fetch configuration"
  type        = string
  default     = "https://api.mavvrik.ai/"
}


variable "api_key" {
  description = "API Key for authentication to be fetched from Mavvrik Console"
  type        = string
  sensitive   = true
} 
  1. providers.tf

terraform {
  required_providers {
    helm = {
      source  = "hashicorp/helm"
      version = "~> 2.17.0"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.35.1"
    }
    http = {
      source  = "hashicorp/http"
      version = "~> 3.4.0"
    }
  }
}

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

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

provider "http" {}
  1. main.tf

locals {
  encoded_resource_id = replace(var.cloud_resource_id, "/", "%2F")
}

# GET call to fetch configuration
data "http" "get_config" {
  url = "${var.api_url}/${var.tenant_id}/k8s/init/${var.dx_provider}?cloudResourceId=${local.encoded_resource_id}"
  
  request_headers = {
    x-api-key = "${var.api_key}"
    Accept = "*/*"
  }
}
output "api_debug" {
  value = {
    status_code = data.http.get_config.status_code
    response_body = data.http.get_config.response_body
    response_headers = data.http.get_config.response_headers
  }
}

# Add

# Parse the JSON response
locals {
  config = jsondecode(data.http.get_config.response_body)
}

# Debug output to verify parsed values
output "parsed_config" {
  value = local.config
}

# Deploy using Helm
resource "helm_release" "dx_k8s_appliance" {
  name                  = "dx-k8s-appliance"
  repository            = "oci://us-central1-docker.pkg.dev/cloudwiz-io/public-charts"
  chart                 = "dx-k8s-appliance"
  namespace             = "mavvrik"
  create_namespace      = true

  set {
    name  = "config.tenantId"
    value = var.tenant_id
  }

  set {
    name  = "config.clusterId"
    value = local.config.id  # From API response
  }

  set {
    name  = "config.apiUrl"
    value = var.api_url
  }

  set {
    name  = "config.apiKey"
    value = local.config.secret  # From API response
  }

  set {
    name  = "config.prometheusEndpoint"
    value = var.prometheus_endpoint
  }

  set {
    name  = "config.provider"
    value = var.dx_provider
  }
}


# Debug outputs
output "helm_values" {
  sensitive = true  # Since it contains apiKey
  value = {
    repository = "oci://us-central1-docker.pkg.dev/cloudwiz-io/public-charts/"
    tenant_id  = var.tenant_id
    cluster_id = local.config.id
    api_key    = local.config.secret
    api_url    = var.api_url
    provider   = var.dx_provider
  }
}


  1. Initialize Terraform

    terraform init
    
  2. Review the execution plan

    terraform plan
    
  3. Apply the configuration

    terraform apply