Skip to content

Commit

Permalink
Merge pull request #87 from ks6088ts/feature/issue-86_fastapi-azfunc
Browse files Browse the repository at this point in the history
add Azure Functions template using FastAPI
  • Loading branch information
ks6088ts authored Sep 8, 2024
2 parents 9f3e8b0 + cdc0e0b commit d15ffe8
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 0 deletions.
1 change: 1 addition & 0 deletions templates/azure_functions_basic/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local.settings.json
1 change: 1 addition & 0 deletions templates/azure_functions_basic/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION=0.0.0
3 changes: 3 additions & 0 deletions templates/azure_functions_basic/.funcignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


.venv
52 changes: 52 additions & 0 deletions templates/azure_functions_basic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
bin
obj
csx
.vs
edge
Publish

*.user
*.suo
*.cscfg
*.Cache
project.lock.json

/packages
/TestResults

/tools/NuGet.exe
/App_Data
/secrets
/data
.secrets
appsettings.json
local.settings.json

node_modules
dist

# Local python packages
.python_packages/

# Python Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Azurite artifacts
__blobstorage__
__queuestorage__
__azurite_db*__.json

# Project
!requirements.txt
!local.settings.json
5 changes: 5 additions & 0 deletions templates/azure_functions_basic/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"ms-azuretools.vscode-azurefunctions"
]
}
11 changes: 11 additions & 0 deletions templates/azure_functions_basic/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/python:4-python3.11-appservice
FROM mcr.microsoft.com/azure-functions/python:4-python3.11

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY requirements.txt /
RUN pip install -r /requirements.txt

COPY . /home/site/wwwroot
34 changes: 34 additions & 0 deletions templates/azure_functions_basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Azure Functions in Python

## Usage

```shell
mkdir azure_functions_basic
cd azure_functions_basic

# Create a new Azure Functions project
func init --python --docker

# Create virtual environment
python -m venv .venv

# Activate the virtual environment
source .venv/bin/activate

# Install the required packages
pip install -r requirements.txt

# Run the project locally
func start --verbose
```

## Deploy

```shell
# Deploy resources to Azure
bash scripts/deploy_resources.sh

# Deploy the Function App to Azure
FUNCTION_APP_NAME="adhoc-azure-functions-..."
func azure functionapp publish $FUNCTION_APP_NAME
```
14 changes: 14 additions & 0 deletions templates/azure_functions_basic/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from os import getenv

from fastapi import FastAPI

app = FastAPI(
docs_url="/",
)


@app.get("/info")
def read_root():
return {
"VERSION": getenv("VERSION", "0.0.0"),
}
10 changes: 10 additions & 0 deletions templates/azure_functions_basic/function_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import azure.functions as func
from core import app as fastapi_app
from dotenv import load_dotenv

load_dotenv()

app = func.AsgiFunctionApp(
app=fastapi_app,
http_auth_level=func.AuthLevel.ANONYMOUS,
)
20 changes: 20 additions & 0 deletions templates/azure_functions_basic/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
},
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
8 changes: 8 additions & 0 deletions templates/azure_functions_basic/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"AzureWebJobsStorage": ""
}
}
3 changes: 3 additions & 0 deletions templates/azure_functions_basic/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
azure-functions==1.20.0
python-dotenv==1.0.1
fastapi==0.114.0
34 changes: 34 additions & 0 deletions templates/azure_functions_basic/scripts/deploy_resources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

# Variables
LOCATION=japaneast
RANDOM_SUFFIX=$(openssl rand -hex 4)
RESOURCE_GROUP_NAME="rg-adhoc-azure-functions-$RANDOM_SUFFIX"
STORAGE_NAME=stadhoc"$RANDOM_SUFFIX"
FUNCTION_APP_NAME=adhoc-azure-functions-"$RANDOM_SUFFIX"

# Create a resource group
az group create \
--name "$RESOURCE_GROUP_NAME" \
--location "$LOCATION"

# Create a storage account
az storage account create \
--name "$STORAGE_NAME" \
--location "$LOCATION" \
--resource-group "$RESOURCE_GROUP_NAME" \
--sku Standard_LRS

# Create a function app
az functionapp create \
--resource-group "$RESOURCE_GROUP_NAME" \
--consumption-plan-location "$LOCATION" \
--runtime python \
--runtime-version 3.11 \
--functions-version 4 \
--name "$FUNCTION_APP_NAME" \
--os-type linux \
--storage-account "$STORAGE_NAME"

# create json file containing the resource group name and storage account name
echo "{\"RESOURCE_GROUP_NAME\": \"$RESOURCE_GROUP_NAME\", \"STORAGE_NAME\": \"$STORAGE_NAME\", \"FUNCTION_APP_NAME\": \"$FUNCTION_APP_NAME\"}"
11 changes: 11 additions & 0 deletions tests/templates/test_azure_functions_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi import status
from fastapi.testclient import TestClient

from templates.azure_functions_basic.core import app

client = TestClient(app)


def test_root():
response = client.get("/info")
assert response.status_code == status.HTTP_200_OK

0 comments on commit d15ffe8

Please sign in to comment.