Connecting Azure App Service to Azure SQL Database and Storage Account using Azure CLI part 1.
Tags: azure, cli, cloud, beginners
Published: 2023-05-08 11:56:27

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx1zzltxj6uhl0o9yapv.png)
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/un0cfgrse8t3yvib7k0u.png)
- This is a breakdown of my script with variables declared first.

![Variable declaration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwhj230o1w6sz6yucq51.png)
- Creating a resource group which will house all resources used in this demo with the command below:

![Resource group command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rsh8iitvz04aq0fxic7.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ulyx1uk1nu548n1nblr.png)
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1014vfwhzlne7c13pr7.png)
If all goes well, the output will look like this:

![App service plan output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekp13a9tbg165cdg62l0.png)

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

![Web app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0wjsjb436a14oc651jc.png)
With output as:

![Web app output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2e8e86r7hxpu5nprjro.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcjmnj9an6kyhq7db1af.png)

![DB server output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05rzsj0qez85kv6h4zvx.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqhhe7fthtr0k3opbaw6.png)

![Firewall output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1okeevulk0qt2hlxv5ay.png)

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

![Create DB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4tc9j69v5y47bzd22fn.png)

![Create Db output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lsd9ioenkg3pmplqt1s.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hg4eee0axk9hw0vvs0wk.png)

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

![Add credentials](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6hiiudyxx2uotm5kowh.png)  

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

![Assign connection string](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0w78q5qa4k48u72yw2y.png)

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

![Resources created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0n01ehgkgu86a18cffo9.png)

From my resource group, the resources created are visible.

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

![Web app output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35otddeppwvd077t11n6.png)

- 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](https://github.com/KeneOjiteli/connect-app-service-to-azure-db)

Kindly read [part 2](https://dev.to/keneojiteli/connecting-azure-app-service-to-azure-sql-database-and-storage-account-using-azure-cli-part-2-4a1m) of this article.