Azure Classroom Series – 10/Jan/2020

Azure Functions Sample in Python

  • Ensure 64-bit Python is installed.
  • Create a new directory for azure functions mkdir azurefunctionssamples && cd azurefunctionssamples
  • Create an azure functions project
func init myazurefunctionssample --language python
  • Create a new function with Http Trigger
func new --name HttpTrigger --template "HTTP trigger"
  • To run azure function locally on your machine, open cmd or powershell as admin and execute func host start
  • Stop the process by entering ctrl+c in powershell
  • Now in the azurefunctionssamples\requirements.txt ensure you have following
azure-functions
azure
  • From powershell terminal after cd into azurefunctionssamples
pip install -r requirements.txt
  • If azure cli is not installed install as suggested over here
  • For Python SDK usage refer here
  • Get Azure Credentials by executing the following commands
az ad sp create-for-rbac --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"

az account show --query "{ subscription_id: id }"
  • Navigate to HttpTrigger folder_init_.py and make the changes as shown below
import logging
import os

import azure.functions as func
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    subscription_id = os.environ.get(
    'AZURE_SUBSCRIPTION_ID','<your-subcription-id>') # your Azure Subscription Id
    credentials = ServicePrincipalCredentials(
        client_id="<your-client-id>",
        secret="<your-client-secret>",
        tenant="<your-tenant>"
        )
    client = ResourceManagementClient(credentials, subscription_id)
    name = ""
    for item in client.resource_groups.list():
        name = name + item.name + '\n'
    #name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

Azure Logic Apps

  • Before defining Logic App, Lets create one
  • Navigate to Home => All Services => Logic and create the Logic App.
  • Once you navigate to resource, a designer screen is opened where the workflow of your logic can be designed
  • So, Azure Logic Apps is serverless implementation of Workflow.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin