Service Principal
To securely automate Terraform deployments in Azure, use a Service Principal with the minimum required permissions. This script creates a Service Principal with Contributor and User Access Administrator roles at the subscription scope—suitable for most DevOps CI/CD scenarios (e.g., GitHub Actions, Azure Pipelines).
Bash Script: Create Service Principal for Terraform
Output:
Usage in Terraform and CI/CD
Store the output values (
clientId
,clientSecret
,subscriptionId
,tenantId
) as environment variables or in your CI/CD secret manager (e.g., GitHub Actions, Azure DevOps).Reference these in your Terraform provider block:
Best Practices
Use a unique Service Principal per environment (dev, staging, prod)
Grant only the minimum permissions needed
Rotate credentials regularly and never commit them to source control
Store secrets in a secure vault (Azure Key Vault, GitHub/Azure DevOps secrets)
References
Tip: For passwordless authentication in CI/CD, consider using OIDC with GitHub Actions or Azure Pipelines.
Last updated