Can you define Kubernetes Services / Pods using YAML in Terraform?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes, you can manage Kubernetes objects from Terraform using YAML, but the answer needs a caveat. It works best when you understand the tradeoff between reusing existing manifests and letting Terraform manage those objects as first-class infrastructure resources.
Two Main Approaches
There are two common ways to do this:
- write native Terraform Kubernetes resources such as
kubernetes_serviceorkubernetes_deployment - load a YAML manifest and apply it through a manifest-capable resource
The first approach is more Terraform-native. The second is useful when you already have Kubernetes YAML you want to keep.
Applying YAML Through Terraform
A common pattern is to read YAML from disk, decode it, and pass it to a Kubernetes manifest resource.
Example pod.yaml:
You can do the same for a service:
That means the short answer to the question is yes: Terraform can consume Kubernetes YAML and apply it.
When Native Terraform Resources Are Better
If you are building everything from scratch in Terraform, native resources are often easier to review and refactor:
This gives you stronger schema validation and clearer plans. Terraform can show field-by-field changes more naturally than when it manages a large decoded YAML object.
Tradeoffs You Should Consider
Using YAML inside Terraform is attractive because it avoids rewriting existing manifests. Still, it adds some friction:
- diffs may be noisier
- provider support varies by resource type and API version
- server-populated fields can create drift
- complex YAML templating can become harder to maintain than either pure YAML or pure Terraform
For teams already invested in Kubernetes manifests, Terraform can be a coordination layer. For teams managing cloud infrastructure and cluster resources together, native Terraform resources may produce cleaner plans and state.
A Good Hybrid Pattern
A practical compromise is to let Terraform manage cluster infrastructure, namespaces, and foundational services, while an in-cluster tool such as Helm, Argo CD, or plain kubectl handles application manifests.
That split keeps Terraform focused on durable infrastructure and avoids turning every application rollout into a Terraform state change.
Still, if you have a small platform or only a few manifests, using YAML through kubernetes_manifest is completely reasonable.
Common Pitfalls
- Assuming Terraform will behave exactly like
kubectl apply. The lifecycle model is different. - Storing raw YAML in Terraform without considering drift from fields the API server adds automatically.
- Choosing Pods for long-lived workloads when a
Deploymentwould be more resilient. - Mixing too much string templating into YAML files, which makes plans hard to read and debug.
- Using YAML in Terraform for everything, even when native resources or Helm would be simpler.
Summary
- Yes, Terraform can define Kubernetes Services and Pods from YAML.
- '
kubernetes_manifestwithyamldecode(file(...))is a common pattern.' - Native Terraform Kubernetes resources provide better schema-aware plans in many cases.
- YAML reuse is useful when manifests already exist and you want Terraform to orchestrate them.
- Pick the approach based on drift, maintainability, and how much of Kubernetes you want Terraform to own.

