Deploy Java (Spring boot) application with MS-SQL database on Azure (Part 2)
In this part of series, we will deploy the application that we created on the previous tutorial to Azure using the portal.
I — Create a Resource Group
- Connect to Azure Portal: https://portal.azure.com/
- Click on <Resource groups> and click on <+ Create> button
- Subscription: Select your Azure subscription
- Resource group: Enter the resource group name. For example: “azure-spring-mssql-rg”
- Region: Select a Region from the list. Make sure to select the same region for all next resources. For Example: France Central
- Click on <Review + Create>
3. Now, you can find the newly created resource group in the <Resource Groups> list.
II — Create the database
- Connect to Azure Portal: https://portal.azure.com/
- Click on <Create a resource>
- Select <Database> in the left menu
- Click on <SQL Database> and select <Create>
5. Select your Azure subscription
6. Select the newely created resource group: “azure-spring-mssql-rg”
7. Enter the database name, for example: “demoDB”
8. For server, click on <Create new> and fill out the server form
- Server name: enter “my-mssql-server-0123”
- Server admin login: enter “YOUR_ADMIN_LOGIN”
- Password: enter “YOUR_STRONG_PASSWORD”
- Location: select France Central (or any other region near you)
- Click <OK>
9. For Computer + Storage, select <Basic>
10. Click the <Review + create> button
11. When finish, click on <Go to resource>
12. In the left menu, click on <Firewalls and virtual networks>
13. Click <+ Add client IP> to add your local IP address (Optional)
14. Click <Yes> to allow Azure services and resources to access this server
Don’t do this for an application on production, by clicking <Yes> you’re allowing all Azure subsciptions to access your database server
15. Click <Save>
III — Run the application locally using the newly reated database (Optional)
Make sure to add your local IP address in Azure before running the application on local machine (Step 13).
- Change your application.yml file
2. Run your application without any active profile
$> mvn spring-boot:run
3. Check the database creation by using any SQL Server client. For example I’m using Azure Data Studio to connect to Azure databases.
4. Enjoy !
IV — Create the Azure Web App
- Connect to Azure Portal: https://portal.azure.com/
- Click on <Create a resource>
- Select <Web> in the left menu
- Click <Create Web App>. If not present, you can use the searching box and search for “web app”
- Fill the form with tho following data:
- Subscription: select your Azure subscription
- Resource group: select the new created resource group. For example “azure-spring-mssql-rg”
- Name: enter the web app name. For example: “azure-spring-mssql-app”
- Publish: select <Code>
- Runtime stack: select <Java 11>
- Java web server stack: select <Java SE (Embeded Web Server)>
- Operating System: select <Linux>
- Region: select France Central (or any other region near you)
- Linux Plan: click <Create new> and enter a name for the new Linux Plan. For example “azure-spring-mssql-sp”
- Sku and size: click <Dev / Test> and select <F1> and then click <Apply>
6. Click <Review + create>
V — Deploy the application
To deploy a Maven project directly to Azure App Service we need the azure-maven-plugin.
First, sign-in to Azure by using Azure CLI (A pop-up will appear and then you can connect or select your Azure accoun):
$> az login
Then you can prepare the application for Azure Web App with the command:
$> mvn com.microsoft.azure:azure-webapp-maven-plugin:1.14.0:config
This command will add the azure-webapp-maven-plugin plugin to your pom.xml file after prompting you to select an existing Web App or to create a new one.
Next is the newly created plugin in pom.xml file:
After checking the configuration, you can deploy the application by running the command:
$> mvn package azure-webapp:deploy
Just wait for it to be deployed, then you should have a Build Success like this:
Go to the Web App resource in Azure portal and click in the URL to interact with your application.
You can use Postman to list / add / update / delete students
Final step
- For the application creation tutorial:
- You can find the source code on my Github: https://github.com/benstitou/azure-spring-mssql-demo
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
Et voila ! Thank you, and enjoy your spring application in the cloud :-)