Post

How to Create Azure Functions with Azure Function Core Tools and the Azure CLI

Azure Functions are great for small pieces of code your want to run in Azure where you don’t need to worry about any maintenance due it running on a serverless architecture.

It can be quite affordable when using a consumption plan given that you are paying per execution of your Azure Function and for the resource consumption by the second.

Your Azure Function can be triggered as a result of something happening to another Azure resource you manage like a new file going into a Storage Account and a new message on the Service Bus Queue.

This content is available in video form on the Cloud Engineer Skills YouTube channel.

How to Create an Azure Function

In this tutorial, we will cover, the end to end process of:

  • Coding your Azure Function
  • Testing it locally using Azure Function Core Tools
  • Using the Azure CLI to create the Azure resources required to deploy the Azure Function
  • Deploying your Azure Function to Azure
  • Verifying the Azure Function running in Azure

Install Azure Function Core Tools

You can test if you have Azure Function Core Tools installed by running the func command.

If it is not installed you can install it from the azure-functions-core-tools npm package using the following command.

1
npm i -g azure-functions-core-tools@4 --unsafe-perm true

Give it a try by running the func command.

Initializing a Node.js Azure Functions Project

Create a new directory you would like to initiate an Azure Functions project in.

Initialize the Azure Functions project with a func init command.

1
func init

From there you can enter the following inputs to create an Azure Functions project using Node.js.

  • Select node
  • Select javascript
  • Wait for npm install to complete

You will see that a few files and directories have been created:

Directories:

  • /src/functions/: a directory for where new Azure Functions will be created

Files:

  • .funcignore: files that will not get deployed to Azure when you publish your Azure Function
  • host.json: configuration options that affect all functions in a function app instance (see host.json reference for Azure Functions 2.x and later)
  • local.settings.json: stores app settings and connection strings when the Azure Function is running locally (see Local settings file)

Create an Azure Function

You can create an Azure Function and add it to your project with the func new command.

1
func new

From there you can enter the following inputs to create an Azure Function.

  • Select HTTP Trigger
  • Enter a name for the Azure Function

Testing an Azure Function Locally

You can test out the Azure Function locally with the func start command.

1
func start

This will run the Azure Function on your local machine and output a URL you can copy and paste in a browser to verify it is working as expected.

The logs will be output to the console as you interact with the URL in the browser.

You could also make HTTP GET or POST requests to this URL using code or a tool like Postman.

Creating the Azure Resources using the Azure CLI

You will need to create the following Azure Resources before you can deploy your Azure Function.

  1. Azure Resource Group
  2. Azure Storage Account
  3. Azure Function App
1
2
3
az group create -g cloudengineerskills-af-rg -l eastus
az storage account create -n cesstorageaccount123 -g cloudengineerskills-af-rg -l eastus --sku Standard_LRS
az functionapp create -g cloudengineerskills-af-rg --consumption-plan-location eastus -n cloudengineerskillsfuncapp -s cesstorageaccount123 --runtime node --os-type Linux --functions-version 4

Deploying your Azure Functions to the Azure Function App

Now from within the directory containing your Azure Functions project you can deploy your Azure Functions to Azure using the func azure functionapp publish command.

1
func azure functionapp publish cloudengineerskillsfuncapp

Trying out your Azure Function running in Azure

From here you can get the URL of the Azure Function and copy and paste it in a browser and test it works.

1
az functionapp function list -g cloudengineerskills-af-rg -n cloudengineerskillsfuncapp --query "[].{name: name, url: invokeUrlTemplate}"

Clean up the resources

IMPORTANT! az functionapp create creates the Application Insights Log Analytics workspace into it’s own Resource Group with the prefix DefaultResourceGroup-, make sure you also delete this as it is no longer required.

Clean up the remaining Azure resources you have created in this tutorial by deleting the Azure Resource Group.

1
az group delete --name cloudengineerskills-af-rg

Further Reading

This post is licensed under CC BY 4.0 by the author.