linuxazureterraformazure-cliazure-pipeline-python-script-task

Scripting Azure authentication with Terraform on Linux using a token


az login
terraform init
terraform validate
terraform apply
az login -u <username> -p <password>

Solution

  • You provided the azure-pipelines tag so not sure if that means you're using Azure DevOps.

    If so, that will make it easier as you can just create a service connection within ADO (which will create a service principal in AAD in the background) that you can use to run your pipeline so you won't have to run az login within your scripts. You can check out Charles Zipp's ADO pipeline task for Terraform which I find is a bit more flexible than Microsoft's Terraform task. If you setup a pipeline you can then set access control on the pipeline (and even service connection) to allow only those you want to run the pipeline.

    If you don't use ADO and just want to run things locally then you'd need to create a service principal and provide the credentials in the provider block as @AnsumanBal-MT mentions in the comments on your question.