Deploy a basic python bot created using Bot Framework SDK to Azure. We will deploy an Echo that we created in our last blog using Python.
Prepare the Bot for Deployment
When we created the Python bot locally in our last blog using the Cookiecutter template, it contained the deployment templates as well along with the bot source code. We will make use of these templates for deployment. These are the ARM templates used for managing Azure resources.
Check the Azure CLI version by running the below command in any command-line tool. I will be showing the demo in Windows OS and will be working on Command Prompt.
Make sure the Azure CLI version is above 2.2.0, else you might face some errors. We will log in to Azure using the Azure CLI by running the below command.
This command will launch a login page in the browser prompting you to log in to Azure. Once the log-in is successful, your Azure subscription details will be shown in the command prompt/command line.
You might have multiple Azure subscriptions connected to your account. Set the default subscription using the below command.
az account set --subscription "<azure-subscription-id>"
azure-subscription-id value can be found from the above screenshot having the
id parameter. You can also find this value from the Azure Portal -> Subscription -> Subscription ID.
To connect the bot to various channels, we need an Azure application registration created. Use the below command to create an app in Azure AD.
az ad app create --display-name "displayName" --password "AtLeastSixteenCharacters_0" --available-to-other-tenants
The above command has the following parameters –
- display-name: Name of your app that will be listed in Azure AD.
- password: This is a client secret. It must be at least sixteen characters long, contain at least one upper- or lower-case alphabetical character, and contain at least one special character.
- available-to-other-tenants: Enable it to get this app available to other tenants and work with Azure Bot Channels.
Make a note of
appId that is generated and the client secret/password you provided when creating the app registration. This is required for deployment.
Deploy Python Bot using Azure Resource Manager (ARM) Template [Create Azure Resources]
If you see the
deploymentTemplates folder in your project directory, there you will find two templates. The first one is used for deploying to a new resource group. The second one is for deploying to an existing resource group.
An ARM template is a JSON file that declaratively defines one or more Azure resources and that defines dependencies between the deployed resources.
We will make use of 1st template that creates the new resource group, and new app service plan. Use the below command to start the process.
az deployment sub create --template-file "<path-to-template-with-new-rg.json" --location <region-location-name> --parameters appId="<app-id-from-previous-step>" appSecret="<password-from-previous-step>" botId="<id or bot-app-service-name>" botSku=F0 newAppServicePlanName="<new-service-plan-name>" newWebAppName="<bot-app-service-name>" groupName="<new-group-name>" groupLocation="<region-location-name>" newAppServicePlanLocation="<region-location-name>" --name "<bot-app-service-name>"
The above command has the following options –
- name: Name of the deployment. I will use
- template-file: Path of the
template-with-new-rg.jsonfile present inside the
deploymentTemplatesfolder. My path is D:\Chatbot\echo-bot\deploymentTemplates\template-with-new-rg.json.
- location: Region where you want to deploy your bot. You can get the list of location using the below command.
az account list-locations
The red arrows are pointed to the location short name that is to be provided in the deployment command. I will use
eastus as the location.
- parameters: These are the deployment parameters having below values –
- appId: The appId that was generated from the app registration output.
- appSecret: The value you provided to create the app registration.
botId: Global unique value. I will use
- botSku: The pricing tier; it can be F0 (Free) or S1 (Standard). I will use the free pricing tier.
- newAppServicePlanName: The name of the new application service plan. I will use
- newWebAppName: A name for the bot application service. I will use
- groupName: A name for the new resource group. I will use
- groupLocation: The location of the Azure resource group. I will use the same location that I used in deployment option, i.e.,
- newAppServicePlanLocation: The location of the application service plan. I will use
eastusas the location.
Below is my final command with all the values that I used for deployment.
az deployment sub create --template-file "D:\Chatbot\echo-bot\deploymentTemplates\template-with-new-rg.json" --location eastus --parameters appId="808c125e-********" appSecret="*********" botId="python-echo-bot-jd-bots" botSku=F0 newAppServicePlanName="python-echo-bot-asp" newWebAppName="python-echo-bot-dev-app" groupName="PythonBot-RG" groupLocation="eastus" newAppServicePlanLocation="eastus" --name "Python-Echo-Bot-Deployment"
You can check the Azure portal and see if the resource group is created along with 3 resources in it.
Prepare your Bot source Code for Deployment
Till now, we have created all the resources that are required to host our bot application. Now, we will package the source code into a zip file and deploy it.
The folder you have to zip is your root project folder where
app.py file is present. Use the below command to create a zip file. Make sure you are in the root directory of your project. My root directory is
Compress-Archive -Path * -DestinationPath pythonEchoBot.zip
Use the PowerShell to run this command, because the command prompt will not recognize the command you run. The command created the zip file inside the root directory of my project.
Deploy the Python Basic Echo Bot to Azure
Now, let us deploy our code to Azure using the below command.
az webapp deployment source config-zip --resource-group "<resource-group-name>" --name "<name-of-web-app>" --src "<project-zip-path>"
My command looks like this as shown below.
az webapp deployment source config-zip --resource-group "PythonBot-RG" --name "python-echo-bot-dev-app" --src "D:\Chatbot\echo-bot\pythonEchoBot.zip"
This will take some time for the deployment to complete and few more minutes before you can test your bot.
Test Python Basic Echo Bot in Web Chat
Log in to Azure Portal. Click on Resource Groups from the home page.
Click on the resource group that was created using the deployment template.
Open the resource of type Azure Bot.
Click on Test in Web Chat.
Wait for the Welcome message to come and then start chatting with the bot. It took around 5-10 minutes for the bot to work after deployment.
For the regular deployments, you don’t have to follow all the steps as above. Just start from Prepare your Bot Source Code for Deployment.