Setup Grafana in Microsoft Azure (WebApp) using Azure CLI
Tags: azure, grafana, monitoring, tutorial
Published: 2022-07-14 11:43:29

**Goal :**

- Create Grafana WebApp in Microsoft Azure using Azure CLI
- Database is `mysql` `Azure Database for MySQL single server`

**Pre-Requisite :** 

- [Deploying Grafana Image to Azure Container Registry Using Azure CLI](https://dev.to/suryabhansv/deploying-grafana-image-to-azure-container-registry-using-azure-cli-mc4)

**Resource will Create :**

- App Service plan
- Storage account
- App Service
- Azure Database for MySQL single server

**Script**


```
$ResourceGroupName		= "suryarg"
$LocationName			= "eastus2"
$ACRName				= "suryacr"
$ACRUrl 				= "$ACRName.azurecr.io"

$AppServicePlan			= "suryaasp"
$AppServicePlanSku		= "S1"
$StorageAccountName		= "suryastorageaccountsa"
$StorageAccountSku		= "Standard_ZRS"
$StorageAccountShare	= "suryasgrafana"
$WebAppName				= "suryawagrafana"
```
```
$MYSQLServer			= "suryamysql"
$MYSQLDB				= "grafana"

#(Basic, Gen 5 hardware, 1 vCores).
$MYSQLServerSku			= "B_Gen5_1"
$MYSQLUsername			= "suryauser"
$MYSQLPassword			= "surya@pass@2022"
$IP 					= (Invoke-WebRequest -uri "https://api.ipify.org/").Content
```
```
#Create App Service Plan
az appservice plan create --resource-group $ResourceGroupName --name $AppServicePlan --is-linux --sku $AppServicePlanSku	

#Create a Storage Account
az storage account create --resource-group $ResourceGroupName --name $StorageAccountName --kind StorageV2 --sku $StorageAccountSku

#Create an Storage Account File Share
az storage share-rm create --resource-group $ResourceGroupName --storage-account $StorageAccountName --name $StorageAccountShare --access-tier "TransactionOptimized" --quota 64

#Create an Webapp for Grafana
az webapp create --resource-group $ResourceGroupName --name $WebAppName --plan $AppServicePlan -i "$ACRUrl/grafana:SuryaLatest"

#Create MYSQL Server for Grafana Database
az mysql server create --resource-group $ResourceGroupName --name $MYSQLServer --location $LocationName --admin-user $MYSQLUsername --admin-password $MYSQLPassword --sku-name $MYSQLServerSku --storage-size 5120

#Create MYSQL Database
az mysql db create --resource-group $ResourceGroupName --server-name $MYSQLServer --name $MYSQLDB
```

```
#Create Storage Mount for Grafana WebApp
$storageaccountkey	=	$(az storage account keys list --resource-group $ResourceGroupName --account-name $StorageAccountName --query [0].value -o tsv)

az webapp config storage-account add --resource-group $ResourceGroupName --name $WebAppName --custom-id "grafana" --storage-type "AzureFiles" --share-name $StorageAccountShare --account-name $StorageAccountName --access-key $storageaccountkey  --mount-path "/var/lib/grafana"
az webapp config storage-account add --resource-group $ResourceGroupName --name $WebAppName --custom-id "config" --storage-type "AzureFiles" --share-name $StorageAccountShare --account-name $StorageAccountName --access-key $storageaccountkey  --mount-path "/etc/grafana"
az webapp config storage-account add --resource-group $ResourceGroupName --name $WebAppName --custom-id "log" --storage-type "AzureFiles" --share-name $StorageAccountShare --account-name $StorageAccountName --access-key $storageaccountkey  --mount-path "/var/log/grafana"
```
```
#Set an Environment Variable for Grafana WebApp
$envg0 = "mysql"
az webapp config appsettings set --resource-group $ResourceGroupName --name $WebAppName --settings GF_DATABASE_TYPE=$envg0

$envg1 = "mysql://$($MYSQLUsername)@$($MYSQLServer):$($MYSQLPassword)@$($MYSQLServer).mysql.database.azure.com:3306/$($MYSQLDB)"
az webapp config appsettings set --resource-group $ResourceGroupName --name $WebAppName --settings GF_DATABASE_URL=$envg1

$envg2="/etc/grafana/custom.ini"
az webapp config appsettings set --resource-group $ResourceGroupName --name $WebAppName --settings GF_PATHS_CONFIG=$envg2

$envg3="console file"
az webapp config appsettings set --resource-group $ResourceGroupName --name $WebAppName --settings GF_LOG_MODE=$envg3

$envg4="/var/log/grafana"
az webapp config appsettings set --resource-group $ResourceGroupName --name $WebAppName --settings GF_PATHS_LOGS=$envg4


az webapp config appsettings set --resource-group $ResourceGroupName --name $WebAppName --settings WEBSITES_PORT=3000
```
```
#Allow WebApp IP Address in MySQL Server
$WebAppAllIP = (az webapp show --resource-group $ResourceGroupName --name $WebAppName --query possibleOutboundIpAddresses --output tsv).split(",")
foreach($singleIP in $WebAppAllIP)
{
	az mysql server firewall-rule create --resource-group $ResourceGroupName --server $MYSQLServer --name $WebAppAllIP.IndexOf($singleIP) --start-ip-address $singleIP --end-ip-address $singleIP
}

#Allow Current IP Address in MySQL Server
az mysql server firewall-rule create --resource-group $ResourceGroupName --server $MYSQLServer --name AllowMyIP --start-ip-address $IP --end-ip-address $IP
```

**Output :**


![Grafana1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqh2lujcw0uwjghkj88u.png)
![Grafana2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwejk9suwi2c1wynvy0w.png)
![Grafana3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4s9vohixyex2dzvgdez.png)
![Grafana4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1fs2ufzkmvkl2r7a9u8.png)
![Grafana5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ib9f1716iz6ll9ka0oig.png)
![Grafana6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru6xxhtmu42aeqjizuk3.png)
![Grafana7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxj5v77265akvnvuwmfg.png)
**________________________________________X_________________________________________**
![done](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28m4w6s1ks7qnjk5twty.gif)