DEV Community

Dhanush Reddy
Dhanush Reddy

Posted on

 

Using Azure's F1 Free Plan to Host a REST API

In this tutorial, we'll explore how to build and deploy a REST API using Azure App Service and the free F1 plan. We'll use the Flask web framework to build the API, and we'll use Azure's continuous deployment feature to automatically deploy our code from a Git repository. By the end of this tutorial, you'll have a fully functional REST API running on Azure's F1 plan.

Microsoft Azure

Flask is a popular Python web framework for building REST API's and web applications quickly. If you have built a Flask app and want to deploy it on Microsoft Azure, the following steps will guide you through the process of deploying your app on Azure's F1 free plan. There are two ways of deploying a Flask app on Azure, via the CLI or directly using the Azure Portal. In both the cases you need a Git Repo that has your code similar to this repo. You do not need to setup the Github Actions/Workflow file like in my repo. Azure will automatically create it for you. You just need the requirements.txt and app.py files initially to be more specific.

Prerequisites

Before you begin, you should have the following things set up:

  • A Microsoft Azure account. You can sign up for a free account at azure.com. If you are a student, you can create a free Azure account and get a free $100 credit even without a credit card. For more details: azure.microsoft.com/en-in/free/students

  • The Azure CLI (not needed for Azure Portal version) installed on your computer. You can install the Azure CLI by following the instructions at learn.microsoft.com/en-us/cli/azure/install-azure-cli.

  • A Git repository for your Flask app. If you don't have one already, you can create a new repository on GitHub or any other Git hosting service.

  • The latest version of Python and pip installed on your computer.

Deploying the Flask App via Azure Portal

To deploy a web app using the Azure portal without using the Azure CLI, follow these steps:

  • Sign in to the Azure portal at portal.azure.com.

  • Click the Create a resource button in the top-left corner of the portal.

  • In the Search box, search for Web App and select it from the results.

  • Click the Create button to open the Create Web App blade.

  • In the Create Web App, specify the following details:

    • Resource group: Select an existing resource group or create a new one.
    • Name: Enter a unique name for your web app.
    • Operating system: Select "Linux".
    • Region: Select a region where you want to deploy your web app.
    • App Service plan/Location: Select Create new and specify a name for the App Service plan. Then, select the F1 Free pricing tier and choose a location.
    • Runtime stack: Select the version of Python you want to use for your web app.
  • Click the Create button to create the web app.

Connecting the Git Repo for Continuous Deployment

Once the web app is created, you can link it to a Git repository by following these steps:

  • In the Azure portal, go to the Deployment options of your web app.

  • Select Deployment Center and follow the prompts in connecting your Github Repo.

  • Click the Save button to save your changes.

  • Click the Sync button and Github Actions will be automatically triggered to clone the repo and build the web app.

That's it. Your web app will now be deployed from the Git repository. Any future changes you push to the repository will be automatically deployed to the web app.

Now we shall see how we can do the same by using Azure CLI:

Deploying the Flask App using CLI

  • First, log in to your Azure account using the Azure CLI by running the following command in your terminal:
az login
Enter fullscreen mode Exit fullscreen mode
  • Create a new resource group for your Flask app by running the following command:
az group create --name my-resource-group --location westus
Enter fullscreen mode Exit fullscreen mode

I have chosen westus for my location. You can chose the location as per your website's traffic.

  • Create a new web app in the resource group you just created by running the following command:
az webapp create --name my-flask-app --resource-group my-resource-group --plan F1
Enter fullscreen mode Exit fullscreen mode
  • Set up continuous deployment for your web app by running the following command:
az webapp deployment source config --name my-flask-app --resource-group my-resource-group --repo-url {enter your-git-repo-url here}
Enter fullscreen mode Exit fullscreen mode
  • In your Flask app's root directory, create a file called runtime.txt and add the following line to specify the Python version to use:
python-3.9
Enter fullscreen mode Exit fullscreen mode
  • Create a file called requirements.txt and add the required Python packages to it. For flask it is just Flask:
Flask
Enter fullscreen mode Exit fullscreen mode

gunicron is provided automatically, so no need of adding it.

  • In the app.py file, make sure that the Flask app is created using the Flask class from the flask module. For example:
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')
def get_data():
    data = {
        'name': 'Dhanush',
        'age': 20,
        'city': 'Hyderabad'
    }
    return jsonify(data)
Enter fullscreen mode Exit fullscreen mode

This Flask app has a single endpoint at "/", which returns a JSON object containing some basic information about a person.

  • Commit and push your changes to your Git repository. Azure will automatically detect the changes and deploy your Flask app.

That's it! You have successfully deployed your Flask app on Microsoft Azure's F1 free plan. You can access your app at the URL provided by Azure, which should be in the format https://{YOUR_APP_NAME}.azurewebsites.net. The web app that I built is available at: flaskonazure29.azurewebsites.net.

Few points to note:

  • The F1 plan in Azure App Service is a free plan that is intended for testing and development purposes. It has the following limitations:
  • Limited compute and memory resources: The F1 plan provides a shared instance of a single CPU core and 1 GB of memory. This may not be sufficient for more resource-intensive applications.
  • Limited storage: The F1 plan provides only 1 GB of storage, which may not be enough for storing large files or data.
  • Limited traffic: The F1 plan allows only up to 1 hour of CPU usage in a day. This may not be sufficient for applications with high traffic.
  • Limited features: The F1 plan does not include some features that are available on paid plans, such as custom domains, SSL certificates, and auto-scaling.
  • It's important to keep these limitations in mind when using the F1 plan for testing and development purposes. If you need more resources or features, you may need to upgrade to a paid plan.

I hope this information is helpful. In case if you still have any questions regarding this post or want to discuss something with me feel free to connect on LinkedIn or Twitter.

If you run an organisation and want me to write for you, please connect with me on my Socials 🙃

Top comments (0)

50 CLI Tools You Can't Live Without

The top 50 must-have CLI tools, including some scripts to help you automate the installation and updating of these tools on various systems/distros.