In this post, we'll learn what Azure Functions are, and how you can use VS Code to write your first Azure Function in Python code.
I will show how you can create a simple Azure Function which retrieves data from Yahoo Finance (See Using Python and Pandas Datareader to retrieve financial data - part 3: Yahoo Finance) and saves the retrieved data in a CSV file in Azure blob storage. I will be using the Python v1 programming model for Azure Functions since v2 is still in preview.
Introduction to Serverless and Azure Functions
More traditional forms of cloud usage require you to provision virtual machines in the cloud, deploy your code to these VMs, manage resource usage and scaling, keep the OS up to date and the underlying stack, setup monitoring, perform backups, etc...
But if you just want to deploy some piece of code which needs to handle some kind of event, serverless compute might be the right choice for you. With serverless compute, you can develop your applications, deploy it to the serverless service like Azure Functions and you don't need to worry about the underlying hosting architecture. Serverless compute is most of the time cheaper than PAAS or IAAS hosting models.
Several versions of the Azure Functions runtime are available - see Languages by runtime version for an overview which languages are supported in each runtime version. Python 3.7, 3.8 and 3.9 are supported by Azure Functions v2, v3 and v4.
How to create an Azure Function using Azure Portal
You can deploy an Azure Function from your local machine to Azure without leaving VS Code, but I would recommend doing it first using the Azure Portal to understand what VS Code is doing behind the scenes.
To create your Azure Function, click the Create a resource link on the Azure Portal home page and next select Function App.
This brings us to the function creation screen, where we have to provide some configuration details before our function is created:
- Subscription: Azure subscription in which you want to deploy your Azure Function App
- Resource group: container that holds related resources for an Azure solution - these resources typically share the same development lifecycle, permissions and policies, ...
- Function App Name
- Runtime stack: Python
- Version: choose 3.9 (latest supported version) unless you have specific Python version dependencies.
- Region: choose the same region as other resource that you need to deploy e.g., blob storage, Cosmos DB, etc. ...
- Operating system: only Linux is supported
- Plan type: leave it to Consumption (Serverless) unless you have very specific requirements with regards to execution time limit higher than 10 minutes (see Azure functions scale and hosting - function app timeout duration for more details)
- Azure subscription
- Azure Functions Core Tools version 4.x
- Supported Python version (minimum Python 3.6 or a distro like Anaconda or Miniconda
- Visual Studio Code
- Python extension for Visual Studio Code
- Azure Functions extension for Visual Studio Code
- function.json - configuration file for our function
- sample.dat - sample data to test the function
- __init__.py - main file with the code that our function runs
- local.settings.json: stores app settings and connection strings when running locally
- requirements.txt: list of Python packages the system installs when publishing to Azure
- host.json: configuration options that affect all functions in a function app instance
- .venv: folder which contains the Python virtual environment used by local development.
References:
- Quickstart: Create a function in Azure with Python Using Visual Studio Code
- Status of Python versions - info on the Python language version support policy timeline
- Serverless Python Applications with Azure Functions - YouTube recording Build 2019
- An introduction to web scraping with Python and Azure Functions (YouTube)
No comments:
Post a Comment