How to update statefulset spec with terraform helm provider

Solution for How to update statefulset spec with terraform helm provider
is Given Below:

I have a statefulset created using the terraform helm provider. I need to update the value of an attribute (serviceName) in the statefulset but I keep getting the following error

Error: failed to replace object: StatefulSet.apps "value" is invalid: spec: Forbidden:
updates to statefulset spec for fields other than 'replicas', 'template', and 
'updateStrategy' are forbidden

Error is pretty descriptive and I understand that serviceName property can’t be changed but then how do I update this property? I am totally fine with having a downtime and also letting helm delete all the old pods and create new ones.

I have tried setting force_update and recreate_pods properties to true in my helm chart with no luck. Manually deleting the old helm chart is not an option for me.

I maintain the Kustomization provider and unlike the helm integration into Terraform it tracks each individual Kubernetes resource in the Terraform state. Therefor, it will show changes to the actual Kubernetes resources in the plan. And, most important for your issue here, it will also generate destroy-and-recreate plans for cases where you have to change immutable fields.

It’s a bit of a migration effort, but you can make it easier for you by using the helm template command to write YAML to disk and then point the Kustomize provider at the YAML.

As part of Kubestack, the Terraform framework for AKS, EKS and GKE I maintain, I also provide a convenience module.

You could use it like this, to have it apply the output of helm template:

module "example_stateful_set" {
  source  = "kbst.xyz/catalog/custom-manifests/kustomization"
  version = "0.1.0"

  configuration_base_key = "default"
  configuration = {
    default = {
      resources = [
        "${path.root}/path/to/helm/template/output.yaml"
      ]
    }
  }
}

Finally, you will have to import the existing Kubernetes resources into the Terraform state, so that the provider can start managing the existing resources.