|
| 1 | +--- |
| 2 | +layout: "backend-types" |
| 3 | +page_title: "Backend Type: Kubernetes" |
| 4 | +sidebar_current: "docs-backends-types-standard-kubernetes" |
| 5 | +description: |- |
| 6 | + Terraform can store state remotely in Kubernetes and lock that state. |
| 7 | +--- |
| 8 | + |
| 9 | +# kubernetes |
| 10 | + |
| 11 | +**Kind: Standard (with locking)** |
| 12 | + |
| 13 | +Stores the state in a [Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/) |
| 14 | +with locking done in the same secret. |
| 15 | + |
| 16 | +## Example Configuration |
| 17 | + |
| 18 | +```hcl |
| 19 | +terraform { |
| 20 | + backend "kubernetes" { |
| 21 | + key = "state" |
| 22 | + load_config_file = true |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +This assumes the user/service account running terraform has [permissions](https://kubernetes.io/docs/reference/access-authn-authz/authorization/) to read/write secrets |
| 28 | +in the [namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/) |
| 29 | +used to store the secret. |
| 30 | + |
| 31 | +If the `load_config_file` flag is set the backend will attempt to use a [kubeconfig file](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/) to |
| 32 | +gain access to the cluster. |
| 33 | +If the `service_account` flag is set the backend will attempt to use a [service account](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/) to |
| 34 | +access the cluster. This can be used if Terraform is being ran from within a pod |
| 35 | +running in the Kubernetes cluster. |
| 36 | + |
| 37 | +For most use cases either `service_account` or `load_config_file` will need to be set to `true`. |
| 38 | +If both flags are set the configuration from `load_config_file` will be used. |
| 39 | + |
| 40 | +Note that for the access credentials we recommend using a |
| 41 | +[partial configuration](/docs/backends/config.html#partial-configuration). |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +## Example Referencing |
| 46 | + |
| 47 | +```hcl |
| 48 | +data "terraform_remote_state" "foo" { |
| 49 | + backend = "kubernetes" |
| 50 | + config = { |
| 51 | + key = "state" |
| 52 | + load_config_file = true |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Configuration variables |
| 58 | + |
| 59 | +The following configuration options are supported: |
| 60 | + |
| 61 | +* `key` - (Required) Used to name the secret in Kubernetes and added as a label. |
| 62 | +* `namespace` - (Optional) Namespace to store the secret in. Can be sourced from `KUBE_NAMESPACE`. |
| 63 | +* `service_account` - (Optional) Use a service account assigned to a pod to access the cluster. Can be sourced from `KUBE_SERVICE_ACCOUNT`. |
| 64 | +* `load_config_file` - (Optional) Use a kubeconfig file to access the cluster. Can be sourced from `KUBE_LOAD_CONFIG_FILE`. |
| 65 | +* `host` - (Optional) The hostname (in form of URI) of Kubernetes master. Can be sourced from `KUBE_HOST`. Defaults to `https://localhost`. |
| 66 | +* `username` - (Optional) The username to use for HTTP basic authentication when accessing the Kubernetes master endpoint. Can be sourced from `KUBE_USER`. |
| 67 | +* `password` - (Optional) The password to use for HTTP basic authentication when accessing the Kubernetes master endpoint. Can be sourced from `KUBE_PASSWORD`. |
| 68 | +* `insecure` - (Optional) Whether server should be accessed without verifying the TLS certificate. Can be sourced from `KUBE_INSECURE`. Defaults to `false`. |
| 69 | +* `client_certificate` - (Optional) PEM-encoded client certificate for TLS authentication. Can be sourced from `KUBE_CLIENT_CERT_DATA`. |
| 70 | +* `client_key` - (Optional) PEM-encoded client certificate key for TLS authentication. Can be sourced from `KUBE_CLIENT_KEY_DATA`. |
| 71 | +* `cluster_ca_certificate` - (Optional) PEM-encoded root certificates bundle for TLS authentication. Can be sourced from `KUBE_CLUSTER_CA_CERT_DATA`. |
| 72 | +* `config_path` - (Optional) Path to the kube config file. Can be sourced from `KUBE_CONFIG` or `KUBECONFIG`. Defaults to `~/.kube/config`. |
| 73 | +* `config_context` - (Optional) Context to choose from the config file. Can be sourced from `KUBE_CTX`. |
| 74 | +* `config_context_auth_info` - (Optional) Authentication info context of the kube config (name of the kubeconfig user, `--user` flag in `kubectl`). Can be sourced from `KUBE_CTX_AUTH_INFO`. |
| 75 | +* `config_context_cluster` - (Optional) Cluster context of the kube config (name of the kubeconfig cluster, `--cluster` flag in `kubectl`). Can be sourced from `KUBE_CTX_CLUSTER`. |
| 76 | +* `token` - (Optional) Token of your service account. Can be sourced from `KUBE_TOKEN`. |
| 77 | + |
0 commit comments