Example: Use the Azure libraries to provision and deploy a web app
This example demonstrates how to use the Azure SDK management libraries in a Python script to provision a web app on Azure App Service and deploy app code from a GitHub repository. (Equivalent Azure CLI commands are given at later in this article.)
All the commands in this article work the same in Linux/macOS bash and Windows command shells unless noted.
1: Set up your local development environment
If you haven't already, follow all the instructions on Configure your local Python dev environment for Azure.
Be sure to create a service principal for local development, and create and activate a virtual environment for this project.
2: Install the needed Azure library packages
Create a file named requirements.txt with the following contents:
azure-mgmt-resource
azure-mgmt-web
azure-identity
In a terminal or command prompt with the virtual environment activated, install the requirements:
pip install -r requirements.txt
3: Fork the sample repository
Visit https://github.com/Azure-Samples/python-docs-hello-world and fork the repository into your own GitHub account. You use a fork to ensure that you have permissions to deploy the repository to Azure.

Then create an environment variable named REPO_URL with the URL of your fork. The example code in the next section depends on this environment variable:
4: Write code to provision and deploy a web app
Create a Python file named provision_deploy_web_app.py with the following code. The comments explain the details:
import random, os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.web import WebSiteManagementClient
# Acquire a credential object using CLI-based authentication.
credential = AzureCliCredential()
# Retrieve subscription ID from environment variable
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
# Constants we need in multiple places: the resource group name and the region
# in which we provision resources. You can change these values however you want.
RESOURCE_GROUP_NAME = 'PythonAzureExample-WebApp-rg'
LOCATION = "centralus"
# Step 1: Provision the resource group.
resource_client = ResourceManagementClient(credential, subscription_id)
rg_result = resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME,
{ "location": LOCATION })
print(f"Provisioned resource group {rg_result.name}")
# For details on the previous code, see Example: Provision a resource group
# at https://docs.microsoft.com/azure/developer/python/azure-sdk-example-resource-group
#Step 2: Provision the App Service plan, which defines the underlying VM for the web app.
# Names for the App Service plan and App Service. We use a random number with the
# latter to create a reasonably unique name. If you've already provisioned a
# web app and need to re-run the script, set the WEB_APP_NAME environment
# variable to that name instead.
SERVICE_PLAN_NAME = 'PythonAzureExample-WebApp-plan'
WEB_APP_NAME = os.environ.get("WEB_APP_NAME", f"PythonAzureExample-WebApp-{random.randint(1,100000):05}")
# Obtain the client object
app_service_client = WebSiteManagementClient(credential, subscription_id)
# Provision the plan; Linux is the default
poller = app_service_client.app_service_plans.begin_create_or_update(RESOURCE_GROUP_NAME,
SERVICE_PLAN_NAME,
{
"location": LOCATION,
"reserved": True,
"sku" : {"name" : "B1"}
}
)
plan_result = poller.result()
print(f"Provisioned App Service plan {plan_result.name}")
# Step 3: With the plan in place, provision the web app itself, which is the process that can host
# whatever code we want to deploy to it.
poller = app_service_client.web_apps.begin_create_or_update(RESOURCE_GROUP_NAME,
WEB_APP_NAME,
{
"location": LOCATION,
"server_farm_id": plan_result.id,
"site_config": {
"linux_fx_version": "python|3.8"
}
}
)
web_app_result = poller.result()
print(f"Provisioned web app {web_app_result.name} at {web_app_result.default_host_name}")
# Step 4: deploy code from a GitHub repository. For Python code, App Service on Linux runs
# the code inside a container that makes certain assumptions about the structure of the code.
# For more information, see How to configure Python apps,
# https://docs.microsoft.com/azure/app-service/containers/how-to-configure-python.
#
# The create_or_update_source_control method doesn't provision a web app. It only sets the
# source control configuration for the app. In this case we're simply pointing to
# a GitHub repository.
#
# You can call this method again to change the repo.
REPO_URL = os.environ["REPO_URL"]
poller = app_service_client.web_apps.begin_create_or_update_source_control(RESOURCE_GROUP_NAME,
WEB_APP_NAME,
{
"location": "GitHub",
"repo_url": REPO_URL,
"branch": "master"
}
)
sc_result = poller.result()
print(f"Set source control on web app to {sc_result.branch} branch of {sc_result.repo_url}")
This code uses CLI-based authentication (using AzureCliCredential) because it demonstrates actions that you might otherwise do with the Azure CLI directly. In both cases you're using the same identity for authentication.
To use such code in a production script (for example, to automate VM management), use DefaultAzureCredential (recommended) or a service principal based method as described in How to authenticate Python apps with Azure services.
Reference links for classes used in the code
- AzureCliCredential (azure.identity)
- ResourceManagementClient (azure.mgmt.resource)
- WebSiteManagementClient (azure.mgmt.web import)
5: Run the script
python provision_deploy_web_app.py
6: Verify the web app deployment
Visit the deployed web site by running the following command:
az webapp browse -n PythonAzureExample-WebApp-12345Replace "PythonAzureExample-WebApp-12345" with the specific name of your web app.
You should see "Hello, World!" in the browser.
Visit the Azure portal, select Resource groups, and check that "PythonAzureExample-WebApp-rg" is listed. Then Navigate into that list to verify the expected resources exist, namely the App Service Plan and the App Service.
7: Clean up resources
az group delete -n PythonAzureExample-WebApp-rg --no-wait
Run this command if you don't need to keep the resources provisioned in this example and would like to avoid ongoing charges in your subscription.
You can also use the ResourceManagementClient.resource_groups.delete method to delete a resource group from code.
For reference: equivalent Azure CLI commands
The following Azure CLI commands complete the same provisioning steps as the Python script:
az group create -l centralus -n PythonAzureExample-WebApp-rg
az appservice plan create -n PythonAzureExample-WebApp-plan --is-linux --sku F1
az webapp create -g PythonAzureExample-WebApp-rg -n PythonAzureExample-WebApp-12345 ^
--plan PythonAzureExample-WebApp-plan --runtime "python|3.8"
rem You can use --deployment-source-url with the first create command. It is shown here
rem to match the sequence of the Python code.
az webapp create -n PythonAzureExample-WebApp-12345 --plan PythonAzureExample-WebApp-plan ^
--deployment-source-url %REPO_URL% --runtime "python|3.8"
rem Replace <your_fork> with the specific URL of your forked repository.
See also
- Example: Provision a resource group
- Example: List resource groups in a subscription
- Example: Provision Azure Storage
- Example: Use Azure Storage
- Example: Provision and use a MySQL database
- Example: Provision a virtual machine
- Use Azure Managed Disks with virtual machines
- Complete a short survey about the Azure SDK for Python
الملاحظات
إرسال الملاحظات وعرضها المتعلقة بـ