Steps to deploy an HTTP API in Azure functions
Create function
Install dependencies
Before you can get started, you should install Node.js which includes npm.
Run the following command to install the Core Tools package:
npm install -g azure-functions-core-tools@4 --unsafe-perm true
Create an Azure Functions project
In the terminal window or from a command prompt, navigate to an empty folder for your project, and run the following command:
You will also be prompted to choose a runtime for the project. Select .
Create a function
To create a function, run the following command:
This will prompt you to choose a template for your function. We recommend HTTP trigger for getting started.
Run your function project locally
Run the following command to start your function app:
The runtime will output a URL for any HTTP functions, which can be copied and run in your browser's address bar.
To stop debugging, use Ctrl-C in the terminal.
Deploy your code to Azure
To publish your Functions project into Azure, enter the following command:
func azure functionapp publish django-azure-http
You may be prompted to sign into Azure (if not logged in already).
FAQS
Value of AzureWebJobsStorage app setting is invalid
Possible Issues and respective solutions:
AzureWebJobsStorage env var not cofigured for the app, set the env var in the azure console to fix the issue.
AzureWebJobsStorage value set in the env vars is invalid, check once if it is in the below format:
DefaultEndpointsProtocol=https;AccountName=****;AccountKey=*******;EndpointSuffix=core.windows.net
Django App Deployment
Method 1 (with serverless)
create a file azure_handler.py
at project root level with below content.
# azure_handler.py
import azure.functions as func
from <your_project>.wsgi import application
def handler(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
return func.WsgiMiddleware(application).handle(req, context)
create a file serverless.yml
at project root level with below content.
# serverless.yml
service: django-app-deployment
useDotenv: true
provider:
name: azure
region: EAST US
runtime: python3.8
prefix: ""
stage: ${opt:stage, 'dev'}
environment:
DJANGO_SETTINGS_MODULE: ${self:custom.config.DJANGO_SETTINGS_MODULE}
AZ_STORAGE_ACCOUNT_NAME: ${self:custom.config.AZ_STORAGE_ACCOUNT_NAME}
AZ_STORAGE_CONTAINER: ${self:custom.config.AZ_STORAGE_CONTAINER}
AZ_STORAGE_KEY: ${self:custom.config.AZ_STORAGE_KEY}
AZ_DB_ENGINE: ${self:custom.config.AZ_DB_ENGINE}
AZ_DB_HOSTNAME: ${self:custom.config.AZ_DB_HOSTNAME}
AZ_DB_PORT: ${self:custom.config.AZ_DB_PORT}
AZ_DB_USERNAME: ${self:custom.config.AZ_DB_PASSWORD}
AZ_DB_PASSWORD: ${self:custom.config.AZ_DB_PASSWORD}
AZ_DB_NAME: ${self:custom.config.AZ_DB_NAME}
plugins:
- serverless-azure-functions
custom:
config:
DJANGO_SETTINGS_MODULE: ${env:DJANGO_SETTINGS_MODULE}
AZ_STORAGE_ACCOUNT_NAME: ${env:AZ_STORAGE_ACCOUNT_NAME}
AZ_STORAGE_CONTAINER: ${env:AZ_STORAGE_CONTAINER}
AZ_STORAGE_KEY: ${env:AZ_STORAGE_KEY}
AZ_DB_ENGINE: ${env:AZ_DB_ENGINE, 'django.db.backends.mysql'}
AZ_DB_HOSTNAME: ${env:AZ_DB_HOSTNAME}
AZ_DB_PORT: ${env:AZ_DB_PORT}
AZ_DB_USERNAME: ${env:AZ_DB_USERNAME}
AZ_DB_PASSWORD: ${env:AZ_DB_PASSWORD}
AZ_DB_NAME: ${env:AZ_DB_NAME}
package:
patterns:
- '!.venv/**'
- '!__pycache__/**'
- '!.vscode/**'
- '!.idea/**'
functions:
usersCreate:
handler: azure_handler.handler
events:
- http: true
authLevel: anonymous
name: req
methods:
- post
- get
route: "{*route}"
create a dir inside your django project let say azure_app
.
create a file azure_handler.py
with below content.
import azure.functions as func
from <your_project>.wsgi import application
def handler(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
return func.WsgiMiddleware(application).handle(req, context)