DEV Community

loading...
Cover image for a first look at azure functions

a first look at azure functions

anthony-campolo
full stack web dev
Updated on ・7 min read

Azure Functions is an event-driven serverless compute platform that manages deploying and maintaining servers. It provides all up-to-date resources and orchestration needed to keep your applications running. Developer tools for debugging are included along with triggers and bindings for integrating services.

Outline

  1. Setup environment
    • Install the Azure Functions Core Tools
    • Create a local Functions project with func init
    • host.json
    • local.settings.json
  2. Create an HTTP trigger function with func new
    • index.js
    • function.json
    • Test function locally with func start
  3. Create an Azure subscription
    • Install the Azure CLI
    • Check version number with az version
    • Log in with az login
    • Configure subscription with az account set
  4. Create a function app
    • Create a resource group with az group create
    • Create a storage account with az storage account create
    • Create a function app with az functionapp create
    • Publish app with func azure functionapp publish
    • Test deployed functions with curl

1. Setup environment

The code for this article can be found on my GitHub.

Install the Azure Functions Core Tools

Azure Functions Core Tools includes a version of the same runtime that powers Azure Functions runtime that you can run on your local development computer. It also provides commands to create functions, connect to Azure, and deploy function projects.

brew tap azure/functions
brew install azure-functions-core-tools@3
Enter fullscreen mode Exit fullscreen mode

Create a local Functions project with func init

A Functions project directory contains host.json, local.settings.json, and subfolders with the code for individual functions.

func init ajcwebdev-azure \
  --worker-runtime javascript
Enter fullscreen mode Exit fullscreen mode

Output:

Writing package.json
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing /Users/ajcwebdev/ajcwebdev-azure/.vscode/extensions.json
Enter fullscreen mode Exit fullscreen mode

Navigate into the newly created project.

cd ajcwebdev-azure
Enter fullscreen mode Exit fullscreen mode

host.json

The host.json metadata file contains global configuration options that affect all functions for a function app.

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}
Enter fullscreen mode Exit fullscreen mode

local.settings.json

The local.settings.json file stores app settings, connection strings, and settings used by local development tools.

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsStorage": ""
  }
}
Enter fullscreen mode Exit fullscreen mode

2. Create an HTTP trigger function with func new

HTTP triggers let you invoke a function with an HTTP request. You can use an HTTP trigger to build serverless APIs and respond to webhooks. To create an HTTP trigger function, run func new with the following arguments.

func new \
  --template "Http Trigger" \
  --name HttpTrigger
Enter fullscreen mode Exit fullscreen mode

Output:

Select a number for template: Http Trigger
Function name: [HttpTrigger]

Writing
/Users/ajcwebdev/ajcwebdev-azure/HttpTrigger/index.js

Writing
/Users/ajcwebdev/ajcwebdev-azure/HttpTrigger/function.json

The function "HttpTrigger" was created successfully
from the "Http Trigger" template.
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can run func new to see the list of available templates and enter 10 to select HTTP trigger.

index.js

// index.js

module.exports = async function (context, req) {
  context.log('You did it!')

  const name = (req.query.name || (req.body && req.body.name))
  const responseMessage = name
    ? "Hello, " + name + ". It worked!"
    : "It worked! Pass a name for a personalized response."

  context.res = {
    status: 200,
    body: responseMessage
  }
}
Enter fullscreen mode Exit fullscreen mode

function.json

In function.json we set req and res to the direction in and out. Requests can be get or post.

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Test function locally with func start

To run a Functions project, run the Functions host. The host enables triggers for all functions in the project.

func start
Enter fullscreen mode Exit fullscreen mode

When the Functions host starts, it outputs the URL of HTTP-triggered functions:

Core Tools Version:       3.0.3477
Commit hash:              5fbb9a76fc00e4168f2cc90d6ff0afe5373afc6d  (64-bit)
Function Runtime Version: 3.0.15584.0

Functions:

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

For detailed output, run func with --verbose flag.
Enter fullscreen mode Exit fullscreen mode

01-localhost:7071-api-HttpTrigger

If you look back at the terminal you will see the following output.

Executing 'Functions.HttpTrigger'
(
  Reason='This function was programmatically called via the host APIs.',
  Id=82dff9f9-6973-4275-8cd9-ff95524706b1
)

You did it!

Executed 'Functions.HttpTrigger'
(
  Succeeded,
  Id=82dff9f9-6973-4275-8cd9-ff95524706b1,
  Duration=301ms
)
Enter fullscreen mode Exit fullscreen mode

