I want to deploy resources in Azure through a workspace in Terraform Cloud.
I am using Terraform Cloud's dynamic provider credentials feature for authenticating to Azure, as described in this article. It involves creating a service principal with federated credentials and hence no secrets.
Its working fine and I am able to deploy resources in Azure.
However, I have a local-exec resource that uses az login
az login --service-principal -u <client-id> -p <client-secret> --tenant <tenant>
Since I don't have a client-secret, how do I use az login ?
I came across this article, that shows to do this via GitHub Action, but I dont want to go the Actions route.
Thank you in advance for your help!
Need to check below: -
Approach-1:
Firstly, to work with
az loginwithout usingCLIENT_SECRETyou can try usingCLIENT_IDand AzureTENANT_IDfor logging purpose. To use these values set them as environment variables with the help ofexportcommand inAzCLI.Or
Go to
Terraform Cloudand you needed to set the environment variablesCLIENT_ID,TENANT_IDby choosing the specificworkspace >> variablesas detailed in SO by @Monkey Supersonic.export TENANT_ID = "xxx"Now use the local-exec resource to log into Azure and it works as expected.
Approach-2:
Using
az login --use-device-codecommand is another way to use thelocal-execresource in Terraform Cloud without aclientsecret.Once you execute the above command, it prompts you to open the web URL and enter the device code as shown. Then you will be able to log into the Azure successfully.
Approach-3:
Other approach is using managed identity login. You can use the managed identity to sign in. The
--identityflag is used to sign in with the resource's identity.az login --identity --username CLIENT_IDComing to the above error, it comes when the
Azure CLIis not properly installed or not identified in the system path. Sometimes upgrading theAzure CLIusingaz upgraderesolves this error.After upgrading it, restart the
Azure CLI. If upgrading doesn't work, then remove the existing CLI configuration and reinstall it again.Reference MS Doc CLI installation.
Note: If still the issue persists, check to see if the location where
Azure CLIis installed is in your system'sPATHenvironment variable. Add it if not present.Refer the github doc for the relevant issue.