Watch Aspire live streamsDocsTry Aspire

Set up Azure Functions in the AppHost

Azure Functions logo

This article is the reference for the Aspire Azure Functions Hosting integration. It enumerates the AppHost APIs — with examples for AppHost.cs, apphost.mts, and apphost.go — that you use to model an Azure Functions project resource in your AppHost project.

If you are new to the Azure Functions integration, start with the Get started with the Azure Functions integration guide. For how your Functions code reads the connection information this page sets up, see Azure Functions runtime configuration.

The Aspire Azure Functions integration has the following project constraints for .NET Functions projects:

To start building an Aspire app that uses Azure Functions, install the 📦 Aspire.Hosting.Azure.Functions NuGet package:

Terminal
aspire add azure-functions

Learn more about aspire add in the command reference.

This updates your aspire.config.json with the Azure Functions hosting integration package:

aspire.config.json
{
"packages": {
"Aspire.Hosting.Azure.Functions": "17.0.0"
}
}

The Azure Functions hosting integration models Azure Functions projects as the AzureFunctionsProjectResource type. Call AddAzureFunctionsProjectaddAzureFunctionsProjectAddAzureFunctionsProject on the builder instance to add an Azure Functions project resource to the app model.

There are two ways to add an Azure Functions project in C#, depending on whether the Functions project is referenced by the AppHost project. The TypeScript AppHost always uses the file path approach.

