Deploy SpringBoot API to Azure with MySQL Database

In this tutorial, I will explain to you how to deploy Spring Boot REST API with MySQL database to MS Azure for free using the free tier. We would also, as a prerequisite have our application pushed to Github. In this way, we would also achieve a CI/CD pipeline.

Prerequisites

  • You need to create an account in Azure Portal
  • Sign up for the Free Tier plan which provides you with a $200.00 credit in your billing account

We would cover the following:

  1. Create the Azure App Service
  2. Create the Azure MySQL Database
  3. Obtain and Setup the Credentials
  4. Create and Update the Github Workflow
  5. Obtain the Workflow Yaml

 

1. Create the Azure App Service

Sign in to Azure Portal and select App Services.

Select App Service in Azure
Select App Service in Azure

 

Click on Create -> Web App

Follow the steps to complete creating you web app (watch the video for the procedure)

 

2. Create Azure MySQL Database

We would have to create an Azure MySQL database. Then we would copy the database credentials and use it to update our application properties. Follow the procedure below:

  • Search for Azure Databases for MySQL Servers
  • Click on Create
  • Choose Flexible Servers
  • Click on Create an fill the required details
  • Click on Review and Create and complete the process.
  • Once the creation and deployment is complete
  • Copy the name of the server and update the database url in the application.properties file in Spring Boot
  • Connect to the database using this command
mysql -h productdb2.mysql.database.azure.com -P 3306 -u rootuser -p

 

Once connected, run the create database command and create the database specified in your application.properties file

3. Obtain and setup the Credentials/Secrets

Open the Azure terminal and run this command:

az ad sp create-for-rbac --name "myApp" --role contributor \
    --scopes /subscriptions/{subscription-id}/resourceGroups/{resource-group} \
    --sdk-auth

 

Now we have to copy the json generated in Step 2 above to Github.

  • From your repo in Github, click on Settings.
  • Select Environments from the left.
  • Click on the environment
  • Click on New Environment secret
  • Enter the name as AZURE_CREDENTIALS
  • In the value field, paste the secret you copied and then save.

 

4. Create and Update the Workflow

We would have to create workflow yam file in Github. Follow the steps:

  • From Github, click on the Actions tab and select New Workflow
  • The click on Set up a workflow yourself link
Set up a new workflow yourself
Set up a new workflow yourself

This would open an empty workflow yaml file. You will obtain what you’ll place in this file from Azure.

 

5. Obtain the Workflow Yaml

Follow the steps below:

Open your web app in Azure portal

Click on Deployments as shown below:

Configuring webapp deployment in Azure
Configuring webapp deployment in Azure

 

Click on the Deployment Center link

Make the selections as shown below in the figure below:

Github deployment settings
Github deployment settings

 

In the authentication settings, select User-Assigned Identity

Leave the other entries with their default values

Deployment authentication settings
Deployment authentication settings

 

Then click on Preview File.

Copy the the output to the Github workflow file.

Output of Github Deployment configuration
Output of Github Deployment configuration

 

Enable Public Access

  • Navigate to your app in Azure
  • Click on Networking
  • Check and make sure it’s Enabled

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

Your email address will not be published. Required fields are marked *