Azure Functions Core Tool Installation on Ubuntu

Azure Functions Core tool.

Azure Functions Core tool supports developing and testing functions on a local computer from the command prompt or terminal. This locally developed function can deploy to an Azure account or subscription. It is a command-line tool scaffolding to create many common types of function and in multiple programming languages. It is cross-platform and runs on Windows, Mac, and Linux.

 

Here, we will install the Azure Function Core tool on Ubuntu 18.04.

Azure Functions Core tool installation on Ubuntu 18.04

Login to the Ubuntu machine,  download the Azure package repository and install it. Complete steps and commands are given below.

wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb 

sudo apt-get update 

sudo apt-get install apt-transport-https -y

sudo apt-get install dotnet-sdk-2.1 -y

sudo apt-get install azure-functions-core-tools -y

The dotnet-sdk-2.1 package installs all the binaries and files required for the .NET software to work and the azure-function-core package is for Azure Function App.

Once installed, execute the below command to check the version.

$ dotnet --version
2.1.804

$ func
Azure Functions Core Tools (2.7.2184 Commit hash: 5afacc827c2848e4debc2cc7)
Function Runtime Version: 2.0.12961.0

Create and Run the Function locally

We have installed all the required package to create and run a Function.  Next, create a sample Function App, for this create a directory called “myfunc” and enter it.

mkdir myfunc

$cd myfunc/

To initiate the Function execute “func init” command and you can see the option as follows.

Next, execute “func new” to create a new Function. It will ask for you to select a template, I choose HTTPTrigger based and given a name called “MyHttpFunc”

To see the code present in the file use cat command.

$ cat MyHttpFunc.cs
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace myfunc
{
    public static class MyHttpFunc
    {
        [FunctionName("MyHttpFunc")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string name = req.Query["name"];
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;
            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }
}

You can make the changes with the help of an editor (vi or vim). Once done we can start the function with the help of “func start” command.

Once the function started successfully you will get the function URL at the end.

Open another terminal and access the above URL with curl command and don’t forget to pass the parameter.

$ curl http://localhost:7071/api/MyHttpFunc?name=techies

Hello, techies

The log started printing in the previous terminal.

 

Deploy the Functions to Azure Cloud

I have modified the existing code as follows (Added only edited part of the code as I have already pasted complete code.)

string name = req.Query["location"];
             string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
             dynamic data = JsonConvert.DeserializeObject(requestBody);
             name = name ?? data?.name;
             return name != null                 ? (ActionResult)new OkObjectResult($"place, {location}")

To test the Function again execute “func start” command. But this time we will deploy this code on the Azure Function app. Please note we can deploy a Function code only to an existing Azure Function App.

To create the Function App from the Azure portal follow the below article.

Create a Function App from the portal

We have to create only the Function that doesn’t require creating a New Function because we are publishing existing code from our local machine. Added a screenshot of my Function App for reference.

 

Before publishing the code to Azure Function App, first, we should log in to the Azure cloud. To login to execute the below command.

az login

If you are new to the az command, I recommend you to read the below article.

Azure az installation and configuration

Once logged in to the account set the subscription where you are Function App is running. Here my Function App is running Devops-test” subscription, so I set it as my account.

 az account set -s "Devops-test"

Now, we are good to deploy our code, enter to your project directory and execute the below command to publish your code.

$ func azure functionapp publish Devopsfunctest

You're trying to publish to a v1 function app from v2 tooling.
You can pass --force to force update the app to v2, or downgrade to v1 tooling for publishing

I got the above error so as they suggested I executed it with –force parameter.

func azure functionapp publish Devopsfunctest --force

 

Once the deployment completed, login to the Function App overview dashboard and copy the Function App URL, we can access this directly on the web browser or from the Powershell.

Please note that we have to add parameters at the end of the URL to get the output. Added a complete URL for the reference.

https://devopsfunctest.azurewebsites.net/api/MyHttpFunc?code=EkUDqh/GMR6Ai1gS0EDbsohWltflBr/VfCfYPbMHWWHvyXg==&location=India

 

We have completed the Azure Function core tool installation on the local Ubuntu machine and deployed it in the cloud Function App.

 

Leave a Reply

Your email address will not be published. Required fields are marked *