Deploy an app to Azure App Service

This is a year of changes and of learning. Things are going back to normal, but it’s still unclear what will happen in the near future. Maybe we’ll have to go back inside again. It’s been hard for everybody, but it’s also an opportunity to try something new. This is what I’ve been doing. Taking on new projects, studying and planning. This blog is to share my path, to document my progress and to interact and keep learning. I hope you like it. Please feel free to say anything you want. I love to chat.

I’ll be posting a series of experiments involving the cloud, DevOps and Linux. This is the first of a series. Besides that, I may write something on productivity, organization, management and other stuff that will eventually come to my mind.

Let’s go!

For this lab, I deployed a Python app to Azure App Service. Applications in App Service can run on Windows or Linux environments. For this lab, the app will be running on Linux. This app uses the Flask framework.

Before starting, we need to have Git and Azure CLI installed, and an Azure account.

The first step is to clone the repository from GitHub to our local machine. This is a Python app I grabbed from Microsoft’s Azure Samples GitHub page.

git clone https://github.com/Azure-Samples/python-docs-hello-world

This will create a new directory. To move into this new directory, we run:

cd python-docs-hello-world

We then proceed to create a virtual environment for our project. This is done so all the app’s dependencies are installed in an isolated environment, detached from the local machine. Like that, everybody working on the project will have the exact same environment, with the same software versions.

To install the required dependencies in our local environment, we run:

pip install -r requirements.txt

I used Windows PowerShell for this project. These are the commands to run to create and activate the virtual environment:

python -m venv env
env/scripts/activate

Running the application locally

We always try the app locally, to see if everything’s working as it should be. To run the Flask app, we type:

Set-Item Env:FLASK_APP ".\application.py"
flask run

This is the output we should see.

If we visit the URL indicated, we should see the app running with “Hello World!”.

Deploying the app

To deploy apps to Azure App Service, you’ll need a deployment user. This is not the same as the Azure subscription credentials.

az webapp deployment user set --user-name [username] --password [password]

The output of the command should look like this.

We then create a resource group, where the App Service will reside. This is the command I ran to create the resource group, which I named HelloPythonWorld.

az group create --name HelloPythonWorld --location westeurope

In App Service, an app runs in an App Service Plan. An App Service Plan defines a set of compute resources for the app to run. This is the command I ran to create the App Service Plan. The F1 sku is the free pricing tier. The –is-linux parameter indicates this is a Linux container.

az appservice plan create --name HelloPythonWorld --resource-group HelloPythonWorld --sku F1 --is-linux

This is the output we should see for the creation of the App Service Plan.

Notice the line where it says “provisioningState”. It should say “Succeeded”. It’s all looking good!

In Azure CLI, we can use the az webapp up command to create the app and deploy the code. In this case, I had previously created the resource group and the App Service Plan. I wasn’t sure, though, if I could have run this command directly, without the need to run the az appservice plan create. Both commands can take the same parameters. This is something to be investigated.

az webapp up --sku F1 --name HelloPythonWorld --location westeurope --plan HelloPythonWorld --resource-group HelloPythonWorld

This command should give the following output.

The app is now up and running!

Changing our code and Redeploying

Now, we’ll change something in our app and redeploy it. My editor of choice is Visual Studio Code, and to open it from the terminal, we just type this.

code .

Visual Studio Code will load with the current working directory open. We choose the correct Python environment, in this case the virtual environment we created earlier. I changed the text from “Hello World!” to “Hello Cloud!”

To deploy the changes, we simply run the az webapp up again.

az webapp up

Now we see the changes we made.

Success!

Deploying the app with Git

That is a very simple way to deploy a zipped package of the app. We can alternatively use Git for our deployments. Let’s do it!

For this new deployment, I deleted everything and started from scratch.

Let’s create the web app, now defining Local Git as the deployment mode. This time we’ll use the az webapp create command.

az webapp create --resource-group HelloPythonWorld --name HelloPythonWorld --plan HelloPythonWorld --runtime "PYTHON|3.7" --deployment-local-git

This is what we should see after running the command. I had to run this command in Azure Cloud Shell because for some reason my local Azure CLI terminal wouldn’t take it. Another thing to investigate.

Notice the line that says “deploymentLocalGitUrl”. It also appears in the first line, in yellow. This is the URL we need to use for the next command.

The above command creates an empty web app with git deployment enabled. Now let’s deploy our code with these two commands. The first sets the git deploy and the second pushes our code.

git remote add azure [deploymentLocalGitUrl]
git push azure master

When you run the above command, you’ll be prompted for your Deployment User credentials. These are the ones you created in the step above. This action takes some time.

To deploy changes, we commit and then push the changes.

git commit -am "updated text"
git push azure master

And this is the result!

Let’s talk! Please write or comment.

See you in the next deployment!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s