# AzureKubernetesEnvironmentExtensions Methods

- Package: [Aspire.Hosting.Azure.Kubernetes](/reference/api/csharp/aspire.hosting.azure.kubernetes.md)
- Type: [AzureKubernetesEnvironmentExtensions](/reference/api/csharp/aspire.hosting.azure.kubernetes/azurekubernetesenvironmentextensions.md)
- Kind: `Methods`
- Members: `7`

Provides extension methods for adding Azure Kubernetes Service (AKS) environments to the application model.

## AddAzureKubernetesEnvironment(IDistributedApplicationBuilder, string)

- Name: `AddAzureKubernetesEnvironment(IDistributedApplicationBuilder, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureKubernetesEnvironmentResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentExtensions.cs#L52-L132)

Adds an Azure Kubernetes Service (AKS) environment to the distributed application. This provisions an AKS cluster and configures it as a Kubernetes compute environment.

```csharp
public static class AzureKubernetesEnvironmentExtensions
{
    public static IResourceBuilder<AzureKubernetesEnvironmentResource> AddAzureKubernetesEnvironment(
        this IDistributedApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the AKS environment resource.

## Returns

`IResourceBuilder<AzureKubernetesEnvironmentResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This method internally creates a Kubernetes environment for Helm-based deployment and provisions an AKS cluster via Azure Bicep. It combines the functionality of `AddKubernetesEnvironment` with Azure-specific provisioning.

## Examples

```csharp
var aks = builder.AddAzureKubernetesEnvironment("aks");
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddNodePool(IResourceBuilder<AzureKubernetesEnvironmentResource>, string, string, int, int)

- Name: `AddNodePool(IResourceBuilder<AzureKubernetesEnvironmentResource>, string, string, int, int)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AksNodePoolResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentExtensions.cs#L167-L185)

Adds a node pool to the AKS cluster.

```csharp
public static class AzureKubernetesEnvironmentExtensions
{
    public static IResourceBuilder<AksNodePoolResource> AddNodePool(
        this IResourceBuilder<AzureKubernetesEnvironmentResource> builder,
        string name,
        string vmSize = "Standard_D2s_v5",
        int minCount = 1,
        int maxCount = 3)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureKubernetesEnvironmentResource>`)
  The AKS environment resource builder.
- `name` (`string`)
  The name of the node pool.
- `vmSize` (`string`) `optional`
  The VM size for nodes. Defaults to `Standard_D2s_v5` if not specified.
- `minCount` (`int`) `optional`
  The minimum node count for autoscaling. Defaults to 1.
- `maxCount` (`int`) `optional`
  The maximum node count for autoscaling. Defaults to 3.

## Returns

`IResourceBuilder<AksNodePoolResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for the new node pool.

## Remarks

The returned node pool resource can be passed to `KubernetesEnvironmentExtensions.WithNodePool` on compute resources to schedule workloads on this pool.

## Examples

```csharp
var aks = builder.AddAzureKubernetesEnvironment("aks");

// With defaults (Standard_D2s_v5, 1-3 nodes)
var pool = aks.AddNodePool("workload");

// With explicit VM size and scaling
var gpuPool = aks.AddNodePool("gpu", "Standard_NC6s_v3", 0, 5);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithContainerRegistry(IResourceBuilder<AzureKubernetesEnvironmentResource>, IResourceBuilder<AzureContainerRegistryResource>)

- Name: `WithContainerRegistry(IResourceBuilder<AzureKubernetesEnvironmentResource>, IResourceBuilder<AzureContainerRegistryResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureKubernetesEnvironmentResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentExtensions.cs#L316-L349)

Configures the AKS environment to use a specific Azure Container Registry for image storage. When set, this replaces the auto-created default container registry.

```csharp
public static class AzureKubernetesEnvironmentExtensions
{
    public static IResourceBuilder<AzureKubernetesEnvironmentResource> WithContainerRegistry(
        this IResourceBuilder<AzureKubernetesEnvironmentResource> builder,
        IResourceBuilder<AzureContainerRegistryResource> registry)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureKubernetesEnvironmentResource>`)
  The AKS environment resource builder.
- `registry` (`IResourceBuilder<AzureContainerRegistryResource>`)
  The Azure Container Registry resource builder.

## Returns

`IResourceBuilder<AzureKubernetesEnvironmentResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Remarks

If not called, a default Azure Container Registry is automatically created. The registry endpoint is flowed to the inner Kubernetes environment so that Helm deployments can push and pull images.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithSubnet(IResourceBuilder<AzureKubernetesEnvironmentResource>, IResourceBuilder<AzureSubnetResource>)

- Name: `WithSubnet(IResourceBuilder<AzureKubernetesEnvironmentResource>, IResourceBuilder<AzureSubnetResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureKubernetesEnvironmentResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentExtensions.cs)

Configures the AKS cluster to use a VNet subnet for node pool networking. Unlike `AzureVirtualNetworkExtensions.WithDelegatedSubnet`, this does NOT add a service delegation to the subnet -- AKS uses plain (non-delegated) subnets.

```csharp
public static class AzureKubernetesEnvironmentExtensions
{
    public static IResourceBuilder<AzureKubernetesEnvironmentResource> WithSubnet(
        this IResourceBuilder<AzureKubernetesEnvironmentResource> builder,
        IResourceBuilder<AzureSubnetResource> subnet)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureKubernetesEnvironmentResource>`)
  The AKS environment resource builder.
- `subnet` (`IResourceBuilder<AzureSubnetResource>`)
  The subnet to use for AKS node pools.

## Returns

`IResourceBuilder<AzureKubernetesEnvironmentResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Examples

```csharp
var vnet = builder.AddAzureVirtualNetwork("vnet", "10.0.0.0/16");
var subnet = vnet.AddSubnet("aks-subnet", "10.0.0.0/22");
var aks = builder.AddAzureKubernetesEnvironment("aks")
    .WithSubnet(subnet);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithSubnet(IResourceBuilder<AksNodePoolResource>, IResourceBuilder<AzureSubnetResource>)

- Name: `WithSubnet(IResourceBuilder<AksNodePoolResource>, IResourceBuilder<AzureSubnetResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AksNodePoolResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentExtensions.cs)

Configures a specific AKS node pool to use its own VNet subnet. When applied, this node pool's subnet overrides the environment-level subnet set via [AzureKubernetesEnvironmentExtensions.WithSubnet(IResourceBuilder<AzureKubernetesEnvironmentResource>, IResourceBuilder<AzureSubnetResource>)](/reference/api/csharp/aspire.hosting.azure.kubernetes/azurekubernetesenvironmentextensions/methods.md#withsubnet-iresourcebuilder-azurekubernetesenvironmentresource-iresourcebuilder-azuresubnetresource).

```csharp
public static class AzureKubernetesEnvironmentExtensions
{
    public static IResourceBuilder<AksNodePoolResource> WithSubnet(
        this IResourceBuilder<AksNodePoolResource> builder,
        IResourceBuilder<AzureSubnetResource> subnet)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AksNodePoolResource>`)
  The node pool resource builder.
- `subnet` (`IResourceBuilder<AzureSubnetResource>`)
  The subnet to use for this node pool.

## Returns

`IResourceBuilder<AksNodePoolResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Examples

```csharp
var vnet = builder.AddAzureVirtualNetwork("vnet", "10.0.0.0/16");
var defaultSubnet = vnet.AddSubnet("default", "10.0.0.0/22");
var gpuSubnet = vnet.AddSubnet("gpu-subnet", "10.0.4.0/24");

var aks = builder.AddAzureKubernetesEnvironment("aks")
    .WithSubnet(defaultSubnet);

var gpuPool = aks.AddNodePool("gpu", AksNodeVmSizes.StandardNCSv3.StandardNC6sV3, 0, 5)
    .WithSubnet(gpuSubnet);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithSystemNodePool(IResourceBuilder<AzureKubernetesEnvironmentResource>, string, int, int)

- Name: `WithSystemNodePool(IResourceBuilder<AzureKubernetesEnvironmentResource>, string, int, int)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureKubernetesEnvironmentResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentExtensions.cs#L219-L229)

Replaces the default system node pool with a customized configuration.

```csharp
public static class AzureKubernetesEnvironmentExtensions
{
    public static IResourceBuilder<AzureKubernetesEnvironmentResource> WithSystemNodePool(
        this IResourceBuilder<AzureKubernetesEnvironmentResource> builder,
        string vmSize = "Standard_D2s_v5",
        int minCount = 1,
        int maxCount = 3)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureKubernetesEnvironmentResource>`)
  The AKS environment resource builder.
- `vmSize` (`string`) `optional`
  The VM size for system pool nodes. Defaults to `Standard_D2s_v5` if not specified.
- `minCount` (`int`) `optional`
  The minimum node count for autoscaling. Defaults to 1.
- `maxCount` (`int`) `optional`
  The maximum node count for autoscaling. Defaults to 3.

## Returns

`IResourceBuilder<AzureKubernetesEnvironmentResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Remarks

Every AKS cluster requires exactly one system node pool for hosting system pods. By default, the system pool uses `Standard_D2s_v5`. Use this method to change the VM size when the default SKU is not available in your subscription or region. Calling this method multiple times replaces the previous system pool configuration.

## Examples

```csharp
var aks = builder.AddAzureKubernetesEnvironment("aks")
    .WithSystemNodePool("Standard_B2s");

// With explicit scaling
var aks2 = builder.AddAzureKubernetesEnvironment("aks2")
    .WithSystemNodePool("Standard_B2s", minCount: 2, maxCount: 5);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithWorkloadIdentity(IResourceBuilder<AzureKubernetesEnvironmentResource>, bool)

- Name: `WithWorkloadIdentity(IResourceBuilder<AzureKubernetesEnvironmentResource>, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureKubernetesEnvironmentResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentExtensions.cs#L369-L373)

Enables or disables workload identity on the AKS environment, allowing pods to authenticate to Azure services using federated credentials.

```csharp
public static class AzureKubernetesEnvironmentExtensions
{
    public static IResourceBuilder<AzureKubernetesEnvironmentResource> WithWorkloadIdentity(
        this IResourceBuilder<AzureKubernetesEnvironmentResource> builder,
        bool enabled = true)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureKubernetesEnvironmentResource>`)
  The resource builder.
- `enabled` (`bool`) `optional`
  `true` to enable workload identity (the default); `false` to disable it.

## Returns

`IResourceBuilder<AzureKubernetesEnvironmentResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Remarks

This ensures the AKS cluster is configured with OIDC issuer and workload identity enabled. Workload identity is automatically wired when compute resources have an `Azure.AppIdentityAnnotation`, which is added by `WithAzureUserAssignedIdentity` or auto-created by `AzureResourcePreparer`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
