A machine-readable index of this documentation for AI agents is available at https://kubevela.io/llms.txt. Markdown versions of documentation pages are available by appending .md to their URLs.
Skip to main content
Version: Terraform Controller Next

State in a Namespace of Your Own

By default the Terraform state goes to a Secret in the namespace the chart's backend.namespace names. spec.backend puts one Configuration's state somewhere else: here, a tf-state namespace, under a Secret name you pick. That's useful when teams keep their own state apart, or when you want to back the state up with the rest of a namespace.

1. Apply it​

Save this as backend.yaml:

apiVersion: v1
kind: Namespace
metadata:
name: tf-state
---
apiVersion: terraform.core.oam.dev/v1beta2
kind: Configuration
metadata:
name: random-state
spec:
hcl: |
resource "random_pet" "name" {
length = 2
}

output "name" {
value = random_pet.name.id
}
backend:
backendType: kubernetes
kubernetes:
namespace: tf-state
secret_suffix: random-state
inlineCredentials: true
writeConnectionSecretToRef:
name: random-state-conn
namespace: default
kubectl apply -f backend.yaml
kubectl wait configuration/random-state --for=jsonpath='{.status.apply.state}'=Available --timeout=10m
kubectl get configuration random-state
NAME APPLY DESTROY AGE
random-state Available 21s

2. Look at the state​

Terraform's kubernetes backend names the Secret tfstate-<workspace>-<secret_suffix>, and the workspace is always default:

kubectl get secret -n tf-state
NAME TYPE DATA AGE
tfstate-default-random-state Opaque 1 17s

The state is gzipped JSON under the tfstate key:

kubectl get secret -n tf-state tfstate-default-random-state -o jsonpath='{.data.tfstate}' \
| base64 -d | gunzip | jq '{serial, outputs}'
{
"serial": 1,
"outputs": {
"name": {
"value": "polite-wahoo",
"type": "string"
}
}
}

3. Clean up​

kubectl delete configuration random-state
kubectl delete namespace tf-state

Delete the Configuration first: the destroy needs its state.

Notes​

  • backendType: s3 keeps the state in an S3 bucket instead (s3.bucket, s3.key, and s3.region, which defaults to the Provider's); the controller takes the AWS credentials from the Configuration's Provider, and the bucket must already exist.
  • Leave out kubernetes.namespace and the state goes to the chart's backend.namespace, under your secret_suffix.