DEV Community

Kene Ojiteli
Kene Ojiteli

Posted on

Connecting Azure App Service to Azure SQL Database and Storage Account using Azure CLI part 1.

This article is a walkthrough on how to connect an azure app service to an azure SQL database and also an azure storage account using the command line interface.

Recall the following terms:

  • Azure app service is a fully managed service which enables you to build and host web apps, mobile back ends, and RESTful APIs in the programming language of your choice without managing infrastructure.
  • Azure SQL database is a fully-managed platform as a service (PAAS), that handles management functions such as patching, upgrading, backups, etc and gives an SLA of 99.99% availability.
  • Azure storage account contains all of your Azure Storage data objects, including blobs, file shares, queues, tables, and disks.

Prerequisites for this project include:

  • An Azure account with an active subscription.
  • Azure Cloud shell or a Azure CLI installed on local machine (I will be using powershell with azure CLI installed).

Things to Note

  • I will be using powershell (an automated engine with an interactive command-line shell), I will run a powershell script (this file will contain all the configuration needed for connecting app service to azure sql database, and will be run at once).
  • I will breakdown the commands in the script via screenshot.

Steps

  • Login into your azure account using az login command.

Azure login

  • Create your script and save on an editor using .ps1 extension (showing it is a powershell script); then navigate to the directory where script is saved and run the script as shown below

Run script

  • This is a breakdown of my script with variables declared first.

Variable declaration

  • Creating a resource group which will house all resources used in this demo with the command below:

Resource group command
Creating a resource group needs a resource group name, a location and a tag which will be referenced by calling my variables. Upon successful creation, the output will be:

RG-output

  • Creating an app service plan that defines a set of compute resources for a web app to run, this requires a name, resource group and a location.

App service plan
If all goes well, the output will look like this:

App service plan output

  • Creating a web app which requires an app service plan and a resource group.

Web app
With output as:

Web app output

  • Since we need a database, it is essential to create a server for the database with parameters like server name, resource group, location, and username and password, this is done below with the output as:

DB server

DB server output

  • To enable access to the server, a firewall rule is needed, this rule determines what traffic is allowed or blocked by the firewall.

Firewall rule

Firewall output

  • Creating a database and attach it to the server using the database name, server, resource group variables and service objective.

Create DB

Create Db output

  • Getting a connection string which is required by say the application hosted on the app service to form a connection with the database server, which requires the database name, server, client driver (ado.net in this demo) and output format stored in a variable using the command below:

Get connection string

  • Add login credentials to the connection string variable using the command below.

Add credentials

  • Assign the connection string to app setting in the azure app service, this would help to prevent exposing credentials.

Assign connection string

  • After the script is run and successful, head over to azure portal to view the created resources.

Resources created

From my resource group, the resources created are visible.

  • And my web app is running but without a content.

Web app output

  • To avoid incurring excess charges on your azure account, clean up your resources using the command below:

az group delete --name $resourceGroup

which would prompt you to choose a yes or no.

Thank you for reading and I hope you learned something new, the powershell script is available on my repo

Kindly read part 2 of this article.

Top comments (0)