Skip to content

Commit 8a4fb6f

Browse files
authored
Merge pull request #7 from ks6088ts-labs/feature/issue-6_add-azure-functions
add azure functions
2 parents b6cff1a + c4e7a7c commit 8a4fb6f

File tree

10 files changed

+1027
-1
lines changed

10 files changed

+1027
-1
lines changed

.env.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VERSION=0.0.0

docs/index.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# References
2+
3+
## Azure Functions
4+
5+
Run the function app locally
6+
7+
```shell
8+
# Run the function app locally with the Azure Functions Core Tools
9+
$ poetry run func start
10+
```
11+
12+
Deploy the function app to Azure
13+
14+
```shell
15+
# Deploy resources to Azure
16+
$ bash scripts/deploy_azure_functions_resources.sh
17+
18+
$ export FUNCTION_APP_NAME="CHANGE_ME"
19+
20+
# Publish the function app to Azure
21+
$ bash scripts/publish_azure_functions.sh
22+
```
23+
24+
### References
25+
26+
- [Using FastAPI Framework with Azure Functions](https://learn.microsoft.com/en-us/samples/azure-samples/fastapi-on-azure-functions/fastapi-on-azure-functions/)

function_app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import azure.functions as func
2+
from dotenv import load_dotenv
3+
4+
from workshop_azure_iot.core import app as fastapi_app
5+
6+
load_dotenv()
7+
8+
app = func.AsgiFunctionApp(
9+
app=fastapi_app,
10+
http_auth_level=func.AuthLevel.ANONYMOUS,
11+
)

host.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": "2.0",
3+
"extensions": {
4+
"http": {
5+
"routePrefix": ""
6+
}
7+
}
8+
}

local.settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"FUNCTIONS_WORKER_RUNTIME": "python",
5+
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
6+
"AzureWebJobsStorage": ""
7+
}
8+
}

poetry.lock

Lines changed: 916 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ package-mode = false
99

1010
[tool.poetry.dependencies]
1111
python = "^3.10"
12+
python-dotenv = "^1.0.1"
13+
typer = "^0.12.5"
14+
fastapi = {extras = ["standard"], version = "^0.115.2"}
15+
azure-functions = "^1.21.3"
1216

1317
[tool.poetry.group.dev.dependencies]
1418
pre-commit = "^3.8.0"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
set -eux
4+
5+
# Variables
6+
LOCATION=japaneast
7+
RANDOM_SUFFIX=$(openssl rand -hex 4)
8+
RESOURCE_GROUP_NAME="rg-adhoc-azure-functions-$RANDOM_SUFFIX"
9+
STORAGE_NAME=stadhoc"$RANDOM_SUFFIX"
10+
FUNCTION_APP_NAME=adhoc-azure-functions-"$RANDOM_SUFFIX"
11+
12+
# Create a resource group
13+
az group create \
14+
--name "$RESOURCE_GROUP_NAME" \
15+
--location "$LOCATION"
16+
17+
# Create a storage account
18+
az storage account create \
19+
--name "$STORAGE_NAME" \
20+
--location "$LOCATION" \
21+
--resource-group "$RESOURCE_GROUP_NAME" \
22+
--sku Standard_LRS
23+
24+
# Create a function app
25+
az functionapp create \
26+
--resource-group "$RESOURCE_GROUP_NAME" \
27+
--consumption-plan-location "$LOCATION" \
28+
--runtime python \
29+
--runtime-version 3.11 \
30+
--functions-version 4 \
31+
--name "$FUNCTION_APP_NAME" \
32+
--os-type linux \
33+
--storage-account "$STORAGE_NAME"

scripts/publish_azure_functions.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
set -eux
4+
5+
poetry export -f requirements.txt -o requirements.txt --without-hashes
6+
7+
func azure functionapp publish $FUNCTION_APP_NAME

workshop_azure_iot/core.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import logging
2+
from os import getenv
3+
4+
from fastapi import FastAPI
25

36
logger = logging.getLogger(__name__)
7+
app = FastAPI(
8+
docs_url="/",
9+
)
10+
11+
12+
@app.get("/info")
13+
def read_root():
14+
return {
15+
"version": getenv("VERSION", "0.0.0"),
16+
}
417

518

619
def hello_world(verbose: bool = False):

0 commit comments

Comments
 (0)