When using the Terraform CLI, make sure you have all the necessary tools installed on your workstation.
Prepare a file with authentication information, as described in Authenticating Terraform to access Google Cloud.
Prepare your terraform.tfvars file, as described in Customize Input Values.
Initialize the Terraform environment for this project by running
terraform initThis creates a .terraform directory locally and initializes Terraform plugins and modules used in this project.
Note: terraform init only needs to be run once unless new Terraform plugins or modules were added.
To preview the resources that the Terraform script will create, run
terraform planWhen satisfied with the plan and ready to create cloud resources, run
terraform apply -auto-approveThis command can take a few minutes to complete. Once complete, Terraform output values are written to the console.
The kubeconfig file for the cluster is being written to [prefix]-gke-kubeconfig.conf in the current directory $(pwd).
Once the cloud resources have been created with apply command, to display Terraform output values, run
terraform outputAfter provisioning the infrastructure, if further changes were to be made then add the variable and desired value to terraform.tfvars and run terrafom apply again.
To destroy the kubernetes cluster and all related resources, run
terraform destroyNOTE: The "destroy" action is irreversible.
Creating the cloud resources writes the kube_config output value to a file ./[prefix]-gke-kubeconfig.conf. When the Kubernetes cluster is ready, use kubectl to interact with the cluster.
Note this requires cluster_endpoint_public_access_cidrs value to be set to your local ip or CIDR range.
export KUBECONFIG=$(pwd)/<your prefix>-gke-kubeconfig.conf
kubectl get nodes