This repository contains a basic example of how to use Terraform with the hashicorp/kubernetes provider. The purpose of this project is to serve as an educational exercise, introducing the basic concepts of Terraform modules, providers, and configuration. This example is not intended for production use.
The project structure is as follows:
root
├── main.tf
├── modules
│ └── namespace
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
├── outputs.tf
├── .terraform.lock.hcl
├── terraform.tfstate
├── terraform.tfvars
└── variables.tf
main.tf: The root configuration file where the main setup of the project is defined.modules/namespace/: Contains the reusable module namednamespacewhich is used to create a Kubernetes namespace.main.tf: Defines the resources within thenamespacemodule.outputs.tf: Outputs variables for thenamespacemodule.variables.tf: Specifies the input variables required by thenamespacemodule.
outputs.tf: Defines output values for the root module, such asns_id..terraform.lock.hcl: Lock file generated by Terraform to ensure consistent provider versions.terraform.tfstate: The state file that tracks the current status of the infrastructure managed by Terraform.terraform.tfvars: Contains the variable values that are passed to the root module configuration.variables.tf: Defines the input variables for the root module.
This example project demonstrates how to:
- Create a reusable Terraform module.
- Use the
hashicorp/kubernetesprovider to create a namespace in a Kubernetes cluster. - Pass input variables to the module from the root configuration.
- Output values after applying the configuration.
- Terraform installed on your local machine.
- Access to a Kubernetes cluster.
- Properly configured Kubernetes provider credentials.
To have a local Kubernetes environment to test out this project with, go to the Rancher Installation page.
Make sure to follow the instructions
To install GPG and Pass you can issue the following command:
sudo apt install gpg passTo make sure Traefik can handle the ports for this worksop example, it is important you issue the command
sudo sysctl -w net.ipv4.ip_unprivileged_port_start=80Once Rancher Desktop is installed, start it and wait for it to warm up. Rancher will automatically create a kubeconfig for you in ~/.kube/config, and no authorization is necessary.
-
Clone this repository:
git clone https://github.com/aiqueneldar/terraform-example.git cd terraform-exampleTake note that there is a terraform.tfstate included in the projekt. It is safe to delete this file before starting experimentation, and it is incurraged to do so.
-
Initialize the Terraform configuration:
terraform init
-
Check the plan to see the changes that will be applied:
terraform plan
-
Apply the configuration to create the namespace:
terraform apply
Once the process completes, the namespace ID (ns_id) will be outputted as a result.
-
To destroy the resources:
terraform destroy
This project is for educational purposes only. It is intended to demonstrate basic Terraform functionality and should not be used in a production environment. Always review and customize any Terraform code before deploying to your infrastructure.