Tofu-controller workshop
In this post I'm sharing a workshop I created for my colleagues at work recently.
Requirement
- Local Kubernetes cluster distribution like minikube, kind, k3s, etc.
- gh cli
- flux cli
- OpenTofu
Goals
- Install tofu-controller
- Create an AWS IAM role or anything you like using tofu-controller
- Play around
Useful links
- https://fluxcd.io/flux/installation/bootstrap/github/#bootstrap-without-a-github-pat
- https://flux-iac.github.io/tofu-controller/
- https://wcarlsen.github.io/#opentofu-enters-the-gitops-game
Create local cluster
Create a local Kubernetes cluster in any fashion you like. I use minikube.
Create a public repository in your own GitHub account
Create a public repository and clone it.
Bootstrap fluxcd v2
Bootstrap fluxcd using your private key.
Install tofu-controller
Add these manifests to your folder cluster/minikube/tofu-controller.yaml. Remember to commit and push it.
Verify that it works with flux get all.
Add OpenTofu code and a Terraform resource
Create a folder in the repository root called infra/ and add a file called infra/main.tf. This is where our OpenTofu code will go eventually. Remember to commit and push.
Then deploy an AWS secret using your local credentials (required to already be logged in with go-aws-sso or which ever tool you use).
Lastly add this Terraform resource to the folder clusters/minikube/terraform.yaml. Again we need to commit and push.
Bonus goals
Below is in no particular order.
- Locate plans and states as Kubernetes Secrets
- Try backup and restore of state https://flux-iac.github.io/tofu-controller/use-tf-controller/backup-and-restore-a-Terraform-state/
- Try branch-planner https://flux-iac.github.io/tofu-controller/branch-planner/branch-planner-getting-started/
- Write output to secrets https://flux-iac.github.io/tofu-controller/use-tf-controller/provision-resources-obtain-outputs/
- Create multiple Terraform resources and use dependsOn
- Perform healthChecks using outputs https://flux-iac.github.io/tofu-controller/use-tf-controller/provision-Terraform-resources-that-are-required-health-checks/
- Is a custom backend better than Kubernetes Secrets https://flux-iac.github.io/tofu-controller/use-tf-controller/with-a-custom-backend/
- Investigate plan-only mode
- Create a local Kubernetes Secret remote backend and do local plans
- Play with tfclt cli tool https://flux-iac.github.io/tofu-controller/tfctl/