In this lesson, you will learn how to transition from manual Azure portal configuration to reproducible, automated deployments for your Blazor hosting environments using Bicep.
As your Blazor applications grow—especially when dealing with complex setups like Server-Side Prerendering or WebAssembly AOT—manually clicking through the Azure Portal to configure App Services or Storage Accounts becomes error-prone. Infrastructure as Code (IaC) allows you to treat your hosting environment like source code, ensuring that your production, staging, and development environments are identical.
We will focus on Bicep, a domain-specific language that simplifies Azure Resource Manager (ARM) templates into clean, readable code. Instead of managing complex JSON, a Bicep file describes the "desired state" of your environment.
Defining a Blazor Hosting Environment
If you are deploying a Blazor WebAssembly app, you typically need an Azure Storage Account configured for static website hosting. Here is how you define that infrastructure in a main.bicep file:
resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'blazorappstorage'
location: 'eastus'
sku: { name: 'Standard_LRS' }
kind: 'StorageV2'
properties: {
staticWebsite: {
enabled: true
indexDocument: 'index.html'
}
}
}
When you deploy this file using the Azure CLI (az deployment group create), Azure reconciles your current environment to match this definition. If you change the sku or update the indexDocument in your code, the deployment process handles the modification automatically.
Why this matters for Blazor
By keeping this infrastructure code alongside your Blazor project in Git, you gain two major benefits:
- Consistency: You eliminate the "it works on my machine" scenario for hosting configurations.
- Version Control: You can track infrastructure changes over time, just like you do with your C# code or your component design system. If a new deployment requires a different configuration—such as adjusting App Service Plan tiers for better AOT compilation performance—you commit the change to Bicep, and it is applied consistently across all environments.
Key Takeaway: Infrastructure as Code turns your hosting environment into a version-controlled asset, ensuring your Blazor app's infrastructure is as reliable and reproducible as the code itself.