Azure Authetication
To use Terraform commands against your Azure subscription, you must first authenticate Terraform to that subscription. This article covers common DevOps scenarios for authenticating to Azure securely
Last updated
To use Terraform commands against your Azure subscription, you must first authenticate Terraform to that subscription. This article covers common DevOps scenarios for authenticating to Azure securely
Last updated
To use Terraform with Azure, you must authenticate Terraform to your Azure subscription. The recommended approach for automation and CI/CD is to use a Service Principal with RBAC. Below are step-by-step instructions for both Bash (Azure CLI) and PowerShell workflows, with real-life DevOps tips.
Sign in to Azure:
(If using Git Bash on Windows) set the environment variable to avoid path conversion issues:
Tip: Add this to your
~/.bashrc
for persistent use.
Create a Service Principal with Contributor role:
Replace <service_principal_name>
and <subscription_id>
as needed.
The output will include appId
, password
, and tenant
—store these securely (e.g., Azure Key Vault, GitHub Actions secrets).
Best Practice: Never commit credentials to source control. Use environment variables or secret managers in CI/CD.
Configure Terraform to use the Service Principal: Add these variables to your environment or your CI/CD pipeline:
Or use a :
Open a PowerShell prompt and sign in:
Check your current subscription:
List all available subscriptions:
Set the active subscription (if needed):
Create a Service Principal with Contributor role:
Store $appId
, $password
, and $tenantId
securely for use in Terraform.
Store your Service Principal credentials as GitHub Actions secrets, then use them in your workflow:
Use a dedicated Service Principal per environment (dev, staging, prod)
Grant only the minimum RBAC permissions needed
Store credentials in a secure secret manager (Azure Key Vault, GitHub/Azure DevOps secrets)
Rotate Service Principal credentials regularly
Never commit credentials to source control
Tip: For fully automated pipelines, use Terraform Cloud or GitHub Actions with OIDC for passwordless authentication to Azure.