Skip to content

Commit

Permalink
Merge pull request #1 from pamelafox/azd-support
Browse files Browse the repository at this point in the history
Azd support
  • Loading branch information
pamelafox authored Feb 24, 2023
2 parents c3a3897 + e6528fe commit 2a73e7b
Show file tree
Hide file tree
Showing 39 changed files with 3,737 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ARG VARIANT=bullseye
FROM --platform=amd64 mcr.microsoft.com/devcontainers/python:0-${VARIANT}
RUN curl -fsSL https://aka.ms/install-azd.sh | bash \
&& curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg \
&& mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg \
&& sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/debian/$(lsb_release -rs | cut -d'.' -f 1)/prod $(lsb_release -cs) main" > /etc/apt/sources.list.d/dotnetdev.list' \
&& apt-get update && apt-get install -y azure-functions-core-tools-4 \
&& apt update && apt-get install -y xdg-utils
37 changes: 37 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "FastAPI on Azure Functions",
"build": {
"dockerfile": "Dockerfile",
"args": {
"VARIANT": "3.9-bullseye"
}
},
"forwardPorts": [8000, 7071],
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {
"version": "2"
},
"ghcr.io/devcontainers/features/node:1": {
"version": "16",
"nodeGypDependencies": false
}
},
"customizations": {
"vscode": {
"extensions": [
"ms-azuretools.azure-dev",
"ms-azuretools.vscode-bicep",
"ms-azuretools.vscode-docker",
"ms-vscode.vscode-node-azure-pack",
"ms-python.python",
"ms-azuretools.vscode-azurefunctions"
]
}
},
"postCreateCommand": "python3 -m venv .venv",
"postAttachCommand": ". .venv/bin/activate",
"remoteUser": "vscode",
"hostRequirements": {
"memory": "8gb"
}
}
23 changes: 23 additions & 0 deletions .github/workflows/azure-bicep-validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Validate bicep scripts
on:
workflow_dispatch:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Azure CLI script
uses: azure/CLI@v1
with:
inlineScript: az bicep build -f infra/main.bicep
62 changes: 62 additions & 0 deletions .github/workflows/azure-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Azure Developer CLI

on:
workflow_dispatch:
push:
branches:
- main
- azd-support

permissions:
id-token: write
contents: read

jobs:
build:
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/azure-dev-cli-apps:latest
env:
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Log in with Azure (Federated Credentials)
if: ${{ env.AZURE_CLIENT_ID != '' }}
run: |
azd login `
--client-id "$Env:AZURE_CLIENT_ID" `
--federated-credential-provider "github" `
--tenant-id "$Env:AZURE_TENANT_ID"
shell: pwsh

- name: Log in with Azure (Client Credentials)
if: ${{ env.AZURE_CREDENTIALS != '' }}
run: |
$info = $Env:AZURE_CREDENTIALS | ConvertFrom-Json -AsHashtable;
Write-Host "::add-mask::$($info.clientSecret)"
azd login `
--client-id "$($info.clientId)" `
--client-secret "$($info.clientSecret)" `
--tenant-id "$($info.tenantId)"
shell: pwsh
env:
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}

- name: Azure Dev Provision
run: azd provision --no-prompt
env:
AZURE_ENV_NAME: ${{ secrets.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ secrets.AZURE_LOCATION }}
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: Azure Dev Deploy
run: azd deploy --no-prompt
env:
AZURE_ENV_NAME: ${{ secrets.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ secrets.AZURE_LOCATION }}
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ local.settings.json
__blobstorage__
__queuestorage__
__azurite_db*__.json
.python_packages
.python_packages
.azure
8 changes: 7 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
"tasks": [
{
"type": "func",
"label": "func: host start",
"command": "host start",
"problemMatcher": "$func-python-watch",
"isBackground": true,
"dependsOn": "pip install (functions)"
"dependsOn": "pip install (functions)",
"options": {
"env": {
"PYTHON_ISOLATE_WORKER_DEPENDENCIES": "1"
}
}
},
{
"label": "pip install (functions)",
Expand Down
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Clone or download [this sample's repository](https://github.com/Azure-Samples/fa

The code in the sample folder has already been updated to support use of the FastAPI. Let's walk through the changed files.

The `requirements.txt` file has an additional dependency of the `fastapi` and `nest_asyncio` modules:
The `requirements.txt` file has an additional dependency of the `fastapi` module:

```
azure-functions
Expand Down Expand Up @@ -111,7 +111,7 @@ async def main(req: func.HttpRequest, context: func.Context) -> func.HttpRespons
First run the command below to install the necessary requirements.

```log
pip3 install -r requirements.txt
python3 -m pip install -r requirements.txt
```

If you are using VS Code for development, follow [the instructions for running a function locally](https://docs.microsoft.com/azure/azure-functions/create-first-function-vs-code-python#run-the-function-locally). Otherwise, follow [these instructions for using Core Tools commands directly to run the function locally](https://docs.microsoft.com/azure/azure-functions/functions-run-local?tabs=v4%2Cwindows%2Cpython%2Cportal%2Cbash#start).
Expand All @@ -130,9 +130,15 @@ http://localhost:7071/sample
http://localhost:7071/hello/YourName
```

### Testing in Azure
### Deploying to Azure

There are three main ways to deploy this to Azure:

If you are using VS Code for development, follow [these instructions for using the extension to create resources and deploying to Azure](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python#publish-the-project-to-azure). Otherwise, follow [these instructions for using the Azure CLI to create resources and deploy to Azure](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-python?tabs=azure-cli%2Cbash%2Cbrowser#create-supporting-azure-resources-for-your-function).
* [Deploy with the VS Code Azure Functions extension](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python#publish-the-project-to-azure).
* [Deploy with the Azure CLI](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-python?tabs=azure-cli%2Cbash%2Cbrowser#create-supporting-azure-resources-for-your-function).
* Deploy with the Azure Developer CLI: After [installing the `azd` tool](https://learn.microsoft.com/en-us/azure/developer/azure-developer-cli/install-azd?tabs=localinstall%2Cwindows%2Cbrew), run `azd up` in the root of the project. You can also run `azd pipeline config` to set up a CI/CD pipeline for deployment.

### Testing in Azure

Once deployed, test different paths on the deployed URL, using either a browser or a tool like Postman.

Expand All @@ -141,6 +147,10 @@ http://<FunctionAppName>.azurewebsites.net/sample
http://<FunctionAppName>.azurewebsites.net/hello/Foo
```

If you get an error about `handle_async` not being defined, that is likely because the Azure Functions runtime doesn't yet have the latest version of `azure-functions`.
To work around that for now, add an environment value with the name `PYTHON_ISOLATE_WORKER_DEPENDENCIES` and value of `1`.
That environment variable ensures that the packages in your `requirements.txt` are installed in a separate virtual environment than the packages of the functions runtime.

## Conclusion and Next Steps

Now you have a simple Azure Function App using the FastAPI framework, and you can continue building on it to develop more sophisticated applications.
Expand Down
9 changes: 9 additions & 0 deletions azure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json

name: fastapi-on-azure-functions

services:
api:
project: .
language: py
host: function
Loading

0 comments on commit 2a73e7b

Please sign in to comment.