Write and publish an HTTP function
In this task you'll create and deploy an HTTP-triggered function.
The quickest first function is an HTTP trigger: it runs whenever someone opens its web address. You write it on your own computer or in Cloud Shell with Azure Functions Core Tools (the func command), then publish it to the function app.
Each HTTP function has an authorisation level. Anonymous means anyone can call it; Function means callers must add a key from the portal.
Example. Larkfield Group's intranet calls a small HTTP function, holidays, that returns the office's bank holidays as JSON. It needed fifteen lines of code and no server.
Before you start
You need: Contributor on the function app. Time: 15 minutes.
Where to look
- Function App
- func-practice
- Functions
Steps
- Open Cloud Shell in Bash and make a project folder:Type
mkdir hello-func && cd hello-func, thenfunc init --worker-runtime node --language javascript - Add an HTTP function called hello:Type
func new --template "HTTP trigger" --name hello. Read the file it creates undersrc/functions: the template setsauthLevel: 'anonymous', so anyone can call it without a key. - Publish it to your function app:Type
func azure functionapp publish <function app name>. It prints the function's address when it's done. - Open the address, adding
?name=SS Kaiserat the end. You'll see Hello, SS Kaiser!If you changeauthLevelto'function'and publish again, the plain address returns 401. Copy the address with Get function URL in the portal, which includes the key. - In the portal, the app's Overview shows Flex Consumption as the plan type and hello in its Functions list. Select hello and look at Code + Test, Invocations and Function Keys.Invocations shows recent runs once Application Insights is turned on.
Commands
# Make a JavaScript project with one HTTP function
mkdir hello-func && cd hello-func
func init --worker-runtime node --language javascript
func new --template "HTTP trigger" --name hello
# Publish it to an existing function app
func azure functionapp publish <function app name>
# List the functions in the app
az functionapp function list --resource-group rg-portal-practice --name <function app name> --output table
Watch out for
- Cost
- A few test runs are well inside the free allowance.
- Security
- Use the Function authorisation level for anything that isn't meant to be public, and never put keys or passwords in the code: use app settings, Key Vault and managed identities.
- Common mistakes
- Running
funccommands outside the project folder. Publishing to the wrong app name. Forgetting the key for a Function-level HTTP trigger and getting a 401 error.
Clean up
- Resource groups
- rg-portal-practice
- Delete resource group
Remove the practice folder with cd ~ && rm -r hello-func. You've finished the functions section: delete the resource group to remove the function app and its storage account.
Words to know
- Azure Functions Core Tools
- The
funccommand, for creating, running and publishing functions. It's already in Cloud Shell. - Authorisation level
- Who can call an HTTP function: anonymous, or only callers with a function key.
- Function key
- A secret code added to the address (as ?code=...) to call a protected function.
Tick off each step and track your progress in the guide.
Open this task in Azure Portal, Hands On