|
| 1 | +--- |
| 2 | +title: Use rule-based routing in Azure Container Apps (preview) |
| 3 | +description: Learn how to use rule-based routing in Azure Container Apps. |
| 4 | +services: container-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: azure-container-apps |
| 7 | +ms.custom: devx-track-azurecli, devx-track-bicep |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 03/26/2025 |
| 10 | +ms.author: cshoe |
| 11 | +zone_pivot_groups: azure-cli-bicep |
| 12 | +--- |
| 13 | + |
| 14 | +# Use rule-based routing with Azure Container Apps (preview) |
| 15 | + |
| 16 | +In this article, you learn how to use rule-based routing with Azure Container Apps. With rule-based routing, you create a fully qualified domain name (FQDN) on your container apps environment. You then use rules to route requests to this FQDN to different container apps, depending on the path of each request. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- Azure account with an active subscription. |
| 21 | + - If you don't have one, you [can create one for free](https://azure.microsoft.com/free/). |
| 22 | + |
| 23 | +- Install the [Azure CLI](/cli/azure/install-azure-cli). |
| 24 | + |
| 25 | +::: zone pivot="bicep" |
| 26 | + |
| 27 | +- [Bicep](/azure/azure-resource-manager/bicep/install) |
| 28 | + |
| 29 | +::: zone-end |
| 30 | + |
| 31 | +## Setup |
| 32 | + |
| 33 | +To sign in to Azure from the CLI, run the following command and follow the prompts to complete the authentication process. |
| 34 | + |
| 35 | +```azurecli |
| 36 | +az login |
| 37 | +``` |
| 38 | + |
| 39 | +To ensure you're running the latest version of the CLI, run the upgrade command. |
| 40 | + |
| 41 | +```azurecli |
| 42 | +az upgrade |
| 43 | +``` |
| 44 | + |
| 45 | +Ignore any warnings about modules currently in use. |
| 46 | + |
| 47 | +Next, install or update the Azure Container Apps extension for the CLI. |
| 48 | + |
| 49 | +If you receive errors about missing parameters when you run `az containerapp` commands in Azure CLI or cmdlets from the `Az.App` module in PowerShell, be sure you have the latest version of the Azure Container Apps extension installed. |
| 50 | + |
| 51 | +```azurecli |
| 52 | +az extension add --name containerapp --upgrade |
| 53 | +``` |
| 54 | + |
| 55 | +> [!NOTE] |
| 56 | +> Starting in May 2024, Azure CLI extensions no longer enable preview features by default. To access Container Apps [preview features](whats-new.md), install the Container Apps extension with `--allow-preview true`. |
| 57 | +> ```azurecli |
| 58 | +> az extension add --name containerapp --upgrade --allow-preview true |
| 59 | +> ``` |
| 60 | +
|
| 61 | +Now that the current extension or module is installed, register the `Microsoft.App` and `Microsoft.OperationalInsights` namespaces. |
| 62 | +
|
| 63 | +```azurecli |
| 64 | +az provider register --namespace Microsoft.App |
| 65 | +``` |
| 66 | +
|
| 67 | +```azurecli |
| 68 | +az provider register --namespace Microsoft.OperationalInsights |
| 69 | +``` |
| 70 | + |
| 71 | +::: zone pivot="azure-cli" |
| 72 | + |
| 73 | +## Create environment variables |
| 74 | + |
| 75 | +Create the following environment variables. |
| 76 | + |
| 77 | +```bash |
| 78 | +$CONTAINER_APP_1_NAME="my-container-app-1" |
| 79 | +$CONTAINER_APP_1_IMAGE="mcr.microsoft.com/k8se/quickstart:latest" |
| 80 | +$CONTAINER_APP_1_TARGET_PORT="80" |
| 81 | +$CONTAINER_APP_2_NAME="my-container-app-2" |
| 82 | +$CONTAINER_APP_2_IMAGE="mcr.microsoft.com/dotnet/samples:aspnetapp" |
| 83 | +$CONTAINER_APP_2_TARGET_PORT="8080" |
| 84 | +$LOCATION="eastus" |
| 85 | +$RESOURCE_GROUP="my-container-apps" |
| 86 | +$ENVIRONMENT_NAME="my-container-apps-env" |
| 87 | +$ROUTE_CONFIG_NAME="my-route-config" |
| 88 | +``` |
| 89 | + |
| 90 | +## Create container apps |
| 91 | + |
| 92 | +1. Run the following command to create your first container app. This container app uses the Container Apps quickstart image. |
| 93 | + |
| 94 | + ```azurecli |
| 95 | + az containerapp up \ |
| 96 | + --name $CONTAINER_APP_1_NAME \ |
| 97 | + --resource-group $RESOURCE_GROUP \ |
| 98 | + --location $LOCATION \ |
| 99 | + --environment $ENVIRONMENT_NAME \ |
| 100 | + --image $CONTAINER_APP_1_IMAGE \ |
| 101 | + --target-port $CONTAINER_APP_1_TARGET_PORT \ |
| 102 | + --ingress external \ |
| 103 | + --query properties.configuration.ingress.fqdn |
| 104 | + ``` |
| 105 | +
|
| 106 | +1. Run the following command to create your second container app. This container app uses the ASP.NET quickstart image. |
| 107 | +
|
| 108 | + ```azurecli |
| 109 | + az containerapp up \ |
| 110 | + --name $CONTAINER_APP_2_NAME \ |
| 111 | + --resource-group $RESOURCE_GROUP \ |
| 112 | + --location $LOCATION \ |
| 113 | + --environment $ENVIRONMENT_NAME \ |
| 114 | + --image $CONTAINER_APP_2_IMAGE \ |
| 115 | + --target-port $CONTAINER_APP_2_TARGET_PORT \ |
| 116 | + --ingress external \ |
| 117 | + --query properties.configuration.ingress.fqdn |
| 118 | + ``` |
| 119 | +
|
| 120 | +## Create HTTP route configuration |
| 121 | +
|
| 122 | +1. Create the following YAML file and save it as `routing.yml`. |
| 123 | +
|
| 124 | + ```yaml |
| 125 | + rules: |
| 126 | + - description: App 1 rule |
| 127 | + routes: |
| 128 | + - match: |
| 129 | + prefix: /app1 |
| 130 | + action: |
| 131 | + prefixRewrite: / |
| 132 | + targets: |
| 133 | + - containerApp: my-container-app-1 |
| 134 | + - description: App 2 rule |
| 135 | + routes: |
| 136 | + - match: |
| 137 | + path: /app2 |
| 138 | + action: |
| 139 | + prefixRewrite: / |
| 140 | + targets: |
| 141 | + - containerApp: my-container-app-2 |
| 142 | + ``` |
| 143 | +
|
| 144 | +1. Run the following command to create the HTTP route configuration. |
| 145 | +
|
| 146 | + ```azurecli |
| 147 | + az containerapp env http-route-config create \ |
| 148 | + --http-route-config-name $ROUTE_CONFIG_NAME \ |
| 149 | + --resource-group $RESOURCE_GROUP \ |
| 150 | + --name $ENVIRONMENT_NAME \ |
| 151 | + --yaml routing.yml \ |
| 152 | + --query properties.fqdn |
| 153 | + ``` |
| 154 | +
|
| 155 | + Your HTTP route configuration's fully qualified domain name (FQDN) looks like this example: `my-route-config.ambitiouspebble-11ba6155.eastus.azurecontainerapps.io` |
| 156 | +
|
| 157 | +::: zone-end |
| 158 | +
|
| 159 | +::: zone pivot="bicep" |
| 160 | +
|
| 161 | +1. Create the following Bicep file and save it as `routing.bicep`. |
| 162 | +
|
| 163 | + ```bicep |
| 164 | + resource containerAppsEnvironment 'Microsoft.App/managedEnvironments@2024-10-02-preview' = { |
| 165 | + name: 'my-container-apps-env' |
| 166 | + location: 'eastus' |
| 167 | + tags: {} |
| 168 | + properties: { |
| 169 | + workloadProfiles: [ |
| 170 | + { |
| 171 | + workloadProfileType: 'Consumption' |
| 172 | + name: 'Consumption' |
| 173 | + } |
| 174 | + ] |
| 175 | + } |
| 176 | + } |
| 177 | +
|
| 178 | + resource httpRouteConfig 'Microsoft.App/managedEnvironments/httpRouteConfigs@2024-10-02-preview' = { |
| 179 | + parent: containerAppsEnvironment |
| 180 | + name: 'my-route-config' |
| 181 | + location: 'eastus' |
| 182 | + properties: { |
| 183 | + rules: [ |
| 184 | + { |
| 185 | + description: 'App 1 rule' |
| 186 | + routes: [ |
| 187 | + { |
| 188 | + match: { |
| 189 | + prefix: '/app1' |
| 190 | + } |
| 191 | + action: { |
| 192 | + prefixRewrite: '/' |
| 193 | + } |
| 194 | + } |
| 195 | + ] |
| 196 | + targets: [ |
| 197 | + { |
| 198 | + containerApp: 'my-container-app-1' |
| 199 | + } |
| 200 | + ] |
| 201 | + } |
| 202 | + { |
| 203 | + description: 'App 2 rule' |
| 204 | + routes: [ |
| 205 | + { |
| 206 | + match: { |
| 207 | + path: '/app2' |
| 208 | + } |
| 209 | + action: { |
| 210 | + prefixRewrite: '/' |
| 211 | + } |
| 212 | + } |
| 213 | + ] |
| 214 | + targets: [ |
| 215 | + { |
| 216 | + containerApp: 'my-container-app-2' |
| 217 | + } |
| 218 | + ] |
| 219 | + } |
| 220 | + ] |
| 221 | + } |
| 222 | + } |
| 223 | +
|
| 224 | + output fqdn string = httpRouteConfig.properties.fqdn |
| 225 | + ``` |
| 226 | +
|
| 227 | +1. Deploy the Bicep file with the following command: |
| 228 | +
|
| 229 | + ```azurecli |
| 230 | + az deployment group create ` |
| 231 | + --name $ROUTE_CONFIG_NAME ` |
| 232 | + --resource-group $RESOURCE_GROUP ` |
| 233 | + --template-file routing.bicep |
| 234 | + ``` |
| 235 | +
|
| 236 | +1. In the output, find `outputs`, which contains your HTTP route configuration's fully qualified domain name (FQDN). For example: |
| 237 | +
|
| 238 | + ``` |
| 239 | + "outputs": { |
| 240 | + "fqdn": { |
| 241 | + "type": "String", |
| 242 | + "value": "my-route-config.ambitiouspebble-11ba6155.eastus.azurecontainerapps.io" |
| 243 | + } |
| 244 | + }, |
| 245 | + ``` |
| 246 | +
|
| 247 | +::: zone-end |
| 248 | +
|
| 249 | +## Test HTTP route configuration |
| 250 | +
|
| 251 | +1. Browse to your HTTP route configuration FQDN with the path `/app1`. For example: `my-route-config.ambitiouspebble-11ba6155.eastus.azurecontainerapps.io/app1`. You see the Container Apps quickstart image. |
| 252 | +
|
| 253 | +1. Browse to your HTTP route configuration FQDN with the path `/app2`. For example: `my-route-config.ambitiouspebble-11ba6155.eastus.azurecontainerapps.io/app2`. You see the ASP.NET quickstart image. |
| 254 | +
|
| 255 | +## Clean up resources |
| 256 | +
|
| 257 | +If you're not going to continue to use this application, run the following command to delete the resource group along with all the resources created in this quickstart. |
| 258 | +
|
| 259 | +> [!CAUTION] |
| 260 | +> The following command deletes the specified resource group and all resources contained within it. If resources outside the scope of this quickstart exist in the specified resource group, they will also be deleted. |
| 261 | +
|
| 262 | +```azurecli |
| 263 | +az group delete --name my-container-apps |
| 264 | +``` |
| 265 | + |
| 266 | +> [!TIP] |
| 267 | +> Having issues? Let us know on GitHub by opening an issue in the [Azure Container Apps repo](https://github.com/microsoft/azure-container-apps). |
| 268 | +
|
| 269 | +## Related content |
| 270 | + |
| 271 | +- [Azure CLI reference](/cli/azure/containerapp/env/http-route-config) |
| 272 | +- [Bicep reference](/azure/templates/microsoft.app/2024-10-02-preview/managedenvironments/httprouteconfigs?pivots=deployment-language-bicep) |
| 273 | +- [ARM template reference](/azure/templates/microsoft.app/2024-10-02-preview/managedenvironments/httprouteconfigs?pivots=deployment-language-arm-template) |
0 commit comments