Member-only story

Using Templates in Terraform to generate Kubernetes YAML

CJ Hewett
4 min readDec 9, 2022

--

The tricky thing with YAML files in Kubernetes, is that it’s not so easy to use variables within the files to inject values into them. Kustomize also doesn’t solve thing problem very well, so I will show a way to use Terraform to take a bunch of YAML files with variables, inject values into those variables to generate “rendered” YAML files, that can then be applied to a Kubernetes cluster.

Note: I would recommend using something like Helm or FluxCD over this, but sometimes needs must. ¯\_(ツ)_/¯

(This doesn’t have to be for Kubernetes YAML files, you could use this for a range of use cases where you need to inject some values into template files.)

Code for this example can be found here:

This is what the templates folder structure looks like:

--

--

CJ Hewett
CJ Hewett

Written by CJ Hewett

🛹 Skateboarder. 🏂 Snowboarder. 🏄 Websurfer. I write monthly* about Cloud/DevOps/IoT. AWS Certified DevOps Engineer and Terraform Associate

Responses (1)