Azure Developer CLI (azd) は、インフラストラクチャ生成と呼ばれる強力な機能を提供します。これにより、Aspire アプリケーションの基になるインフラストラクチャ コードを生成およびカスタマイズできます。 この機能は、 Azure リソース、セキュリティ構成、デプロイ パターンをきめ細かく制御する必要がある運用シナリオに不可欠です。
この記事では、 azd infra gen を使用して次の操作を行う方法について説明します。
- Aspire アプリ モデルから Bicep インフラストラクチャ ファイルを生成します。
- 生成されたインフラストラクチャを運用環境の要件に合わせてカスタマイズします。
- 生成されたリソースにセキュリティのベスト プラクティスを適用します。
- 適切なバージョン管理を使用して、コードとしてのインフラストラクチャを管理します。
[前提条件]
Aspireを使用するには、次のものがローカルにインストールされている必要があります。
-
.NET 8.0 または .NET 9.0。
- Aspire 9.4 以降では、.NET 10 Preview 5 以降がサポートされています。
- OCI 準拠のコンテナー ランタイム。次に例を示します。
- Docker デスクトップ または Podman. 詳細については、「コンテナー ランタイム」を参照してください。
- 次のような統合開発者環境 (IDE) またはコード エディター。
- Visual Studio 2022 バージョン 17.9 以降 (任意)
-
Visual Studio Code (任意)
- C# Dev Kit: 拡張 (オプション)
- JetBrains Rider とプラグインAspire(省略可能)
詳細については、Aspireセットアップとツール、および SDK のAspireに関する説明を参照してください。
Azure Developer CLIをローカルにインストールする必要もあります。
インフラストラクチャの生成のしくみ
azdでのインフラストラクチャの生成により、Bicep テンプレートを使用して、Aspire アプリ モデルが具象Azureインフラストラクチャ定義に変換されます。 このプロセスは、 Aspire の開発時オーケストレーションと、 Azureで必要な運用インフラストラクチャの間のギャップを埋めます。
azd infra genを実行すると、CLI は次のようになります。
- Aspire AppHost プロジェクトを分析します。
- すべてのリソースとその依存関係を識別します。
- Bicep で対応する Azure リソース定義を生成します。
- 展開用のサポート構成ファイルを作成します。
インフラ生成技術を活用する
Aspire ソリューションで generate infrastructure コマンドを呼び出します。
azd infra gen
このコマンドにより、AppHost プロジェクト ディレクトリに次の構造の infra フォルダーが作成されます。
└───📂 infra
├─── abbreviations.json # Azure resource naming conventions
├─── main.bicep # Main infrastructure entry point
├─── main.parameters.json # Parameter values for deployment
└─── resources.bicep # Resource definitions
実稼働に関する考慮事項
生成されたインフラストラクチャはデプロイのための強固な基盤を提供しますが、運用環境では、セキュリティ、スケーラビリティ、保守容易性のための追加の構成が必要です。 このセクションでは、運用環境のデプロイを準備するときにカスタマイズする必要がある重要な領域について説明します。
セキュリティ構成
運用環境のデプロイの準備をする場合は、適切なセキュリティ制御を使用して、生成されたインフラストラクチャを確認して強化します。
ネットワークの分離:
// Example: Configure Container Apps Environment with network restrictions
resource containerAppsEnvironment 'Microsoft.App/managedEnvironments@2023-05-01' = {
name: environmentName
location: location
properties: {
vnetConfiguration: {
infrastructureSubnetId: subnetId
internal: true
}
workloadProfiles: [
{
name: 'Consumption'
workloadProfileType: 'Consumption'
}
]
}
}
ID とアクセスの管理:
// Example: Configure managed identity with least privilege access
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: identityName
location: location
}
// Assign specific roles rather than broad permissions
resource acrPullRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: containerRegistry
name: guid(containerRegistry.id, managedIdentity.id, 'AcrPull')
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '7f951dda-4ed3-4680-a7ca-43fe172d538d') // AcrPull
principalId: managedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
リソースのサイズ設定とスケーリング
生成されたリソース構成で運用環境の要件を確認します。
// Example: Configure appropriate resource limits
resource containerApp 'Microsoft.App/containerApps@2023-05-01' = {
name: appName
location: location
properties: {
configuration: {
ingress: {
external: true
targetPort: 8080
allowInsecure: false // Ensure HTTPS only
}
}
template: {
containers: [
{
name: containerName
image: image
resources: {
cpu: json('1.0') // Adjust based on load requirements
memory: '2.0Gi' // Adjust based on memory needs
}
}
]
scale: {
minReplicas: 2 // Ensure availability
maxReplicas: 10 // Control costs
rules: [
{
name: 'http-requests'
http: {
metadata: {
concurrentRequests: '100'
}
}
}
]
}
}
}
}
環境固有の構成
パラメーターを使用して、環境固有の設定を管理します。
@description('Environment name (dev, staging, prod)')
param environmentType string = 'dev'
@description('Application tier configuration')
var tierConfigurations = {
dev: {
skuName: 'Consumption'
replicas: 1
}
staging: {
skuName: 'Dedicated'
replicas: 2
}
prod: {
skuName: 'Dedicated'
replicas: 3
}
}
var currentTier = tierConfigurations[environmentType]
反復的なカスタマイズ ワークフロー
初期インフラストラクチャを生成した後、継続的なカスタマイズのためのワークフローを確立します。
- 生成された Bicep ファイルにインフラストラクチャを変更します。
- 開発環境でのデプロイをテストします。
- インフラストラクチャの変更をバージョン管理する。
- チーム コラボレーションのドキュメントのカスタマイズ。
Important
azd infra genをもう一度実行すると、ファイルが再生成され、カスタマイズが上書きされる可能性があります。 常に変更をバージョン管理し、再生成後にカスタマイズを再適用する準備をします。
高度なカスタマイズ パターン
カスタム リソース定義
追加の Azure リソースを使用して、生成されたインフラストラクチャを拡張します。
// Add Application Insights for monitoring
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
name: '${resourceBaseName}-ai'
location: location
kind: 'web'
properties: {
Application_Type: 'web'
Flow_Type: 'Redfield'
Request_Source: 'IbizaWebAppExtensionCreate'
WorkspaceResourceId: logAnalyticsWorkspace.id
}
}
// Configure container apps to use Application Insights
resource containerApp 'Microsoft.App/containerApps@2023-05-01' = {
// ... other properties
properties: {
template: {
containers: [
{
// ... other container properties
env: [
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
value: applicationInsights.properties.ConnectionString
}
]
}
]
}
}
}
インフラストラクチャの検証
適切なリソース構成を確保するための検証規則を追加します。
@description('Validates that environment type is supported')
@allowed(['dev', 'staging', 'prod'])
param environmentType string
@description('Validates location is in allowed regions')
@allowed(['eastus', 'westus2', 'northeurope'])
param location string
ベスト プラクティス
- バージョン管理: 生成されたインフラストラクチャ ファイルを常にソース管理にコミットします。
- 環境の分離: 異なる環境に対して個別のリソース グループと名前付け規則を使用します。
- セキュリティ スキャン: Bicep テンプレートの自動セキュリティ スキャンを実装します。
- コストの監視: コスト追跡用の予算アラートとリソース タグを設定します。
- ドキュメント: カスタマイズのドキュメントとその根拠を維持します。
次のステップ
Aspire