Add a referenced Functions project (C# only)

Section titled “Add a referenced Functions project (C# only)”

If the Azure Functions project is referenced in your C# AppHost project, use the generic overload of AddAzureFunctionsProject:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var functions = builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions")
.WithExternalHttpEndpoints();
builder.AddDotnetProject("exampleproject", "../ExampleProject/ExampleProject.csproj")
.WithReference(functions)
.WaitFor(functions);
// After adding all resources, run the app...
builder.Build().Run();

If the Azure Functions project is not referenced in your AppHost project (or you are using a TypeScript AppHost), specify the path to the project file:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const functions = await builder.addAzureFunctionsProject(
"functions",
"../MyFunctions/MyFunctions.csproj"
);
await functions.withExternalHttpEndpoints();
await builder.addNodeApp("api", "./api", "index.js")
.withReference(functions)
.waitFor(functions);
// After adding all resources, run the app...
  1. The path to the Functions project file is relative to the AppHost project directory. If the path is not absolute, it is resolved relative to the AppHost directory.

  2. The functions resource can be referenced by other project resources. The WithReferencewithReferenceWithReference method configures a connection named "functions" in the consuming project.

  3. If the Functions project exposes an HTTP trigger and you call WithExternalHttpEndpointswithExternalHttpEndpointsWithExternalHttpEndpoints, the trigger endpoint is publicly accessible when deployed.

Add Azure Functions resource with host storage

Section titled “Add Azure Functions resource with host storage”

Azure Functions requires an Azure Storage account as host storage for checkpoints, leases, and other internal state. By default, Aspire creates an implicit host storage resource automatically. To use a specific storage account, call WithHostStorage(IResourceBuilder<AzureStorageResource> storage)withHostStorage(storage: AzureStorageResource)WithHostStorage on the Azure Functions resource:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const storage = await builder.addAzureStorage("storage");
await storage.runAsEmulator();
const functions = await builder.addAzureFunctionsProject(
"functions",
"../MyFunctions/MyFunctions.csproj"
);
await functions.withHostStorage(storage);
// After adding all resources, run the app...

The preceding code relies on the 📦 Aspire.Hosting.Azure.Storage NuGet package to add an Azure Storage resource running as an emulator. The storage resource is passed to WithHostStorage, setting the host storage to the emulated account.

To connect Azure Functions triggers and bindings to other Azure resources, chain WithReference (or withReference) on the Azure Functions project resource:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const storage = await builder.addAzureStorage("storage");
await storage.runAsEmulator();
const blobs = await storage.addBlobs("blobs");
const functions = await builder.addAzureFunctionsProject(
"functions",
"../MyFunctions/MyFunctions.csproj"
);
await functions.withHostStorage(storage);
await functions.withReference(blobs);
// After adding all resources, run the app...

The connection information required to connect to the blobs resource is automatically injected into the Azure Functions project, enabling the project to define a BlobTrigger that uses the blobs resource. For how your function code reads the injected connection information, see Azure Functions runtime configuration.

To make HTTP triggers publicly accessible when deployed, call WithExternalHttpEndpoints (or withExternalHttpEndpoints) on the Azure Functions resource:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const functions = await builder.addAzureFunctionsProject(
"functions",
"../MyFunctions/MyFunctions.csproj"
);
await functions.withExternalHttpEndpoints();
// After adding all resources, run the app...

Use AddAzureFunctionsApp (or addAzureFunctionsApp) to model a TypeScript or JavaScript Azure Functions app that does not have a .NET project file. Directory-based apps run on the Azure Functions Node language worker — Aspire sets FUNCTIONS_WORKER_RUNTIME to node for you:

apphost.mts
import { AzureFunctionsLanguage, createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const storage = await builder.addAzureStorage("storage");
await storage.runAsEmulator();
const blob = await storage.addBlobs("blob");
const functions = await builder.addAzureFunctionsApp(
"functions",
"../functions",
AzureFunctionsLanguage.TypeScript
);
await functions.withReference(blob);
// After adding all resources, run the app...
  1. The path to the Functions app directory is relative to the AppHost project directory.

  2. AzureFunctionsLanguage has two values: TypeScript and JavaScript. In Go, use AzureFunctionsLanguageTypeScript or AzureFunctionsLanguageJavaScript. Both run on the Node language worker.

  3. TypeScript apps are launched through npm run start -- --port <port>, so the standard Azure Functions Core Tools-generated prestart script can build the app before the host starts. The start script should resolve Azure Functions Core Tools from the app’s local npm dependencies, for example by depending on the azure-functions-core-tools package in devDependencies. JavaScript apps are launched directly with func host start --port <port> and require func to be available on PATH.

  4. As with .NET Functions projects, Aspire provisions an implicit Azure Storage account for host storage by default. Call WithHostStorage (or withHostStorage) to use a specific storage account instead — see Add Azure Functions resource with host storage.

A directory-based Functions app’s package.json should include scripts similar to the standard Azure Functions TypeScript template:

package.json
{
"scripts": {
"build": "tsc",
"prestart": "npm run build",
"start": "func start"
},
"devDependencies": {
"azure-functions-core-tools": "^4.0.0",
"typescript": "^5.0.0"
}
}

Debug TypeScript and JavaScript Functions apps

Section titled “Debug TypeScript and JavaScript Functions apps”

Use the Aspire extension for VS Code to start your AppHost with debugging. Set breakpoints in your JavaScript or TypeScript source and invoke the function to stop at those breakpoints. Aspire attaches the VS Code JavaScript debugger to the Functions Node worker.

For TypeScript, enable source maps and build your app before invoking the function. By default, the debugger looks for compiled JavaScript under dist/**/*.js in the Functions app directory. If your build uses another directory, set outFiles under debuggers.azure-functions-node in your Aspire launch configuration. For example, add the following property to the type: "aspire" configuration in .vscode/launch.json, adjusting the path for your app:

{
"debuggers": {
"azure-functions-node": {
"outFiles": ["${workspaceFolder}/functions/build/**/*.js"]
}
}
}

The Aspire.Hosting.Azure.Functions package includes support for Azure Durable Task Scheduler, which provides a managed backend for Durable Functions orchestrations. Use AddDurableTaskScheduleraddDurableTaskSchedulerAddDurableTaskScheduler to add a scheduler resource and AddTaskHubaddTaskHubAddTaskHub to associate a task hub with it.

Call AddDurableTaskScheduler on the builder instance to add a scheduler resource, then call AddTaskHub to create a task hub and pass its reference to the Azure Functions project:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const storage = await builder.addAzureStorage("storage");
await storage.runAsEmulator();
const scheduler = await builder.addDurableTaskScheduler("scheduler");
await scheduler.runAsEmulator();
const taskHub = await scheduler.addTaskHub("taskhub");
const functions = await builder.addAzureFunctionsProject(
"functions",
"../MyFunctions/MyFunctions.csproj"
);
await functions.withHostStorage(storage);
await functions.withReference(taskHub);
// After adding all resources, run the app...

In the preceding example:

  • AddDurableTaskScheduler (or addDurableTaskScheduler) adds a DurableTaskSchedulerResource to the app model.
  • RunAsEmulatorrunAsEmulatorRunAsEmulator configures the scheduler to use the local Durable Task emulator for development and testing.
  • AddTaskHub (or addTaskHub) creates a DurableTaskHubResource associated with the scheduler.
  • WithReference(taskHub) (or withReference(taskHub)) injects the task hub connection information into the Azure Functions project.

When using RunAsEmulator(), Aspire starts a local container running the DTS emulator and automatically provides:

  • A Scheduler Dashboard URL on the scheduler resource.
  • A Task Hub Dashboard URL on each task hub resource.
  • A DTS_TASK_HUB_NAMES environment variable on the emulator container listing the associated task hub names.

Durable Task Scheduler emulator image

Tags reflect the latest defaults on the microsoft/aspire main branch, and may be newer than the version pinned by the package you install.

To connect to an already-deployed Durable Task Scheduler instance instead of provisioning a new one, use RunAsExistingrunAsExistingRunAsExisting. The overload accepts either a plain string connection string or an IResourceBuilder<ParameterResource> for securely supplying the value at runtime:

AppHost.cs
#pragma warning disable ASPIREDURABLETASK001
var builder = DistributedApplication.CreateBuilder(args);
var connectionString = builder.AddParameter("dts-connection-string", secret: true);
var scheduler = builder.AddDurableTaskScheduler("scheduler")
.RunAsExisting(connectionString);
var taskHubNameParam = builder.AddParameter("taskhub-name");
var taskHub = scheduler.AddTaskHub("taskhub").WithTaskHubName(taskHubNameParam);
builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions")
.WithReference(taskHub);
builder.Build().Run();
#pragma warning restore ASPIREDURABLETASK001

Deployment to Azure Container Apps (ACA) is supported using the SDK container publish function in Microsoft.Azure.Functions.Worker.Sdk. When deploying to Azure Container Apps, KEDA-based auto-scaling rules are automatically configured for your functions.

For more information, see Azure Functions and Aspire integration.

Deploying TypeScript and JavaScript Functions apps

Section titled “Deploying TypeScript and JavaScript Functions apps”

When a directory-based Functions app doesn’t already contain a Dockerfile, aspire publish generates one that uses the official Azure Functions Node image, copies the app to /home/site/wwwroot, installs npm dependencies, and (for TypeScript apps) runs npm run build. After the build step, Aspire runs npm prune --omit=dev so the deployed container keeps only production dependencies.

If the Functions app directory already contains a Dockerfile, Aspire uses it as-is instead of generating one — use this to select a different Azure Functions Node image, pin a Node version, or add project-specific build steps.

When deployed to Azure Container Apps, directory-based Functions resources are published with the container app kind set to functionapp and the container’s HTTP port set to 80, matching the shape of the Azure Functions Node host image.

If your subscription restricts storage network access, configure the Container Apps environment and storage with private networking. Some policies also require disabling anonymous blob access. See the Azure Virtual Network integration and customizing Azure resources.

For the full reference of how consuming apps and Functions code read the Aspire-injected connection information, see Azure Functions runtime configuration.