9 Deploying Aspire apps to production
Aspire applications are built to run smoothly during development, but production requires a few important adjustments before deployment. The chapter emphasizes planning for scale, swapping in environment-appropriate services, and preparing operational support such as database migration handling and dashboard extensions. These changes help ensure that the same application can behave correctly both locally and after release.
One major theme is making the application deployment-ready by using Aspire’s built-in orchestration features more deliberately. Services can be scaled out by running multiple replicas, with load balancing handled behind the scenes, and local substitutes can be used for cloud-only dependencies so developers can work with realistic behavior on a machine. The chapter also shows how to separate local and deployed code paths, automatically apply database migrations on startup, seed data safely, and extend the dashboard with custom commands such as clearing a cache.
The chapter then walks through turning an Aspire app into deployable infrastructure. It explains how to generate a manifest that describes the application, then use that output with Azure Container Apps, or convert it into Terraform or Kubernetes-friendly files for broader platform support. Finally, it covers CI/CD automation with GitHub Actions and Azure DevOps, showing how to restore, build, test, provision, and deploy automatically, while also handling environment variables, registry access, and Kubernetes deployment steps in a repeatable pipeline.
Hosting multiple instances of a service in Aspire
Looking at the internal HTTP ports of individual instances
Accessing a custom command from the Aspire dashboard
Publishing Aspire apps from Visual Studio
Selecting Azure Container Apps as a deployment target
Summary
- Aspire can scale out individual services by running multiple instances of them. You can have separate logic for the local Aspire instance and the deployed application.
- The Aspire dashboard can be extended with additional custom commands.
- When multiple instances of a service are hosted in Aspire, load balancing is handled automatically.
- Aspire can be integrated with a variety of other tools for deployment to various cloud environments, such as the Azure Developer CLI.
- JSON files known as Aspire manifests describe all individual services hosted by Aspire.
- Aspire can be deployed in Azure Container Apps via the Azure Developer CLI. If we want to deploy it to a Kubernetes cluster in a cloud-agnostic way, we can convert Aspire manifests to YAML files that Kubernetes can understand by using a tool called Aspir8.
FAQ
How do you scale out a service in an Aspire application?
Aspire supports scaling out a service by running multiple replicas. You configure the number of instances in the App Host project usingWithReplicas(), and Aspire uses load balancing to route requests across those replicas.Why might you modify an Aspire app before deploying it to production?
Predeployment changes can make production easier to manage. Common reasons include running multiple replicas for higher throughput, swapping in local substitutes for cloud-only services, and preparing automated database migrations or custom dashboard commands.How does Aspire handle environment-specific services like Azure SQL?
Aspire can use different service definitions depending on whether the app is running locally or being deployed. For example, you can use standard containerized SQL Server in run mode and Azure SQL in publish mode by checkingbuilder.ExecutionContext.IsRunMode or IsPublishMode.What is the purpose of database migrations in an Aspire deployment?
Database migrations let you update a database schema safely over time. Aspire can be configured to apply new migrations automatically during deployment so schema changes are introduced in order and without downtime.How can you automatically apply migrations when an Aspire app starts in production?
You can add a background service, such as aBackgroundService, that resolves the database context, runs MigrateAsync(), and then optionally seeds initial data. This service runs when the app starts, including after redeployment.Why should database creation and seeding be removed from Program.cs when using migration startup logic?
If the background service is responsible for applying migrations and seeding data, keeping database creation or seeding logic in Program.cs can conflict with that process. Removing the duplicate logic avoids startup conflicts and inconsistent initialization.How can you add a custom command to the Aspire dashboard?
You can create an extension method for the resource, define a command withWithCommand(), and specify options such as an icon and enabled state. In the chapter’s example, a Redis cache command was added to clear the cache from the dashboard.What command generates an Aspire manifest for deployment?
Rundotnet run --publisher manifest --output-path manifest.json from the Aspire host project folder. This creates a JSON manifest that describes the application and its resources for deployment.How do you deploy an Aspire app to Azure Container Apps using AZD?
First install the Azure Developer CLI, then runazd init, azd login, and finally azd up. You can also use Visual Studio’s Publish workflow, which still relies on AZD behind the scenes.How can Aspire deployment files be converted for Terraform or Kubernetes?
For Terraform, you can useAspire.Terraform with aspire-tf init and aspire-tf generate. For Kubernetes, you can use Aspir8 with aspirate init and aspirate generate, then deploy the generated YAML with kubectl apply -k ..
Building Distributed Applications that Work ebook for free