Skip to content

Latest commit

 

History

History
142 lines (101 loc) · 13.6 KB

File metadata and controls

142 lines (101 loc) · 13.6 KB

Azure Web App with Custom Docker Image

This sample demonstrates a Python Flask web application hosted on an Azure Web App using a custom Docker image. The app runs on an Azure App Service Plan and uses a container image stored in an Azure Container Registry. For more information on configuring a web app to use a custom container, see Configure a custom container for Azure App Service.

Architecture

The following diagram illustrates the architecture of the solution:

Architecture Diagram

The solution is composed of the following Azure resources:

  1. Azure Resource Group: A logical container scoping all resources in this sample.
  2. Azure Virtual Network: Hosts two subnets:
  3. Azure Private DNS Zone: Handles DNS resolution for the Azure Container Registry Private Endpoint within the virtual network.
  4. Azure Private Endpoint: Secures network access to the Azure Container Registry via a private IP within the VNet.
  5. Azure NAT Gateway: Provides deterministic outbound connectivity for the Web App. Included for completeness; the sample app does not call any external services.
  6. Azure Network Security Group: Enforces inbound and outbound traffic rules across the virtual network's subnets.
  7. Azure Log Analytics Workspace: Centralizes the diagnostic logs and metrics of every resource, the telemetry of Application Insights, and the audit records of the queries run on it.
  8. Azure Application Insights <prefix>-insights-<suffix>: A workspace-based resource that collects the requests, the log records, the exceptions, the custom events and the custom metric of the web app, which the Microsoft OpenTelemetry Distro sends to the connection string of the APPLICATIONINSIGHTS_CONNECTION_STRING app setting. See Application Insights.
  9. Azure Container Registry: A fully-managed container registry service based on the open-source Docker platform used to hold the container image used by the web app.
  10. User-Assigned Managed Identity: Assigned the AcrPull role on the Azure Container Registry, enabling the Web App to pull the container image without storing credentials.
  11. Azure App Service Plan: The underlying compute tier that hosts the web application.
  12. Azure Web App: Runs the Python Flask application from the custom container image stored in the Azure Container Registry.

Prerequisites

Deployment

Set up the Azure emulator using the LocalStack for Azure Docker image. Before starting, ensure you have a valid LOCALSTACK_AUTH_TOKEN to access the Azure emulator. Refer to the Auth Token guide to obtain your Auth Token and set it in the LOCALSTACK_AUTH_TOKEN environment variable. The Azure Docker image is available on the LocalStack Docker Hub. To pull the image, execute:

docker pull localstack/localstack-azure

Start the LocalStack Azure emulator by running:

# Set the authentication token
export LOCALSTACK_AUTH_TOKEN=<your_auth_token>

# Start the LocalStack Azure emulator
lstk start --type azure

# Route all Azure CLI calls to the LocalStack Azure emulator
lstk az start-interception

Deploy the application to LocalStack for Azure using one of these methods:

All deployment methods have been fully tested against Azure and the LocalStack for Azure local emulator.

Note

When you deploy the application to LocalStack for Azure for the first time, the initialization process involves downloading and building Docker images. This is a one-time operation—subsequent deployments will be significantly faster. Depending on your internet connection and system resources, this initial setup may take several minutes.

Test

You can use the call-web-app.sh Bash script below to call the web app. The script calls the web app via the default hostname <web-app-name>.azurewebsites.azure.localhost.localstack.cloud:4566.

Application Insights

The web app sends its telemetry to <prefix>-insights-<suffix>, a workspace-based Application Insights resource that stores it in the <prefix>-log-analytics-<suffix> workspace. The three deployment methods set the APPLICATIONINSIGHTS_CONNECTION_STRING app setting to the connection string of the resource, and the application in the image turns on the Microsoft OpenTelemetry Distro when the setting is present. The application also sends the StatusRequested custom event for each call to /api/status, and counts these calls in the status_requests custom metric:

from microsoft.opentelemetry import use_microsoft_opentelemetry

# The logger of the application
LOGGER_NAME = "webapp"

if os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING"):
    use_microsoft_opentelemetry(enable_azure_monitor=True, logger_name=LOGGER_NAME)

from flask import Flask, jsonify, render_template
from opentelemetry import metrics

status_counter = metrics.get_meter(LOGGER_NAME).create_counter(
    "status_requests", unit="{request}", description="The number of status requests the app answered."
)


@app.route("/api/status")
def status():
    image = image_name()
    hostname = socket.gethostname()
    logger.info(
        f"StatusRequested: {image}",
        extra={"microsoft.custom_event.name": "StatusRequested", "image.name": image, "hostname": hostname},
    )
    status_counter.add(1, {"image": image})

The container runs flask run, which imports Flask before the application: the distro still wraps the application, because its Flask instrumentation replaces the Flask class that app.py imports after the distro starts.

The workspace keeps the telemetry in these tables:

Table What the web app sends
AppRequests The requests it serves, such as GET / and GET /api/status
AppTraces The records of its loggers
AppEvents The StatusRequested custom events, with the image in image.name and the container's host name in hostname
AppMetrics The status_requests custom metric, with the image in image
AppExceptions The exceptions of the requests that fail, with their stack trace

scripts/query-telemetry.sh calls the home page and the status endpoint, and reads the tables back, retrying until their records arrive. It shows the exceptions without waiting for them: a healthy run has none.

Logs and metrics

The web app, its App Service plan, the container registry, the VNet, the two NSGs, the NAT Gateway and its public IP prefix each have a diagnostic setting that sends their resource logs, their platform metrics or both to the <prefix>-log-analytics-<suffix> Log Analytics workspace, and the workspace has one that sends the audit records of the queries run on it (Audit, table LAQueryLogs) to itself. The Application Insights resource has none: its telemetry is already in the workspace it's based on, and a diagnostic setting of a workspace-based Application Insights resource can't send to that workspace. The private endpoint, the private DNS zone and the managed identity offer no diagnostic settings.

Resource Log categories Tables in the workspace What it records
Web App AppServiceHTTPLogs, AppServiceConsoleLogs, AppServicePlatformLogs, AppServiceAppLogs, AppServiceAuditLogs, AppServiceIPSecAuditLogs, AppServiceAuthenticationLogs One table per category, named after it The requests it serves, its console output and the start of its container
Container registry ContainerRegistryLoginEvents, ContainerRegistryRepositoryEvents ContainerRegistryLoginEvents, ContainerRegistryRepositoryEvents The logins to the registry and the pushes and pulls of its images, once the setting exists: the deployment pushes the image before it creates the setting

Their platform metrics land in the AzureMetrics table. The VNet and the two NSGs send their network logs; on the emulator these categories stay empty, because no traffic crosses an emulated network security group.

scripts/query-logs-and-metrics.sh shows the diagnostic settings, calls the web app and reads these tables back, retrying until their records arrive, then reads the same platform metrics from the Azure Monitor metrics API. On Azure the first records of a new diagnostic setting can take up to 90 minutes: raise RETRY_COUNT there. On Azure the script reads the exported platform metrics (AzureMetrics) only once: Azure exports them late and irregularly, so the script shows those that arrived and notes how many are still missing, without failing.

References