Skip to main content

Command Palette

Search for a command to run...

🌟 Simplify Kubernetes to Terraform Conversion with tfk8s

Published
3 min read
🌟 Simplify Kubernetes to Terraform Conversion with tfk8s
R

An experienced DevOps Engineer understands the integration of operations and development in order to deliver code to customers quickly. Has Cloud and monitoring process experience, as well as DevOps development in Windows, Mac, and Linux systems.

Managing Kubernetes manifests with Terraform can be challenging when you have a huge collection of YAML files or when you want to migrate existing resources without rewriting everything into HCL by hand.

That’s where tfk8s comes to the rescue! 🚀

This tool makes it super easy to convert Kubernetes YAML manifests into Terraform configuration so you can manage your infrastructure in a consistent, declarative, and version-controlled way.


🔑 Why tfk8s?

If you’ve ever tried to:

  • Copy Kubernetes YAML from documentation into Terraform

  • Migrate existing YAML manifests into Terraform resources

  • Convert Helm charts into Terraform-managed resources

…then you know how time-consuming and error-prone it can be.

tfk8s solves this by automating YAML → HCL conversion for you.


⚡ Key Features

✅ Convert single or multi-resource YAML files into Terraform
✅ Strip out server-side fields (great when piping from kubectl)
✅ Generate Terraform HCL maps for direct provider use
✅ Convert Helm charts to Terraform
✅ Bulk-convert directories of YAML files into .tf files


🛠️ Installation

You can install tfk8s in several ways depending on your setup:

Using Go:

go install github.com/jrhouston/tfk8s@latest

Using Homebrew (macOS/Linux):

brew install tfk8s

Using MacPorts (macOS):

sudo port install tfk8s

Make sure $(go env GOPATH)/bin is in your $PATH if you install via Go.


🚀 Usage

Run tfk8s with either a file or stdin:

tfk8s -f input.yaml -o output.tf

Or using pipes:

cat input.yaml | tfk8s > output.tf

Example: Convert a ConfigMap

input.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data:
  TEST: test

✨✨ magically becomes ✨✨

output.tf:

resource "kubernetes_manifest" "configmap_test" {
  manifest = {
    "apiVersion" = "v1"
    "data" = {
      "TEST" = "test"
    }
    "kind" = "ConfigMap"
    "metadata" = {
      "name" = "test"
    }
  }
}

Example: Convert with kubectl

kubectl get ns default -o yaml | tfk8s -M

This outputs an HCL map instead of YAML — perfect for Terraform provider integration.


Example: Convert Helm Charts

helm template ./chart-path -f values.yaml | tfk8s > helm_output.tf

This lets you manage Helm-generated manifests via Terraform.


Example: Convert a Directory of Manifests

find manifests/ -name '*.yaml' -type f -exec sh -c 'tfk8s -f {} -o $(echo {} | sed "s/\.yaml$/.tf/")' \;

This bulk-converts all your YAML files into Terraform configurations.


🔗 My GitHub Repo

I’ve created a GitHub repository where I experiment with tfk8s conversions, Helm → Terraform pipelines, and CI/CD integrations. You can explore it here:

👉 My GitHub Repository

Feel free to fork, contribute, or use it as a reference for your own Kubernetes-to-Terraform journey.


🎯 Final Thoughts

Terraform is the gold standard for Infrastructure as Code (IaC), and with tfk8s, you can easily bring your Kubernetes YAML manifests under Terraform’s management without painful manual conversion.

This improves:

  • Consistency across your deployments

  • Version control of your manifests

  • Automation in CI/CD pipelines

So next time you’re working with Kubernetes YAML and want it in Terraform, just let tfk8s do the heavy lifting. 💪

More from this blog

DevOps Simplified

39 posts

An experienced DevOps Engineer understands the integration of operations and development in order to deliver code to customers quickly. Has Cloud and monitoring process experience, as well as DevOps