Wednesday, July 10, 2019

Azure Functions






Azure functions is the event driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in Azure or third party service as well as on-premises systems.Azure Functions is the serverless computing service hosted on the Microsoft Azure public cloud. Azure Functions, and serverless computing, in general, is designed to accelerate and simplify application development.

Traditional application development demands a consideration of the underlying IT infrastructure. For cloud computing, an IT team must create, monitor and pay for cloud computing instances -- regardless of how much work that instance actually does for the business.

The idea behind serverless computing, also known as function as a service, is to eliminate those infrastructure considerations for the user. With serverless, a user can simply create and upload code, and then define the triggers or events that will execute the code. Triggers can come from a wide range of sources, including another user's application or other cloud services, such as databases and event and notification hubs.

Once a trigger or event occurs, it is the cloud provider's responsibility to load the code into a suitable execution environment, run the code and then release the compute resources. There are still servers involved, but the user no longer needs to provision or manage compute instances. In addition, rather than pay for those compute instances and other associated resources each month, users pay for serverless computing based on the amount of time a function runs in a given billing cycle.

Major serverless computing mechanisms in different cloud venders  are below 

  • AWS Lamda functions 
  • Azure functions 
  • Google cloud functions 
  • IBM cloud functions 


Azure Functions alleviates the need to provision, manage and pay for long-term compute resources in the cloud, which enables organizations to focus more on code development. This makes many of the features in Azure Functions beneficial to software developers.

Azure Functions supports functions developed in C#, F#, Node.js, Python, PHP, batch, bash and any executable file format. Azure Functions also supports the NuGet open source package manager and the Node Package Manager for JavaScript, allowing developers to use popular libraries

Now let's create the function using the azure portal



1. Login to azure portal --- resources --- compute and select function app
















2. Create a new app as below provide the name, subscription details,Os type, and hosting plan also
















3. Now create the app and it may take some time to complete the task















4. Now add the custom code to the app ( i have used custom python code than the .net or node.js)















5. If we click the url which is given in app we can see the below screen
















Create the app using  GIT image 

1. Login to the bash shell and clone the image from GIT as below

unixchipsazure@Azure:~$ git clone https://github.com/Azure-Samples/python-docs-hello-world
Cloning into 'python-docs-hello-world'...
remote: Enumerating objects: 46, done.
remote: Total 46 (delta 0), reused 0 (delta 0), pack-reused 46
Unpacking objects: 100% (46/46), done.
Checking connectivity... done.
unixchipsazure@Azure:~$ cd python-docs-hello-world

2. After cloning we can see below files inside the folder

unixchipsazure@Azure:~/python-docs-hello-world$ ls -lrt
total 16
-rw-r--r-- 1 unixchipsazure unixchipsazure   88 Jul  9 15:22 requirements.txt
-rw-r--r-- 1 unixchipsazure unixchipsazure  831 Jul  9 15:22 README.md
-rw-r--r-- 1 unixchipsazure unixchipsazure 1183 Jul  9 15:22 LICENSE
-rw-r--r-- 1 unixchipsazure unixchipsazure  101 Jul  9 15:22 application.py


3. Now execute the command as below to create the app ( app name is unixchipsapp)

unixchipsazure@Azure:~/python-docs-hello-world$ az webapp up -n unixchipapp
Creating Resource group 'unixchipsazure_rg_Linux_centralus' ...
Resource group creation complete
Creating App service plan 'unixchipsazure_asp_Linux_centralus_0' ...
App service plan creation complete
Creating app 'unixchipapp' ...
Webapp creation complete
Configuring default logging for the app, if not already enabled
Updating app settings to enable build after deployment
Creating zip with contents of dir /home/unixchipsazure/python-docs-hello-world ...
Preparing to deploy and build contents to app.
Getting scm site credentials for zip deployment
Starting zip deployment. This operation can take a while to complete ...
All done.
You can launch the app at http://unixchipapp.azurewebsites.net
{
  "app_url": "http://unixchipapp.azurewebsites.net",
  "appserviceplan": "unixchipsazure_asp_Linux_centralus_0",
  "location": "Central US",
  "name": "unixchipapp",
  "os": "Linux",
  "resourcegroup": "unixchipsazure_rg_Linux_centralus",
  "runtime_version": "python|3.7",
  "sku": "PREMIUMV2",
  "src_path": "/home/unixchipsazure/python-docs-hello-world",
  "version_detected": "-"
}

