> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ankra.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Ankra Terraform Provider

> Manage Ankra clusters, stacks, manifests, and add-ons as infrastructure-as-code with the Ankra Terraform provider.

The Ankra Terraform provider lets you manage Kubernetes clusters, stacks, manifests, and add-ons on the Ankra platform using Terraform.

## Example Usage

```hcl theme={null}
terraform {
  required_providers {
    ankra = {
      source  = "ankraio/ankra"
      version = "0.1.5"
    }
  }
}

provider "ankra" {
  ankra_token = var.ankra_token
}
```

## Provider Schema

| Name         | Type   | Required | Description                                  |
| ------------ | ------ | -------- | -------------------------------------------- |
| ankra\_token | String | Yes      | The API token for authenticating with Ankra. |

***

## Pre-configured Stack Example

```hcl theme={null}
terraform {
  required_providers {
    ankra = {
      source  = "ankraio/ankra"
      version = "0.1.5"
    }
  }
}

resource "ankra_cluster" "example" {
  cluster_name            = "dev"
  github_credential_name  = "my-github-cred"
  github_branch           = "main"
  github_repository       = "ankra-io/my-repo"
  ankra_token             = var.ankra_token

  stacks {
    name        = "create-ns-test"
    description = "Test stack for creating a namespace"

    manifests {
      name            = "test-namespace"
      manifest_base64 = base64encode(<<YAML
apiVersion: v1
kind: Namespace
metadata:
  name: test-ns
YAML
      )
    }
    manifests {
      name            = "test-configmap"
      manifest_base64 = base64encode(<<YAML
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
  namespace: test-ns
data:
  key: value
YAML
      )
    }
    addons {
      name                 = "test-addon"
      chart_name           = "nginx"
      chart_version        = "1.2.3"
      repository_url       = "https://charts.example.com"
      namespace            = "test-ns"
      # Optional fields:
      # configuration_type  = "values"
      # configuration       = "{}"
      # parents             = []
      # job_configuration   = "{}"
    }
  }
}

output "ankra_cluster_id" {
  value = ankra_cluster.example.cluster_id
}
```

***

## Data Sources

### ankra\_clusters

This data source retrieves a list of clusters from the Ankra platform.

```hcl theme={null}
data "ankra_clusters" "all" {
  ankra_token = var.ankra_token
}
```

### Example: Realistic Monitoring Stack

Suppose you have these files in your module directory:

`manifests/grafana-namespace.yaml`:

```yaml theme={null}
apiVersion: v1
kind: Namespace
metadata:
  name: grafana
```

`manifests/prometheus-namespace.yaml`:

```yaml theme={null}
apiVersion: v1
kind: Namespace
metadata:
  name: prometheus
```

`manifests/loki-namespace.yaml`:

```yaml theme={null}
apiVersion: v1
kind: Namespace
metadata:
  name: loki
```

And values files for each addon, e.g. `add-ons/grafana/values.yaml`, etc.

