Prerequisites

To build and test locally, you will need to:

To install Azure Functions Core tools you will need also these requirements:

  1. Install .NET Core 2.1 for Windows.
  2. Install Node.js, which includes npm. For version 2.x of the tools, only Node.js 8.5 and later versions are supported.
  3. Install the Core Tools package:
    npm install -g azure-functions-core-tools

Create and activate a virtual environment

To create a Functions project, it is required that you work in a Python 3.6 virtual environment. Run the following commands to create and activate a virtual environment named env.

# In Bash
python3.6 -m venv
env source env/bin/activate

# In PowerShell
py -3.6 -m venv
env env\scripts\activate

# In CMD
python -m venv
env env\scripts\activate

Create a local Functions Project

To create a Functions project, it is required that you work in a Python 3.6 virtual environment. Run the following commands to create and activate a virtual environment named env. In the terminal window or from a command prompt, run the following command:

func init MyFunctionProj –worker-runtime python –docker
func init . –worker-runtime python –docker

You will see something like the following output.

Installing wheel package
Installing azure-functions==1.0.0a5 package
Installing azure-functions-worker==1.0.0a6 package
Running pip freeze
Writing .funcignore
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing D:\__Documents\WebAppProjects\functions-python-3\.vscode\extensions.json
Writing Dockerfile

This generates a Dockerfile with the following content:

FROM mcr.microsoft.com/azure-functions/python:2.0

COPY . /home/site/wwwroot

RUN cd /home/site/wwwroot && \

    pip install -r requirements.txt

Create a function

https://github.com/Azure/azure-functions-python-worker/wiki/Create-your-first-Python-function#create-a-function

Run the function locally

https://github.com/Azure/azure-functions-python-worker/wiki/Create-your-first-Python-function#run-the-function-locally

Run locally as Docker container

Using Docker, run the following command.
Replace <imagename> and <tag> with preferred names.

docker build . –tag <imagename>:<tag>
(example) docker build . — pythonfunction:v1

Once image has been build you could run the following command to start the image:

docker run -p <portexposed>:<portusedinside> <imagename>:<tag>
(example) docker run -p 8080:80 pythonfunction:v1

Once started, you could browse to http://localhost:8080 (using example) to view your function running.

Deploy your function app

You could use Docker Hub or Azure Container Registry to deploy your Docker image.

Push image to Docker Hub

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#push-image-to-acr

First Create a container registry

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#create-a-container-registry

Log in to ACR

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#create-a-container-registry

Push image to ACR

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#push-image-to-acr

Create function app via Azure Portal

Select the New button found on the upper left-hand corner of the Azure portal, then select Compute > Function App.

When configuring the settings, select Linux (Preview) for OS and Docker Image for Publish.

Under Configure Container, select either Azure container Registry or Docker Hub depending on how you deployed your Docker image.

Once deployed, you will be able to access the Function Apps page

Once deployed, you can test your Function App:

Be sure to change <functionappname> and <function-name> with your functions values.

https://<functionappname>.azurewebsites.com/api/<function-name>

Leave a Reply

Close Menu