Skip to content

Commit

Permalink
add Azure Functions template
Browse files Browse the repository at this point in the history
  • Loading branch information
ks6088ts committed Sep 8, 2024
1 parent fed922e commit 4039872
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 6 deletions.
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
2 changes: 2 additions & 0 deletions templates/azure_functions_basic/.funcignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@


.venv
6 changes: 5 additions & 1 deletion templates/azure_functions_basic/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ __pycache__/
# Azurite artifacts
__blobstorage__
__queuestorage__
__azurite_db*__.json
__azurite_db*__.json

# Project
!requirements.txt
!local.settings.json
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"),
}
12 changes: 8 additions & 4 deletions templates/azure_functions_basic/function_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import azure.functions as func
import datetime
import json
import logging
from core import app as fastapi_app
from dotenv import load_dotenv

app = func.FunctionApp()
load_dotenv()

app = func.AsgiFunctionApp(
app=fastapi_app,
http_auth_level=func.AuthLevel.ANONYMOUS,
)
7 changes: 6 additions & 1 deletion templates/azure_functions_basic/host.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
},
"logging": {
"applicationInsights": {
"samplingSettings": {
Expand All @@ -12,4 +17,4 @@
"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\"}"

0 comments on commit 4039872

Please sign in to comment.