-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from ks6088ts/feature/issue-86_fastapi-azfunc
add Azure Functions template using FastAPI
- Loading branch information
Showing
14 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
local.settings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
VERSION=0.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
.venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"recommendations": [ | ||
"ms-azuretools.vscode-azurefunctions" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"IsEncrypted": false, | ||
"Values": { | ||
"FUNCTIONS_WORKER_RUNTIME": "python", | ||
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing", | ||
"AzureWebJobsStorage": "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
templates/azure_functions_basic/scripts/deploy_resources.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |