Setup Grafana in Microsoft Azure (WebApp) using Azure CLI

suryabhansv

Suryabhan Singh Vaghela

Posted on July 14, 2022

Setup Grafana in Microsoft Azure (WebApp) using Azure CLI

Goal :

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

Pre-Requisite :

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"
Enter fullscreen mode Exit fullscreen mode
$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
Enter fullscreen mode Exit fullscreen mode
#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
Enter fullscreen mode Exit fullscreen mode
#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"
Enter fullscreen mode Exit fullscreen mode
#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
Enter fullscreen mode Exit fullscreen mode
#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
Enter fullscreen mode Exit fullscreen mode

Output :

Grafana1
Grafana2
Grafana3
Grafana4
Grafana5
Grafana6
Grafana7
_____________________________________X______________________________________
done

💖 💪 🙅 🚩
suryabhansv
Suryabhan Singh Vaghela

Posted on July 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related