Skip to content

Latest commit

 

History

History
126 lines (88 loc) · 4.25 KB

File metadata and controls

126 lines (88 loc) · 4.25 KB

Cloud Native Python with Azure Container Apps, Azure Container Registry, and FastAPI on PyPy

Walkthrough (vimeo.com)

In this lab you will containerize an existing Python application using the Azure CLI, a private Azure Container Registry, and Azure Container Registry Tasks. You will then deploy it to Azure Container Apps.

Azure Container Apps enables you to run microservices and containerized applications on a serverless platform. With Container Apps, you enjoy the benefits of running containers while leaving behind the concerns of manually configuring cloud infrastructure and complex container orchestrators.

Requirements

1. Clone Sample

git clone https://github.com/asw101/python-fastapi-pypy.git

cd python-fastapi-pypy/

2. Install Azure CLI Extension and Register Resource Providers

If this is the first time you have used Azure Container Apps from the Azure CLI, or with your Azure Account, you will need to install the containerapp extension, and register the resource providers for Microsoft.App and Microsoft.OperationalInsights using the following commands.

az extension add --name containerapp

az provider register --namespace Microsoft.App --wait

az provider register --namespace Microsoft.OperationalInsights --wait

3. Set Environment Variables

RESOURCE_GROUP="my-container-apps"
LOCATION="canadacentral"
CONTAINERAPPS_ENVIRONMENT="my-environment"

SUBSCRIPTION_ID=$(az account show --query id --out tsv)
SCOPE="/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}"
[[ -z "${RANDOM_STR:-}" ]] && RANDOM_STR=$(echo -n "$SCOPE" | shasum | head -c 6)

ACR_NAME="acr${RANDOM_STR}"
ACR_IMAGE_NAME="pypy-fastapi:latest"

4. Create Resource Group

az group create \
  --name $RESOURCE_GROUP \
  --location $LOCATION

5. Create Azure Container Registry

Quickstart (docs.microsoft.com)

az acr create --resource-group $RESOURCE_GROUP \
  --name $ACR_NAME \
  --sku Basic \
  --admin-enabled true

az acr build -t $ACR_IMAGE_NAME -r $ACR_NAME .

CONTAINER_IMAGE="${ACR_NAME}.azurecr.io/${ACR_IMAGE_NAME}"
REGISTRY_SERVER="${ACR_NAME}.azurecr.io"
REGISTRY_USERNAME="${ACR_NAME}"
REGISTRY_PASSWORD=$(az acr credential show -n $ACR_NAME --query 'passwords[0].value' --out tsv)

echo "$CONTAINER_IMAGE"

6. Create Azure Container Apps Environment

Quickstart (docs.microsoft.com)

az containerapp env create \
  --name $CONTAINERAPPS_ENVIRONMENT \
  --resource-group $RESOURCE_GROUP \
  --location $LOCATION

7. Create Container App

az containerapp create \
  --name my-container-app \
  --resource-group $RESOURCE_GROUP \
  --environment $CONTAINERAPPS_ENVIRONMENT \
  --image "$CONTAINER_IMAGE" \
  --registry-server "$REGISTRY_SERVER" \
  --registry-username "$REGISTRY_USERNAME" \
  --registry-password "$REGISTRY_PASSWORD" \
  --target-port 80 \
  --ingress 'external'

8. Test Container App with curl

CONTAINERAPP_FQDN=$(az containerapp show --resource-group $RESOURCE_GROUP \
  --name my-container-app \
  --query properties.configuration.ingress.fqdn \
  --out tsv)

echo "https://${CONTAINERAPP_FQDN}"

curl "https://${CONTAINERAPP_FQDN}/"

9. Delete Resource Group

az group delete \
  --name $RESOURCE_GROUP

Notes