5 min Azure Functions compute 2

Overview

Azure Functions Overview | Microsoft Learn

Create a Python function using Visual Studio Code – Azure Functions | Microsoft Learn

Follow the stuff with run locally

https://towardsdatascience.com/tutorial-of-python-azure-functions-81949b1fd6db

Files

Init default

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    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}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

Run it f5, view terminal

Functions:

        coinfunction01: [GET,POST] http://localhost:7071/api/coinfunction01

Visit URL

Stop shift + f5 stop if shift + 5, and press any key in terminal

Folder structure, we added coinworker.py and urls_firi.py

New init code

import logging
import coinworker as cw

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    worker = cw.CoinWorker()
    logging.info('Python HTTP trigger function processed a request.' + str(worker))

    return func.HttpResponse(str(worker.get_solnok()), mimetype="text/json")

Run it f5, stop if shift + 5, and press any key in terminal

visit URL

Ok, we are happy.

Deploy Azure

Select subscription Choose the subscription to use.

You won’t see this prompt when you have only one subscription visible under Resources.

Enter a globally unique name for the function app Type a name that is valid in a URL path. The name you type is validated to make sure that it’s unique in Azure Functions.


Select a runtime stack Choose the language version on which you’ve been running locally.


Select a location for new resources Choose a region for your function app.

View in Azure we we got

Visit URL

https://prodcoinfunc01.azurewebsites.net

Deploy

Are we sure, yes.

Completed

View output

https://prodcoinfunc01.azurewebsites.net

https://prodcoinfunc01.azurewebsites.net/api/coinfunction01

Hm, just blank page

Edit init to

import logging
# import coinworker as cw
import random

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    # worker = cw.CoinWorker()
    logging.info('Python HTTP trigger function processed a request.')
    # comment
    # Test change on GET
    ran  = random.randint(0,89)
    return func.HttpResponse(str(ran), mimetype="text/json")
    # return func.HttpResponse(str(worker.get_solnok()), mimetype="text/json")

Visit URL

That works, something wrong from local to Azure, hm.

View logs

Monitoring Azure Functions with Azure Monitor Logs | Microsoft Learn

python requests – Calling a REST API using Azure function App and store data in Azure container – Stack Overflow

sucks, only show a var, hm….

Must update requirements, args since import requests. API should work, perfect.

Now it works

https://prodcoinfunc01.azurewebsites.net/api/coinfunction01

All the code is now in init, can move this to different modules now, the import was the issue