```hcl theme={null}
resource "ankra_cluster" "monitoring" {
  cluster_name            = "monitoring"
  github_credential_name  = "my-github-cred"
  github_branch           = "main"
  github_repository       = "ankra-io/monitoring"
  ankra_token             = var.ankra_token

  stacks {
    name        = "monitoring infrastructure"
    description = "Monitor our entire infra in this kubernetes."

    manifests {
      name      = "grafana-namespace"
      from_file = "${path.module}/manifests/grafana-namespace.yaml"
      parents   = []
    }
    manifests {
      name      = "prometheus-namespace"
      from_file = "${path.module}/manifests/prometheus-namespace.yaml"
      parents   = []
    }
    manifests {
      name      = "loki-namespace"
      from_file = "${path.module}/manifests/loki-namespace.yaml"
      parents   = []
    }

    addons {
      name           = "grafana"
      chart_name     = "grafana"
      chart_version  = "9.3.0"
      repository_url = "https://grafana.github.io/helm-charts"
      namespace      = "grafana"
      configuration_type = "standalone"
      # configuration = filebase64("${path.module}/add-ons/grafana/values.yaml")
      from_file      = "${path.module}/add-ons/grafana/values.yaml"
      parents        = ["grafana-namespace"]
    }
    addons {
      name           = "prometheus"
      chart_name     = "prometheus"
      chart_version  = "27.28.2"
      repository_url = "https://prometheus-community.github.io/helm-charts"
      namespace      = "prometheus"
      configuration_type = "standalone"
      from_file      = "${path.module}/add-ons/prometheus/values.yaml"
      parents        = ["prometheus-namespace"]
    }
    addons {
      name           = "loki"
      chart_name     = "loki"
      chart_version  = "6.33.0"
      repository_url = "https://grafana.github.io/helm-charts"
      namespace      = "loki"
      configuration_type = "standalone"
      from_file      = "${path.module}/add-ons/loki/values.yaml"
      parents        = ["loki-namespace"]
    }
    addons {
      name           = "fluent-bit"
      chart_name     = "fluent-bit"
      chart_version  = "2.6.0"
      repository_url = "https://grafana.github.io/helm-charts"
      namespace      = "loki"
      configuration_type = "standalone"
      from_file      = "${path.module}/add-ons/fluent-bit/values.yaml"
      parents        = ["loki"]
    }
  }
}
```

***

## Complete Example: Using from\_file for Manifests and Addons

Suppose you have these files in your module directory:

`namespace.yaml`:

```yaml theme={null}
apiVersion: v1
kind: Namespace
metadata:
  name: test-ns
```

`secret.yaml`:

```yaml theme={null}
apiVersion: v1
kind: Secret
metadata:
  name: test-secret
  namespace: test-ns
type: Opaque
data:
  username: dXNlcm5hbWU=
  password: cGFzc3dvcmQ=
```

```hcl theme={null}
resource "ankra_cluster" "example" {
  cluster_name            = "dev"
  github_credential_name  = "my-github-cred"
  github_branch           = "main"
  github_repository       = "ankra-io/my-repo"
  ankra_token             = var.ankra_token

  stacks {
    name        = "create-ns-test"
    description = "Test stack for creating a namespace and secret"

    manifests {
      name      = "test-namespace"
      from_file = "${path.module}/namespace.yaml"
      parents    = [] # No parents for the namespace
    }
    manifests {
      name      = "test-secret"
      from_file = "${path.module}/secret.yaml"
      parents    = ["test-namespace"] # This secret depends on the namespace
    }
    addons {
      name           = "my-addon"
      chart_name     = "nginx"
      chart_version  = "1.2.3"
      repository_url = "https://charts.example.com"
      namespace      = "test-ns"
      parents        = ["test-namespace", "test-secret"] # This addon depends on both
    }
  }
}
```

***

## Importing a Cluster

If a cluster is created or modified outside of Terraform (for example, via GitOps, CLI, or the Ankra UI), you can import it into your Terraform state:

```sh theme={null}
terraform import ankra_cluster.example <cluster_id>
```

Replace `<cluster_id>` with the actual ID of the cluster you want to manage with Terraform. After import, run `terraform plan` to review any differences and update your configuration as needed.

```hcl theme={null}
resource "ankra_cluster" "imported" {
  cluster_name           = data.ankra_clusters.all.clusters[0].name
  github_credential_name = "my-github-cred"
  github_branch          = "main"
  github_repository      = "ankra-io/my-repo"
  ankra_token            = var.ankra_token
  # … other fields …
}
```

***

## Provider & Data Source Schema

### Provider

| Name         | Type   | Required | Description                                  |
| ------------ | ------ | -------- | -------------------------------------------- |
| ankra\_token | String | Yes      | The API token for authenticating with Ankra. |

### Data Source: ankra\_clusters

| Name     | Type | Description              |
| -------- | ---- | ------------------------ |
| clusters | List | List of cluster objects. |

#### Cluster Object

| Name | Type   | Description       |
| ---- | ------ | ----------------- |
| id   | String | The cluster ID.   |
| name | String | The cluster name. |

***

For more details, see the [Ankra Terraform Provider Registry](https://registry.terraform.io/providers/ankraio/ankra/latest/docs).