The above command will create new resource group in centrel US location . If we hit on the app url we will get the output as hello world 
















Create the function using Azure CLI  using http triggered method

We need to install below prerequisites to create an app using az-cli

1. Python 3.6

http://ubuntuhandbook.org/index.php/2017/07/install-python-3-6-1-in-ubuntu-16-04-lts/

2. Azure CLI in ubuntu

https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-apt?view=azure-cli-latest

3. Azure core functions

https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#v2


1. Initiate a function called "unixchipsproject1" as below

unixchips@unixchips:~$ func init unixchipsproject1
Select a worker runtime:
1. dotnet
2. node
3. python (preview)
4. powershell (preview)
Choose option: 3
python
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing /home/unixchips/unixchipsproject1/.vscode/extensions.json
unixchips@unixchips:~$

2. Once the project is initiated edit the host.jason inside the "unixchipsproject1" folder  to update the microsoft extension bundile

unixchips@unixchips:~/unixchipsproject1$ cat host.json
{
    "version": "2.0",
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    }
}

3. Now start the function using the updated extension bundle as below . We will get an output as below

unixchips@unixchips:~/unixchipsproject1$ func host start

                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %

Azure Functions Core Tools (2.7.1373 Commit hash: cd9bfca26f9c7fe06ce245f5bf69bc6486a685dd)
Function Runtime Version: 2.0.12507.0
[10/07/19 4:24:26 PM] Starting Rpc Initialization Service.
[10/07/19 4:24:26 PM] Initializing RpcServer
[10/07/19 4:24:26 PM] Building host: startup suppressed:False, configuration suppressed: False
[10/07/19 4:24:27 PM] Initializing Host.
[10/07/19 4:24:27 PM] Host initialization: ConsecutiveErrors=0, StartupCount=1
[10/07/19 4:24:27 PM] LoggerFilterOptions
[10/07/19 4:24:27 PM] {
[10/07/19 4:24:27 PM]   "MinLevel": "None",
[10/07/19 4:24:27 PM]   "Rules": [
[10/07/19 4:24:27 PM]     {
[10/07/19 4:24:27 PM]       "ProviderName": null,
[10/07/19 4:24:27 PM]       "CategoryName": null,
[10/07/19 4:24:27 PM]       "LogLevel": null,
[10/07/19 4:24:27 PM]       "Filter": "<AddFilter>b__0"
[10/07/19 4:24:27 PM]     },
[10/07/19 4:24:27 PM]     {
[10/07/19 4:24:27 PM]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[10/07/19 4:24:27 PM]       "CategoryName": null,
[10/07/19 4:24:27 PM]       "LogLevel": "None",
[10/07/19 4:24:27 PM]       "Filter": null
[10/07/19 4:24:27 PM]     },
[10/07/19 4:24:27 PM]     {
[10/07/19 4:24:27 PM]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[10/07/19 4:24:27 PM]       "CategoryName": null,
[10/07/19 4:24:27 PM]       "LogLevel": null,
[10/07/19 4:24:27 PM]       "Filter": "<AddFilter>b__0"
[10/07/19 4:24:27 PM]     }
[10/07/19 4:24:27 PM]   ]
[10/07/19 4:24:27 PM] }
------------------< output omitted> ----------------------------------------

4. Once it is completed create a new resource group as below

unixchips@unixchips:~/unixchipsproject1$ az group create --name unixchipsresourse1 --location westeurope

{
  "id": "/subscriptions/1f882f4c-534e-4728-bd04-22cda3654ef0/resourceGroups/unixchipsresourse1",
  "location": "westeurope",
  "managedBy": null,
  "name": "unixchipsresourse1",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": null
}


