A First Look at Azure Functions
Published:
Azure Functions is an event-driven compute platform that manages deploying and maintaining servers and can be used to create serverless API endpoints.
Outline
- Introduction
- Setup Environment
- Create HTTP Trigger Function
- Create an Azure Subscription
- Create a Function App
All of this project’s code can be found in the First Look monorepo on my GitHub.
Introduction
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.
Setup Environment
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.
Initialize a Local Functions Project
A Functions project directory contains host.json
, local.settings.json
, and sub-folders with the code for individual functions.
Output:
Navigate into the newly created project.
Host Metadata
The host.json
metadata file contains global configuration options that affect all functions for a function app.
Local Settings
The local.settings.json
file stores app settings, connection strings, and settings used by local development tools.
Create HTTP Trigger Function
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.
Output:
Alternatively, you can run func new
to see the list of available templates and enter 10
to select HTTP trigger
.
JavaScript Index File
Function Bindings
In function.json
we set req
and res
to the direction
in
and out
. Requests can be get
or post
.
Test Function Locally
To run a Functions project, run the Functions host. The host enables triggers for all functions in the project.
When the Functions host starts, it outputs the URL of HTTP-triggered functions:
If you look back at the terminal you will see the following output.
Follow the instructions from the disembodied voice speaking from the void and enter a name query!
Alternatively, you can test the endpoints with curl.
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.
Select Subscriptions.
Click add.
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.
Check Azure CLI version number with az version
.
Output:
Authenticate with the Azure CLI
Log in to Azure with az login
.
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
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:
- Resource group - logical container for related resources
- Storage account - maintains state and other information about the functions
- Function app - environment for executing function code
Create a Resource Group
The az group create command
creates a resource group. Create a resource group named ajcwebdev-rg
in the westus
region.
Output:
Create a Storage Account
The az storage account create
command creates the storage account.
Standard_LRS
creates a general-purpose storage account in your resource group and region.
Publish Function App
The az functionapp create
command creates the function app in Azure.
We specify ajcwebdev-rg
for the resource group, ajcwebdevstorage
for the storage account, and ajcwebdev-function-app
for the name of the function app. The func azure functionapp publish
command deploys your local functions project to Azure.
Output:
Enter the provided URL or add &name=person
.
You can also view the home page of your function app at ajcwebdev-function-app.azurewebsites.net.