Skip to main content
Version: 0.21.2

Use TF-controller to set variables for Terraform resources

Breaking Change

This is a breaking change of the v1alpha1 API.

Users who are upgrading from TF-controller <= 0.7.0 require updating varsFrom, from a single object:

  varsFrom:
kind: ConfigMap
name: cluster-config

to be an array of object, like this:

  varsFrom:
- kind: ConfigMap
name: cluster-config

Set variables

TF-controller allows you to pass variables to Terraform using the vars and varsFrom fields in a Terraform object.

Inline variables can be set using the vars field, which supports HCL string, number, bool, object, and list types. The varsFrom field accepts a list of ConfigMaps or Secrets and allows you to select specific keys using the varsKeys property, or you can omit this field to select all keys from the input source.

If the same variable key is passed multiple times, the controller will use the latter most instance of the key passed to varsFrom.

Here is an example of a Terraform object that sets inline variables using the vars field and retrieves variables from a ConfigMap and Secret using the varsFrom field:

Expand to view
---
apiVersion: infra.contrib.fluxcd.io/v1alpha1
kind: Terraform
metadata:
name: helloworld
namespace: flux-system
spec:
approvePlan: auto
interval: 1m
path: ./
sourceRef:
kind: GitRepository
name: helloworld
namespace: flux-system
vars:
- name: region
value: us-east-1
- name: env
value: dev
- name: instanceType
value: t3-small
varsFrom:
- kind: ConfigMap
name: cluster-config
varsKeys:
- nodeCount
- instanceType
- kind: Secret
name: cluster-creds

Variables as HCL

The vars field in a Terraform object allows you to set variables for your Terraform configuration. This field supports HCL string, number, bool, object, and list types.

In the example provided, the vars field sets the value of the cluster_spec variable to an object with four fields: "region", "env", "node_count", and "public". The "region" and "env" fields are strings, the "node_count" field is a number, and the "public" field is a boolean.

This allows you to set variables in your Terraform configuration in a flexible and dynamic way, using data that can be passed in through the Terraform object.

Expand to view
variable "cluster_spec" {
type = object({
region = string
env = string
node_count = number
public = bool
})
}
---
apiVersion: infra.contrib.fluxcd.io/v1alpha1
kind: Terraform
metadata:
name: helloworld
namespace: flux-system
spec:
approvePlan: auto
interval: 1m
path: ./
sourceRef:
kind: GitRepository
name: helloworld
namespace: flux-system
vars:
- name: cluster_spec
value:
region: us-east-1
env: dev
node_count: 10
public: false