6

Serverless Python Bot Using Azure Function

 2 years ago
source link: https://python.plainenglish.io/serverless-python-bot-using-azure-function-90da5de713f7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Responses

Also publish to my profile

There are currently no responses for this story.

Be the first to respond.

You have 2 free member-only stories left this month.

Serverless Python Bot Using Azure Function

A step-by-step tutorial: From API to Database

illustration by Author

Azure Functions is a serverless cloud service available on-demand that supports major programming runtimes such as Python, Node.js, Java, Powershell Core, etc. It will run your application without a need to maintain the machine/server yourself.

In my last chapters, I had introduced the equivalent serverless cloud services to execute Python automatically using — AWS Lambda and Google Cloud Function. And, some have asked me about doing it using Microsoft Azure services and what the differences are. In this article, I give you a step-by-step tutorial on how to deploy Python scripts using the Azure Function in an easy way.

Step-by-Step Tutorial

Use case: Stuttgart Airport Wait Times ✈️

I live in Stuttgart and have to travel often. Sometimes, I have to wait over an hour and sometimes no queue at all. The good news is that the Stuttgart airport website shows the current waiting time which updates every few minutes. However, the website does not share historical data. Here, I will use my Python bot to store historical waiting time and store it in the database. With this average data, I can use it to predict patterns and trends to estimate my waiting prior to my future travels.

1*MNEK-qbTQ4ipiG-CyodHpg.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7
A screenshot from https://www.stuttgart-airport.com/security-wait-times/

Step 0: Understanding the Structure

We want to extract the airport security waiting time data and insert it into the database every 5 minutes. A Python script is used to scrape this data from the website and then update it to the PostgreSQL database as in the figure below:

1*8ZpV_X1_DKOsL4GENKeZAQ.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7

A — Website: https://www.stuttgart-airport.com/security-wait-times/

B — Python Process: Azure Function

C — Database: PostgreSQL

Step 1: Prepare Python Script

To scrape data out of the website, usually, use the BeautifulSoup (bs4) module to extract data out of the HTML contents as the following example:

Fortunately, the data we are targeting is shared as a tabular on the site, so we can use the Pandas module to extract it directly as in this example. After all, this is our final script to extract the waiting time data:

Line [2–3]: Load modules

Line [6–11]: Extract HTML data to Pandas dataframe

Line [14–19]: Connect to database

Step 2: Upload Python Script to Azure Function

I found that there are several ways to do create an azure project and upload your script. In this article, I will recommend the way that I found easiest and most effective. You don't really install Azure CLI to your PC. I would recommend “VS Code” as a tool to edit and upload your Python Script for the Azure Function project.

  • Register for the Azure account
  • Install VS Code (if you haven't had it yet)
  • Open VS Code and install Azure Tools or Azure Functions Extension (Only one of them is enough). I would recommend Azure Tools as it combines with all other Azure features for your other projects too.
1*hEQnyB43CVAR3ikHmRj3Uw.jpeg?q=20
serverless-python-bot-using-azure-function-90da5de713f7
Install Azure Extension in VS Code.
  • Click on the installed Azure menu and log in to your account. The new browser window will pop up and you are prompted to log in with your Microsoft account. Now, your VS code is ready!
  • In VS Code, “open folder” where you would like to initiate the Python Function project.
  • Select Azure menu, under Functions, click on this button to create a new project.
1*k5oQsrUwr5P320sl9qyTpQ.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7
  • You will be prompted to enter a Folder to initiate the project, pick the current one. Select “Python” as the interpreter. You may skip the virtual environment for now.
  • Select “Timer trigger” as the starter template. You may enter default CRON which can be edited later.
  • After that, a set of files will be created in our project folder. Edit the “__init__.py” by adding your Python script from Step 1.
1*ShfXmk7UsX6rEJExNJQTZw.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7
  • Add the module dependencies in the “requirements.txt”. *Azure does not support psycopg2, you need psycopg2-binary instead if your project requires it.
1*YIgnpre9Tb1XwAnd0qYJpw.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7
  • You can edit the time schedule to trigger your Python script using CRON in the “function.json”.
1*D1BpMGz1BJo-Fcvv8cgj6g.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7
  • Please note that you need to use CRON in a six-part format that contains “Second” as a schedule binding above.
1*ZbjGCeaTN4RZIv0Wynynjw.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7
* * * * * *
- - - - - -
| | | | | |
| | | | | +--- day of week (0 - 6) (Sunday=0)
| | | | +----- month (1 - 12)
| | | +------- day of month (1 - 31)
| | +--------- hour (0 - 23)
| +----------- min (0 - 59)
+------------- sec (0 - 59)
  • Now, everything is ready. Head to the Azure menu again and click the deploy button and select the project we created before. This action will upload and replace all the files we just edit to our Azure function project.
1*6umsqLGwAmd_h_yRocI1Gg.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7

Step 3: Testing

  • After the project is uploaded, you may check the log stream by clicking on the “Connect to Log Stream…” The log page will pop up on the new tab of your browser.
Azure Function Log Result

That’s it! Our Python bot is now fully operational on the cloud and automatically scrape and store Stuttgart airport security wait times to the database.

1*xSWmdfhl7UTEq0-XcyeXMg.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7
Stuttgart Airport Security Wait Times

Bonus: this is a short script I used to plot the chart above

What about Azure Function Cost?

According to Microsoft Azure, it provides a free grant of 1 million requests and 400,000 GB-s of resource consumption per month per subscription in pay-as-you-go pricing across all function apps in that subscription.

How much exactly is this free “400,000 GB-s” limit?

To understand this better, let’s do an estimation calculation from our use case. Just head to the log section to see the peak value of consumption units used during function execution.

1*SNYF1UbR-QAFu9mRq9sGLg.png?q=20
serverless-python-bot-using-azure-function-90da5de713f7
Azure Function Execution Units used in MB milliseconds to run the Python function in our use case.

In our case, the highest consumption takes 550000 Function Execution Units in MB milliseconds. To convert to GB-seconds, divide by 1024000.

550000 MB-millisec / 1024000 = 0.537109375 GB-sec/execution.

The execution trigger every 5 minutes, which means it takes

60/5 x 24 x 30 = 8640 executions/month.

0.5371 GB-sec/execution x 8640 executions/month = 4640 GB-sec/month

That means our function takes only 4640/400000 ~ 1.16% from the free tier allowance. So, you could do a lot of things with this free tier limit. Anyway, it is best to check the consumption early to plan your budget. After 400000 execution in a month costs $0.000016/GB-s.

Deployment Difficulty

I found that deploying serverless functions in Azure is more complex to learn in the beginning because we need to maintain several files in the project using the given template and have to use Azure extension in VS Code or Azure CLI to deploy. While we can just upload Python script and requirement.txt in Google Cloud Function/ AWS Lambda via browser. But then, Azure becomes much easier to deploy or edit the script afterward after it is set up and synced to the Azure account.

Alternative?

Yes, there are several options out there which I wrote details about it before.

I hope you enjoy my articles and find them useful for your daily work or projects. Please, feel free to contact me if you have any questions.

Be Safe and Healthy!
Thank you for Reading. 👋😄

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK