Azure Functions: Deploy a Python app through Visual Studio Code

No comments

pythonazure

Introduction:

Back in August, Microsoft GAed Python Support for Azure functions. Ever since then, I was trying to get a small hands-on tutorial blogged to demo that and how you can use Visual Studio Code to do the full deployment for you.
After a quick Google/Bing search, I could tell that there are many demos when it comes to deploying apps using Amazon’s Lambda but very few when it comes to Azure Functions.
I hope this small demo will help you see the power of Azure Functions and how easy and simple it is to use. As an Engineer who has mostly been focused on IaaS, Azure Functions might sound scary and like a black box to many of us but this post aims to show you how it is the simplest thing there is and as we all know, server-less is in fashion these days :D.

Time Needed:
Pre-reqs (if not previously setup): 15 mins
The actual steps to deploy the Azure Function: 10 mins
Total: 25 mins

Pre-requisites:

  • Python 3.7 or 3.6
  • The Azure Functions Core Tools
    • This will first involve installing Node.js and might be a bit confusing.
    • When you are on the Nodes.js page, click on nvm-windows (for windows of course)
    • Then download the setup file from github
    • After running it, open cmd and type nvm install latest
    • Now type nvm use “The version you installed”
    • Now run npm install -g azure-functions-core-tools
      nvm commands
  • Visual Studio Code
  • Python Extension on VS Code.
  • Azure Functions Extension on VS Code.

 

Steps:

  • Login to Azure on VSCode through the Azure Extension. The image below is after you sign-in.
    login-screenshot
  •  Open the integrated Terminal and type func. To open the Terminal, press F1 and select Terminal: Create New Integrated Terminal. If func, is not recognized, run the following command npm install -g azure-functions-core-tools (if you cannot find npm, it means node.js was not properly installed. Follow the instructions above in the pre-req section)
  • Now that we have confirmed that you have Azure Function core tools setup properly on your local environment. Lets create a Python Function
  • Click on New Project under Azure Functions and select a directory to save the project in.
    newproject
  • Now Select the Language to your Functions app Project. In our case, this will be Python.
  • VSCode might ask for your python environment, if you are not seeing any that means python was NOT installed.
  • For the Template, we will select HTTP Trigger.
  • Now Give the Project a Name.
  • For Authorization, select Anonymous.
  •  Now Select, Open in Current Window and let the Magic Start!!
    pythonproject
  • You should now see a similar window like the one shown above.
  •  To get more details on the important parts of the code, check this URL out: https://docs.microsoft.com/en-us/azure/python/tutorial-vs-code-serverless-python-03

Debug Locally

  • Now click on the Debug and Run Icon on the left and click on Attach to Python Functions.
    attachtopython
  • When the URL is available, ctrl+click on the link. You should see a message that states that its missing the name parameter, you can do that by adding ?name=Hammad to the end of the URL.
    click on URL
  • The above proves that you were able to test the function locally and that it all looks great.

Deploy To Azure

  • Now we will deply to Azure by clicking on the Deploy to Azure Icon as shown below.
    deplytoAzure
  • Enter a unique name for the function, the environment and the resource group. Now watch the function get deployed.
    gettingdeployed

Testing and Further Options

  • You will receive a confirmation message from VSCode that the deployment is complete. When you click on output, you can test the URL and test your first Python app on Azure functions!!
    confirmationmessageGetURL
  • You can also stream the logs live, send them to Application Insights or even store them in an Azure Blob Storage. For that you just need to add a binding within the function and adjust your  init.py file accordingly. But this is beyond the scope of our simple tutorial. To do it for yourself, check the following Microsoft post which also shows all the above steps in more detail: https://docs.microsoft.com/en-us/azure/python/tutorial-vs-code-serverless-python-07
    TheApp

Conclusion:

So as you saw above, we deployed a complete Python Application on Azure Functions without opening the Azure portal at all. All through Azure Visual Code.

Since Python is one of the fastest growing languages and loved by DevOps Engineers for automating tasks. Python for Azure Functions can be a very cheap and effective option to run python scripts through simple http triggers. From taking snapshots for VMs in the cloud environments to deploying whole new environments, the Cloud is your limit :D.

 

 

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s