In this article, we'll walk through deploying a simple Azure infrastructure using Terraform, featuring a resource group, an app service plan, two Linux-based web apps (for frontend and backend), and a MySQL flexible server. We'll use the provided main.tf and variables.tf files to achieve this, detailing each step and including essential Terraform commands.
Overview of the Configuration Files
- main.tf File: This file defines the Azure provider and resources to be deployed, including.
- An Azure resource group
- An app service plan
- Two Linux web apps (frontend and backend)
- A MySQL flexible server
- variables.tf File: This file defines various configurable parameters, such as the resource location, resource group name, service plan name, web app names, and MySQL server credentials.
For more detailed Terraform tutorials and guides on getting started with Azure deployments, refer to the officialTerraform documentation.
Setting Up Authentication with a Service Principal
Before using Terraform to deploy resources to Azure, it is recommened to create a service principal to provide Terraform with access credentials.
Step 1. Create a Service Principal.
To create a service principal with az ad sp create-for-RBAC, execute the following command in the Azure CLI. This command creates a new service principal with a Contributor role on the subscription.
az ad sp create-for-rbac --name "<NAME>" \
--role="Contributor" \
--scopes="/subscriptions/<SUBSCRIPTION_ID>"
- Replace <NAME> with a name for your service principal.
- Replace <SUBSCRIPTION_ID> with your Azure subscription ID.
az ad sp create-for-rbac --name "terraform-sp" \
--role="Contributor" \
--scopes="/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
This command will output the following details (make note of these).
{
"appId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"displayName": "terraform-sp",
"password": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"tenant": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
- appId is the client ID.
- password is the client's secret.
- tenant is your Azure AD tenant ID.
Step 2. Configure the Azure Provider with the Service Principal.
To authenticate Terraform with Azure using the service principal, modify your provider block in the main.tf (or set environment variables).

Join the conversation! Your thoughts help the community grow.