Here, I'm trying to create ACR and AKS (in azure portal) with terraform tasks in azure devops pipeline and right after creating these resources I'm trying to add "deploy into AKS" task in the same pipeline (or a different pipeline) and then after the build completes, all the resources should get destroyed in azure portal. The main point is that this pipeline should work without any manual intervention(creating service connection everytime for ACR and AKS).
Yes, there is a way to automate service principal connection. To automate service principal connections for ACR and AKS from Azure DevOps without manual intervention, you can use the Azure CLI task to create and manage service principals. First, create a Terraform project to create ACR and AKS resources. Then, in the Azure DevOps pipeline, add an Azure CLI task to create a service principal with the necessary permissions to access ACR and AKS. Finally, add a Terraform task to deploy the resources, which will use the service principal created in the previous step. This approach eliminates the need to manually create service connections for ACR and AKS in Azure DevOps. I once had this issue and searched for help. You can explore more at: https://triotechsystems.com/azure-devops-service-principal-for-acr-and-aks/