自定义 AspireAzure 部署

Azure Developer CLI (azd) 提供了一项称为基础结构生成的强大功能,可用于为Aspire应用程序生成和自定义底层基础结构代码。 此功能对于需要对资源、安全配置和部署模式进行精细控制 Azure 的生产方案至关重要。

本文介绍如何使用 azd infra gen

  • 从 Aspire 应用模型生成 Bicep 基础结构文件。
  • 自定义生成的基础结构以满足生产要求。
  • 将安全最佳做法应用于生成的资源。
  • 通过正确的版本控制来管理基础设施即代码。

先决条件

若要使用 Aspire,需要在本地安装以下各项:

有关详细信息,请参阅 Aspire 设置和工具以及 Aspire SDK

你还需要在本地安装

基础结构生成的工作原理

使用 Bicep 模板的azd基础结构生成将您的应用模型转换为具体的Aspire基础结构定义Azure。 此过程在 Aspire 的开发时编排与 Azure 所需的生产基础设施之间架起桥梁。

运行 azd infra gen时,CLI:

  1. 对您的 Aspire AppHost 项目进行分析。
  2. 标识所有资源及其依赖项。
  3. 在 Bicep 中生成相应的 Azure 资源定义。
  4. 创建用于部署的支持配置文件。

使用基础结构生成

在解决方案中调用 Aspire 生成基础架构的命令:

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'
      }
    ]
  }
}

标识和访问管理:

// 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]

迭代自定义工作流

生成初始基础结构后,为正在进行的自定义建立工作流:

  1. 对生成的 Bicep 文件进行基础结构更改
  2. 在开发环境中进行测试部署
  3. 版本控制 您的基础设施变更。
  4. 团队协作的文档自定义

重要

再次运行 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 模板的自动安全扫描。
  • 成本监视:设置用于成本跟踪的预算警报和资源标记。
  • 文档:维护定制项文档及其理由说明。

后续步骤