Deploy Java (Spring boot) application with MS-SQL database on Azure (Part 3): Using Azure CLI

Hello everyone, in this part of tutorial we will deploy our Java based on spring boot application to Azure using the Azure CLI.

I — Install Azure CLI

Go to Install the Azure CLI and install the CLI based on your operating system.

After the installation, open a command line and run the following command to connect to Azure:

$> az login

A pop-up will appear, then you can enter your email and password to connect to your microsoft account.

If the connection is successful, you should see all the subscriptions that you have access to.

If you already have Azure CLI installed, you can update it by running the command:

$> az upgrade

II — Create Azure Resource Group

First, we need to create a resource group where we will add other resources using the following command, with the params:

  • name: the name of the resource group
  • location: the region of the resource group to create
$> az group create --name "azure-spring-mssql-rg" --location "West Europe"

III — Create the database

1 - Server

To create a SQL Database on Azure we need first to create a server, to do this we use the command:

$> az sql server create --name "azure-spring-mssql-db-server" --resource-group "azure-spring-mssql-rg" --location "West Europe" --admin-user "YOUR_ADMIN_USERNAME" --admin-password "YOUR_ADMIN_PASSWORD"

With params:

  • name: the name of the server
  • resource-group: the resource group where the server will be created
  • location: the region of the server
  • admin-user: the server admin username
  • admin-password: the server admin password

2 - Firewall Rule

After server creation, we need to create a firewall rule that allow us to access the server:

Note: By using ‘0.0.0.0’ you’re allowing all Azure resources to access your server. Don’t do this in production mode.

$> az sql server firewall-rule create --resource-group "azure-spring-mssql-rg" --server "azure-spring-mssql-db-server" --name AllowYourIp --start-ip-address "0.0.0.0" --end-ip-address "0.0.0.0"

With params:

  • resource-group: the resource group where to apply the firewall rule
  • server: name of the server where to aply the firewall rule
  • name: the name of the firewall rule
  • start-ip-address: the start IP address of the firewall rule (in IPv4 format) Here we use ‘0.0.0.0’ to represent all Azure-internal IP addresses.
  • end-ip-address: the end IP address of the firewall rule (in IPv4 format) We use ‘0.0.0.0’ to represent all Azure-internal IP addresses.

3 - Database

Now we can create our database using the following command:

$> az sql db create --resource-group "azure-spring-mssql-rg" --server "azure-spring-mssql-db-server" --name "demoDB" --sample-name AdventureWorksLT --edition GeneralPurpose --family Gen5 --capacity 2 --zone-redundant false

With params:

  • resource-group: the resource group where the database will be created
  • server: the server for the database
  • name: name of the database
  • sample-name: the name of the sample schema to apply when creating this database
  • edition: the edition component of the sku
  • family: the compute generation component of the sku
  • capacity: the number of DTUs or vcores of the sku
  • zone-redundant: zone redundancy (true or false)

4 - Connection String (optional)

If you want to see the connection string, you can run the command:

$> az sql db show-connection-string --client jdbc --name "demoDB" --server "azure-spring-mssql-db-server"

IV — Deploy the application

To deploy the application to Azure we can use azure-maven-plugin to create the App Service and a Service Plan automatically.

For the next of the tutorial, I will create the Service Plan and the App Service using Azure CLI and use the plugin directly in the pom.xml file.

1 - Create App Servcie Plan

To create the App Service Plan we can use the following command with params:

  • name: the name of the App Service Plan
  • resource-group: the resource group where the App Service Plan will be created
  • is-linux: make the App Service Plan on a linux worker
  • location: region of the App Service Plan
  • sku: the pricing tiers (Values: B1, B2, B3, D1, F1, FREE, I1, I1v2, I2, I2v2, I3, I3v2, P1V2, P1V3, P2V2, P2V3, P3V2, P3V3, PC2, PC3, PC4, S1, S2, S3, SHARED)
$> az appservice plan create --name "azure-spring-mssql-sp" --resource-group "azure-spring-mssql-rg" --is-linux --location "West Europe" --sku F1

2 - Create the Web App

To create the Web App we can use the command:

$> az webapp create --name "azure-spring-mssql-app" --plan "azure-spring-mssql-sp" --resource-group "azure-spring-mssql-rg" --runtime "JAVA|11-java11"

With params:

  • name: the name of the Web App
  • resource-group: the resource group where the Web App will be created
  • plan: the name of the service plan
  • runtime: the web runtime for the app

3 - Add the maven plugin

Update your pom.xml file and add the following plugin in the plugins section:

After checking the configuration, you can deploy the application by running the command:

$> mvn package azure-webapp:deploy

V — Test the application

Go to https://azure-spring-mssql-app.azurewebsites.net/student/ and enjoy your app in the cloud !

Final step

  • For the app creation tutorial:

Note that the application is not secured, in fact this is not our objectifve in this tutorial, stay tuned for other tutorials to learn how to secure a web application on Azure

In the next tutorial we will learn how to deploy the application using GitHub Actions.

Et voila ! Thank you.