Create Azure Function App projects
Develop Azure function app
Prerequisite
Azure Function App project with TypeScript template
Create a new Azure Function App project with TypeScript template
- Use
func --version
to check the version of Azure Functions Core Tools on your computer. - Create a new TypeScript project with
func
command.$ func init codesanook-azure-function-app --typescript - You can change a project name to what is appropriate for you.
- CD to the project folder.$ cd codesanook-azure-function-app
- Add a new HTTP function to the project.$ func new --name http-example --template "HTTP trigger" --authlevel "anonymous"
- You can change a function name to what is appropriate for you.
Run the project locally
- We can test
http-example
function locally by CD back to the root of the project and run some Node package manage commands. - In this example, we are going to use
Yarn
but you can useNPM
as well. - To use Yarn, we need to update scrips section in
package.json
as the following code."scripts" : {"build": "tsc","build:production": "yarn run prestart","watch": "tsc --w","prestart": "yarn run build && func extensions install","start:host": "func start","start": "npm-run-all --parallel start:host watch"} - Run the following commands to launch the project:$ yarn install$ yarn start
- You should now see some output messages in a terminal showing the application is running at a URL and a port number.
- Copy the URL of http-example function, e.g. http://localhost:7071/api/http-example to a browser address bar and append query string
?name=<YOUR_NAME>
. - Press enter to navigate to that URL and execute the function.
- You should see a message as
Hello <YOUR_NAME>
in a browser.
Azure Function App with C# template
Create a C# Azure Function App with timer trigger
Create a new C# Azure Function App project with
func
command.$ func init TimerFunctionApp --dotnetYou can change a project name to what is appropriate for you.
CD to the project folder.
$ cd TimerFunctionAppAdd a new Timer Trigger function to the project.
$ func new --name TimerExample --template "Timer trigger"To list all available templates for C#, use
func templates list -l c#
.You can change a function name to what is appropriate for you.
Open
TimerExample.cs
file and edit Cron expression to run the function every 5 seconds.using System;using Microsoft.Azure.WebJobs;using Microsoft.Extensions.Logging;namespace TimerFunctionApp{public class TimerExample{[FunctionName("TimerExample")]public void Run([TimerTrigger("*/5 * * * * *")]TimerInfo myTimer, ILogger log){log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");}}}
Start Azurite emulator
- Timer Trigger function requires Blob emulator.
- Start Azurite which is an open-source emulator for testing your Azure Blob, Queue Storage, and Table Storage with the following command:$ docker run \-p 10000:10000 \-p 10001:10001 \-p 10002:10002 \-v azurite-data \mcr.microsoft.com/azure-storage/azurite
Run the function
- Run your function from the root of the project with the following command:$ func start
- You should now see some output messages as
C# Timer trigger function executed at: ...
in your terminal every 5 seconds
Azure Function App with C# Isolated process with Docker Linux
Create a C# Azure Function App with timer trigger
- Create a new C# Azure Function App project with
func
command.$ func init CodesanookAzureFunctionApp --worker-runtime dotnet-isolated --docker - You can change a project name to what is appropriate for you.
- CD to the project folder.$ cd TimerFunctionApp
- Add a new HTTP Trigger function to the project.$ func new --name HttpExample --template "HTTP trigger" --authlevel anonymous
- To list all available templates for C#, use
func templates list -l c#
. - You can change a function name to what is appropriate for you.
- You should now see some output messages in a terminal showing the application is running at a URL and a port number.
- Copy the URL of http-example function, e.g. http://localhost:7071/api/HttpExample to a browser address bar.
- Press enter to navigate to that URL and execute the function.
- You should see a message as
Welcome to Azure Functions
in a browser. - To publish your custom Docker image, please continue with this document.
Credit & Reference
- Quickstart: Create a TypeScript function in Azure from the command line
- Quickstart: Create a C# function in Azure from the command line
- Azure Functions Core Tools reference
- Timer trigger for Azure Functions
- Create a function in the Azure portal that runs on a schedule
- Use the Azurite emulator for local Azure Storage development
- Use dependency injection in .NET Azure Functions
- How to do Azure Functions Logging with DI
- Custom logging concept
- Custom logging implementation
- Why no logging when connect to log stream from a local host
- Create a function on Linux using a custom container
Loading comments...