5. Create a new storage group as below

unixchips@unixchips:~/unixchipsproject1$ az storage account create --name unixchipsstorage3 --location westeurope --resource-group unixchipsresourse1 --sku Standard_LRS


{
  "accessTier": null,
  "creationTime": "2019-07-10T18:16:55.734484+00:00",
  "customDomain": null,
  "enableAzureFilesAadIntegration": null,
  "enableHttpsTrafficOnly": true,
  "encryption": {
    "keySource": "Microsoft.Storage",
    "keyVaultProperties": null,
    "services": {
      "blob": {
        "enabled": true,
        "lastEnabledTime": "2019-07-10T18:16:55.890695+00:00"
      },
      "file": {
        "enabled": true,
        "lastEnabledTime": "2019-07-10T18:16:55.890695+00:00"
      },
      "queue": null,
      "table": null
    }
  },
  "failoverInProgress": null,
  "geoReplicationStats": null,
  "id": "/subscriptions/1f882f4c-534e-4728-bd04-22cda3654ef0/resourceGroups/unixchipsresourse1/providers/Microsoft.Storage/storageAccounts/unixchipsstorage3",
  "identity": null,
  "isHnsEnabled": null,
  "kind": "Storage",
  "lastGeoFailoverTime": null,
  "location": "westeurope",
  "name": "unixchipsstorage3",
  "networkRuleSet": {
    "bypass": "AzureServices",
    "defaultAction": "Allow",
    "ipRules": [],
    "virtualNetworkRules": []
  },
  "primaryEndpoints": {
    "blob": "https://unixchipsstorage3.blob.core.windows.net/",
    "dfs": null,
    "file": "https://unixchipsstorage3.file.core.windows.net/",
    "queue": "https://unixchipsstorage3.queue.core.windows.net/",
    "table": "https://unixchipsstorage3.table.core.windows.net/",
    "web": null
  },
  "primaryLocation": "westeurope",
  "provisioningState": "Succeeded",
  "resourceGroup": "unixchipsresourse1",
  "secondaryEndpoints": null,
  "secondaryLocation": null,
  "sku": {
    "capabilities": null,
    "kind": null,
    "locations": null,
    "name": "Standard_LRS",
    "resourceType": null,
    "restrictions": null,
    "tier": "Standard"
  },
  "statusOfPrimary": "available",
  "statusOfSecondary": null,
  "tags": {},
  "type": "Microsoft.Storage/storageAccounts"
}

6. create a function app as below

unixchips@unixchips:~/unixchipsproject1$ az functionapp create --resource-group unixchipsresourse1 --os-type Linux \
> --consumption-plan-location westeurope  --runtime python \
> --name unixchipsurl --storage-account  unixchipsstorage3

Your Linux function app 'unixchipsurl', that uses a consumption plan has been successfullycreated but is not active until content is published usingAzure Portal or the Functions Core Tools.
Application Insights "unixchipsurl" was created for this Function App. You can visit https://portal.azure.com/#resource/subscriptions/1f882f4c-534e-4728-bd04-22cda3654ef0/resourceGroups/unixchipsresourse1/providers/microsoft.insights/components/unixchipsurl/overview to view your Application Insights component
{
  "availabilityState": "Normal",
  "clientAffinityEnabled": false,
  "clientCertEnabled": false,
  "clientCertExclusionPaths": null,
  "cloningInfo": null,
  "containerSize": 0,
  "dailyMemoryTimeQuota": 0,
  "defaultHostName": "unixchipsurl.azurewebsites.net",
  "enabled": true,
  "enabledHostNames": [
    "unixchipsurl.azurewebsites.net"


**********************output is omitted*******************

7. Once it is completed publish app

func azure functionapp publish unixchipsurl


8 . If we login to the azure portal and check we can see new app is created under unixchipsresourse1 ( resource group)

















9. Click on the url and we can see the default app content


















Thank you for reading ...




No comments:

Post a Comment