Follow the instructions from the disembodied voice speaking from the void and enter a name query!

02-localhost:7071-api-HttpTrigger-name-ajcwebdev

Alternatively, you can test the endpoints with curl.

curl --get http://localhost:7071/api/HttpTrigger
Enter fullscreen mode Exit fullscreen mode
curl --request POST http://localhost:7071/api/HttpTrigger \
  --data '{"name":"ajcwebdev"}'
Enter fullscreen mode Exit fullscreen mode

3. Create an Azure subscription

To publish a function you must create a function app with an Azure subscription. A subscription is a container used to provision resources in Azure. It holds the details of all your resources such as VMs and databases.

When you create an Azure resource like a VM, you identify the subscription it belongs to so usage of the VM can be aggregated and billed monthly. You must use the Azure portal to create a subscription.

03-azure-portal

Select Subscriptions.

04-subscriptions

Click add.

05-create-a-subscription

Give your subscription a name.

Install the Azure CLI

There are numerous ways to install the Azure CLI depending on your development environment. I followed the instructions for installing with Homebrew on MacOS.

brew install azure-cli
Enter fullscreen mode Exit fullscreen mode

Check version number with az version

az version
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "azure-cli": "2.27.2",
  "azure-cli-core": "2.27.2",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}
Enter fullscreen mode Exit fullscreen mode

Log in with az login

Log in to Azure with az login.

az login
Enter fullscreen mode Exit fullscreen mode

If you have Multi-Factor Authentication setup then you will need to use az login --tenant TENANT_ID to explicitly login to a tenant. You can find your tenant ID on the Azure Active Directory portal.

Configure subscription with az account set

az account set \
  --subscription ajcwebdev-subscription
Enter fullscreen mode Exit fullscreen mode

4. Create a function app

A function app maps to your local function project and lets you group functions as a logical unit for easier management, deployment, and sharing of resources. Before you can deploy your function code to Azure, you need to create three resources:

Create a resource group with az group create

The az group create command creates a resource group. Create a resource group named ajcwebdev-rg in the westus region.

az group create \
  --name ajcwebdev-rg \
  --location westus
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "id": "/subscriptions/427c04b8-2468-4e47-9537-129b86bc7d3e/resourceGroups/ajcwebdev-rg",
  "location": "westus",
  "managedBy": null,
  "name": "ajcwebdev-rg",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}
Enter fullscreen mode Exit fullscreen mode

Create a storage account with az storage account create

The az storage account create command creates the storage account.

az storage account create \
  --name ajcwebdevstorage \
  --location westus \
  --resource-group ajcwebdev-rg \
  --sku Standard_LRS
Enter fullscreen mode Exit fullscreen mode

Standard_LRS creates a general-purpose storage account in your resource group and region.

Create a function app with az functionapp create

The az functionapp create command creates the function app in Azure.

az functionapp create \
  --resource-group ajcwebdev-rg \
  --consumption-plan-location westus \
  --runtime node \
  --runtime-version 12 \
  --functions-version 3 \
  --name ajcwebdev-function-app \
  --storage-account ajcwebdevstorage
Enter fullscreen mode Exit fullscreen mode

We specify ajcwebdev-rg for the resource group, ajcwebdevstorage for the storage account, and ajcwebdev-function-app for the name of the function app.

Publish app with func azure functionapp publish

The func azure functionapp publish command deploys your local functions project to Azure.

func azure functionapp publish ajcwebdev-function-app
Enter fullscreen mode Exit fullscreen mode

Output:

Getting site publishing info...
Creating archive for current directory...
Uploading 1.63 KB
Upload completed successfully.
Deployment completed successfully.
Syncing triggers...

Functions in ajcwebdev-function-app:
    HttpTrigger - [httpTrigger]
        Invoke url: https://ajcwebdev-function-app.azurewebsites.net/api/httptrigger?code=qFdxLBSxkswsQ/NZIeooMTlC4WS9awDgaGZi/OJPqgUzcKQYFYIwJA==
Enter fullscreen mode Exit fullscreen mode

Enter the provided URL or add &name=person.

06-deployed-HttpTrigger

07-deployed-HttpTrigger-name-person

You can also view the home page of your function app at ajcwebdev-function-app.azurewebsites.net.

08-ajcwebdev-function-app-azurewebsites-net

Discussion (0)