Always switching EKS/Kubernetes context first before applying other changes


I have a very specific scenario here which is causing us issues.

We have multiple EKS/Kubernetes clusters, which are provisioned and managed via Terraform. We need the appropriate Kubernetes context to be used before any other changes are implemented in each terraform apply.

One possible way which we have found to run something every time is to use something like this:

resource "null_resource" "eks-context-switcher" {

  triggers = {
    always_run = timestamp()

  provisioner "local-exec" {
    command = "aws eks --region ${var.region} update-kubeconfig --name ${var.eks_name}"

But this above solution has the problem that we as Terraform is declarative (, the execution order cannot be guaranteed, and some changes might end up being made in the wrong Kubernetes cluster/context.

To add to the above solution, we could add a depends_on meta-argument, which depends on the null_resource, within each EKS/Kubernetes resource. But this seems very error-prone, and adds unnecessary toil, and we can already see someone forgetting to add this.

Is there a built-in mechanism which is available in Terraform which would allow us to always run a certain command/recreate a resource first, before anything else gets applied.