跳转到内容
Docs Try Aspire
Docs Try

Local Azure provisioning

此内容尚不支持你的语言。

Azure logo

This article describes how to enable local Azure provisioning in your Aspire AppHost project. Local Azure provisioning lets Aspire automatically create and configure Azure resources in your Azure subscription during local development — no manual portal or CLI setup required.

When you add an Azure resource using methods like AddAzureStorage() or AddAzureServiceBus(), Aspire:

  1. Detects that the resource doesn’t exist locally
  2. Generates Bicep infrastructure-as-code
  3. Uses the configured Azure credential and Azure APIs to deploy resources
  4. Configures connection information automatically
  5. Injects configuration into your application

Before using local provisioning, ensure you have:

  • Azure subscription: An active Azure subscription
  • Azure credentials: A supported local credential source such as Azure CLI (az login), Visual Studio, Visual Studio Code, or Azure Developer CLI
  • Appropriate permissions: Contributor access to create resources
  • Aspire 9.0+: Local provisioning requires Aspire 9.0 or later

Local provisioning requires configuring your Azure subscription and location. You can do this in multiple ways:

The recommended approach for development is using the Aspire CLI:

Terminal window
aspire secret set "Azure:SubscriptionId" "your-subscription-id"
aspire secret set "Azure:Location" "eastus"

Alternatively, configure in appsettings.Development.json:

{
"Azure": {
"SubscriptionId": "your-subscription-id",
"Location": "eastus"
}
}

You can also use environment variables:

Terminal window
export Azure__SubscriptionId="your-subscription-id"
export Azure__Location="eastus"

The following configuration options are available:

SettingDescriptionDefault
Azure:SubscriptionIdYour Azure subscription ID(required)
Azure:LocationAzure region for resources(required)
Azure:TenantIdAzure tenant to use when selecting subscriptions and credentialsCurrent credential tenant
Azure:ResourceGroupSpecific resource group to create or reuseAuto-generated
Azure:ResourceGroupPrefixPrefix used when Aspire generates a resource group namerg-aspire
Azure:CredentialSourceAuthentication method for local provisioningDefault
Azure:AllowResourceGroupCreationAllow creating resource groupstrue
Azure:CredentialProcessTimeoutSecondsTimeout in seconds for credential subprocess operations (must be between 5 and 600)Azure SDK default

By default, Aspire creates a resource group for your provisioned resources named rg-aspire-{apphost-name}-{random-suffix}. If you set Azure:ResourceGroupPrefix, Aspire uses that prefix instead of rg-aspire.

To use an existing resource group:

{
"Azure": {
"SubscriptionId": "your-subscription-id",
"Location": "eastus",
"ResourceGroup": "my-existing-rg"
}
}

To prevent Aspire from creating resource groups:

{
"Azure": {
"SubscriptionId": "your-subscription-id",
"Location": "eastus",
"AllowResourceGroupCreation": false,
"ResourceGroup": "my-existing-rg"
}
}

Add Azure resources to your AppHost using the relevant Add* APIs, then reference them from your consuming projects:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage");
var blobs = storage.AddBlobs("blobs");
builder.AddProject<Projects.WebApp>("webapp")
.WithReference(blobs);
// After adding all resources, run the app...

Once your AppHost is configured, run the application and Aspire automatically provisions the referenced Azure resources:

  1. Configure your Azure subscription (if not already done)

    Terminal window
    aspire secret set "Azure:SubscriptionId" "your-sub-id"
    aspire secret set "Azure:Location" "eastus"
  2. Run your application

    Terminal window
    aspire run
  3. Automatic provisioning

    Aspire detects the Azure resources and provisions them automatically. You’ll see output like:

    [Azure Provisioning] Creating resource group: rg-myapp
    [Azure Provisioning] Deploying storage account: stmyapp
    [Azure Provisioning] Deployment complete

When Azure:CredentialSource isn’t set, local provisioning uses a development-focused DefaultAzureCredential chain. That default supports local credentials such as Azure CLI, Visual Studio, Visual Studio Code, and Azure Developer CLI, while excluding Azure-hosted credentials such as managed identity and workload identity.

If you want to force a specific local credential, set Azure:CredentialSource to one of the supported values:

ValueUses
DefaultDevelopment credential chain for local provisioning
AzureCliAzure CLI sign-in (az login)
AzureDeveloperCliAzure Developer CLI sign-in (azd auth login)
VisualStudioVisual Studio Azure account
VisualStudioCodeVisual Studio Code Azure account
AzurePowerShellAzure PowerShell sign-in
InteractiveBrowserBrowser-based sign-in prompt

Most local credential sources (Azure CLI, Azure PowerShell, Visual Studio, and Azure Developer CLI) acquire tokens by invoking a subprocess. On machines where these tools are slow to respond — for example, due to antivirus scanning or network latency — credential validation can time out before authentication completes.

Use Azure:CredentialProcessTimeoutSeconds to override the credential subprocess timeout. The value must be between 5 and 600 seconds. When unset, the Azure SDK’s default timeout is used.

appsettings.Development.json
{
"Azure": {
"CredentialProcessTimeoutSeconds": 120
}
}

This setting applies to the AzureCli, AzurePowerShell, VisualStudio, and AzureDeveloperCli credential sources, and to the development credential chain used when CredentialSource isn’t explicitly set. It has no effect on InteractiveBrowser or ManagedIdentity credentials, which don’t shell out to a subprocess.

Aspire generates unique resource names following Azure naming conventions:

  • Storage accounts: st{appname}{hash} (lowercase, no hyphens)
  • Key Vaults: kv-{appname}-{hash} (hyphens allowed)
  • Service Bus: sb-{appname}-{hash}
  • Cosmos DB: cosmos-{appname}-{hash}

The hash ensures uniqueness across subscriptions.

When you use local provisioning, Aspire generates Bicep files in the ./infra directory of your AppHost project. These files:

  • Define all Azure resources
  • Include configurations and connections
  • Can be customized using ConfigureInfrastructure()
  • Are used for both local provisioning and production deployment

Example generated structure:

AppHost/
└── infra/ # Bicep files
├── main.bicep
├── storage.bicep
└── servicebus.bicep
Program.cs

Local provisioning has some limitations:

  • First-run delays: Initial provisioning takes several minutes
  • Subscription limits: Subject to Azure subscription quotas
  • Network requirements: Requires internet connectivity
  • Cost considerations: Provisioned resources incur Azure charges

If you see authentication errors:

Terminal window
az login
az account set --subscription "your-subscription-id"

Ensure your account has Contributor role:

Terminal window
az role assignment list --assignee your-email@domain.com

If resource names conflict, delete the existing resources or change your app name:

Terminal window
az group delete --name rg-myapp

Enable detailed logging:

{
"Logging": {
"LogLevel": {
"Aspire.Hosting.Azure": "13.3.0"
}
}
}
AspectLocal ProvisioningManual Provisioning
Setup timeAutomaticManual Azure Portal/CLI
ConfigurationAutomaticManual connection strings
Infrastructure as CodeAuto-generated BicepManual Bicep/ARM
Development speedFastSlower
Learning curveLowerHigher
ControlGood (via ConfigureInfrastructure)Full
CostPay for provisioned resourcesPay for provisioned resources