|
| 1 | +--- |
| 2 | +title: 'Tutorial: Communication between microservices in Azure Container Apps' |
| 3 | +description: Learn how to communicate between microservices deployed in Azure Container Apps |
| 4 | +services: container-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: container-apps |
| 7 | +ms.topic: tutorial |
| 8 | +ms.date: 05/13/2022 |
| 9 | +ms.author: cshoe |
| 10 | +zone_pivot_groups: container-apps-image-build-type |
| 11 | +--- |
| 12 | + |
| 13 | +# Tutorial: Communication between microservices in Azure Container Apps Preview |
| 14 | + |
| 15 | +Azure Container Apps exposes each container app through a domain name if [ingress](ingress.md) is enabled. Ingress endpoints for container apps within an external environment can be either publicly accessible or only available to other container apps in the same [environment](environment.md). |
| 16 | + |
| 17 | +Once you know the fully qualified domain name for a given container app, you can make direct calls to the service from other container apps within the shared environment. |
| 18 | + |
| 19 | +In this tutorial, you deploy a second container app that makes a direct service call to the API deployed in the [Deploy your code to Azure Container Apps](./quickstart-code-to-cloud.md) quickstart. |
| 20 | + |
| 21 | +The following screenshot shows the UI microservice deploys to container apps at the end of this article. |
| 22 | + |
| 23 | +:::image type="content" source="media/communicate-between-microservices/azure-container-apps-album-ui.png" alt-text="Screenshot of album list UI microservice."::: |
| 24 | + |
| 25 | +In this tutorial, you learn to: |
| 26 | + |
| 27 | +> [!div class="checklist"] |
| 28 | +> * Deploy a front end application to Azure Container Apps |
| 29 | +> * Link the front end app to the API endpoint deployed in the previous quickstart |
| 30 | +> * Verify the frontend app can communicate with the back end API |
| 31 | +
|
| 32 | +## Prerequisites |
| 33 | + |
| 34 | +In the [code to cloud quickstart](./quickstart-code-to-cloud.md), a back end web API is deployed to return a list of music albums. If you haven't deployed the album API microservice, return to [Quickstart: Deploy your code to Azure Container Apps](quickstart-code-to-cloud.md) to continue. |
| 35 | + |
| 36 | +## Setup |
| 37 | + |
| 38 | +If you're still authenticated to Azure and still have the environment variables defined from the quickstart, you can skip the following steps and go directly to the [Prepare the GitHub repository](#prepare-the-github-repository) section. |
| 39 | + |
| 40 | +[!INCLUDE [container-apps-code-to-cloud-setup.md](../../includes/container-apps-code-to-cloud-setup.md)] |
| 41 | + |
| 42 | +Sign in to the Azure CLI. |
| 43 | + |
| 44 | +# [Bash](#tab/bash) |
| 45 | + |
| 46 | +```azurecli |
| 47 | +az login |
| 48 | +``` |
| 49 | + |
| 50 | +# [PowerShell](#tab/powershell) |
| 51 | + |
| 52 | +```powershell |
| 53 | +az login |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +::: zone pivot="docker-local" |
| 59 | + |
| 60 | +# [Bash](#tab/bash) |
| 61 | + |
| 62 | +```azurecli |
| 63 | +az acr login --name $ACR_NAME |
| 64 | +``` |
| 65 | + |
| 66 | +# [PowerShell](#tab/powershell) |
| 67 | + |
| 68 | +```powershell |
| 69 | +az acr login --name $ACR_NAME |
| 70 | +``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +::: zone-end |
| 75 | + |
| 76 | +## Prepare the GitHub repository |
| 77 | + |
| 78 | +1. In a new browser tab, navigate to the [repository for the UI application](https://github.com/azure-samples/containerapps-albumui) and select the **Fork** button at the top of the page to fork the repo to your account. |
| 79 | + |
| 80 | + Follow the prompts from GitHub to fork the repository and return here once the operation is complete. |
| 81 | + |
| 82 | +1. Navigate to the parent of the *code-to-cloud* folder. If you're still in the *code-to-cloud/src* directory, you can use the below command to return to the parent folder. |
| 83 | + |
| 84 | + ```console |
| 85 | + cd ../.. |
| 86 | + ``` |
| 87 | + |
| 88 | +1. Use the following git command to clone your forked repo into the *code-to-cloud-ui* folder: |
| 89 | + |
| 90 | + ```git |
| 91 | + git clone https://github.com/$GITHUB_USERNAME/containerapps-albumui.git code-to-cloud-ui |
| 92 | + ``` |
| 93 | + |
| 94 | + > [!NOTE] |
| 95 | + > If the `clone` command fails, check that you have successfully forked the repository. |
| 96 | + |
| 97 | +1. Next, change the directory into the *src* folder of the cloned repo. |
| 98 | + |
| 99 | + ```console |
| 100 | + cd code-to-cloud-ui/src |
| 101 | + ``` |
| 102 | + |
| 103 | +## Build the front end application |
| 104 | + |
| 105 | +::: zone pivot="acr-remote" |
| 106 | + |
| 107 | +# [Bash](#tab/bash) |
| 108 | + |
| 109 | +```azurecli |
| 110 | +az acr build --registry $ACR_NAME --image albumapp-ui . |
| 111 | +``` |
| 112 | + |
| 113 | +# [PowerShell](#tab/powershell) |
| 114 | + |
| 115 | +```powershell |
| 116 | +az acr build --registry $ACR_NAME --image albumapp-ui . |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +Output from the `az acr build` command shows the upload progress of the source code to Azure and the details of the `docker build` operation. |
| 122 | + |
| 123 | +::: zone-end |
| 124 | + |
| 125 | +::: zone pivot="docker-local" |
| 126 | + |
| 127 | +1. The following command builds a container image for the album UI and tags it with the fully qualified name of the ACR log in server. The `.` at the end of the command represents the docker build context, meaning this command should be run within the *src* folder where the Dockerfile is located. |
| 128 | + |
| 129 | + # [Bash](#tab/bash) |
| 130 | + |
| 131 | + ```azurecli |
| 132 | + docker build --tag $ACR_NAME.azurecr.io/albumapp-ui . |
| 133 | + ``` |
| 134 | +
|
| 135 | + # [PowerShell](#tab/powershell) |
| 136 | +
|
| 137 | + ```powershell |
| 138 | + docker build --tag $ACR_NAME.azurecr.io/albumapp-ui . |
| 139 | + ``` |
| 140 | +
|
| 141 | + --- |
| 142 | +
|
| 143 | +## Push the image to your ACR registry |
| 144 | +
|
| 145 | +1. First, sign in to your Azure Container Registry. |
| 146 | +
|
| 147 | + # [Bash](#tab/bash) |
| 148 | +
|
| 149 | + ```azurecli |
| 150 | + az acr login --name $ACR_NAME |
| 151 | + ``` |
| 152 | +
|
| 153 | + # [PowerShell](#tab/powershell) |
| 154 | +
|
| 155 | + ```powershell |
| 156 | + az acr login --name $ACR_NAME |
| 157 | + ``` |
| 158 | +
|
| 159 | + --- |
| 160 | +
|
| 161 | +1. Now, push the image to your registry. |
| 162 | +
|
| 163 | + # [Bash](#tab/bash) |
| 164 | +
|
| 165 | + ```azurecli |
| 166 | + docker push $ACR_NAME.azurecr.io/albumapp-ui . |
| 167 | + ``` |
| 168 | +
|
| 169 | + # [PowerShell](#tab/powershell) |
| 170 | +
|
| 171 | + ```powershell |
| 172 | + docker push $ACR_NAME.azurecr.io/albumapp-ui . |
| 173 | + ``` |
| 174 | +
|
| 175 | + --- |
| 176 | +
|
| 177 | +::: zone-end |
| 178 | +
|
| 179 | +## Communicate between container apps |
| 180 | +
|
| 181 | +In the previous quickstart, the album API was deployed by creating a container app and enabling external ingress. Setting the container app's ingress to *external* made its HTTP endpoint URL publicly available. |
| 182 | +
|
| 183 | +Now you can configure the front end application to call the API endpoint by going through the following steps: |
| 184 | +
|
| 185 | +* Query the API application for its fully qualified domain name (FQDN). |
| 186 | +* Pass the API FQDN to `az containerapp create` as an environment variable so the UI app can set the base URL for the album API call within the code. |
| 187 | +
|
| 188 | +The [UI application](https://github.com/Azure-Samples/containerapps-albumui) uses the endpoint provided to invoke the album API. The following code is an excerpt from the code used in the *routes > index.js* file. |
| 189 | +
|
| 190 | +```javascript |
| 191 | +const api = axios.create({ |
| 192 | + baseURL: process.env.API_BASE_URL, |
| 193 | + params: {}, |
| 194 | + timeout: process.env.TIMEOUT || 5000, |
| 195 | +}); |
| 196 | +``` |
| 197 | + |
| 198 | +Notice how the `baseURL` property gets its value from the `API_BASE_URL` environment variable. |
| 199 | + |
| 200 | +Run the following command to query for the API endpoint address. |
| 201 | + |
| 202 | +# [Bash](#tab/bash) |
| 203 | + |
| 204 | +```azurecli |
| 205 | +API_BASE_URL=$(az containerapp show --resource-group $RESOURCE_GROUP --name $API_NAME --query properties.configuration.ingress.fqdn -o tsv) |
| 206 | +``` |
| 207 | + |
| 208 | +# [PowerShell](#tab/powershell) |
| 209 | + |
| 210 | +```powershell |
| 211 | +$API_BASE_URL=$(az containerapp show --resource-group $RESOURCE_GROUP --name $API_NAME --query properties.configuration.ingress.fqdn -o tsv) |
| 212 | +``` |
| 213 | + |
| 214 | +--- |
| 215 | + |
| 216 | +Now that you have set the `API_BASE_URL` variable with the FQDN of the album API, you can provide it as an environment variable to the frontend container app. |
| 217 | + |
| 218 | +## Deploy front end application |
| 219 | + |
| 220 | +Create and deploy your container app with the following command. |
| 221 | + |
| 222 | +# [Bash](#tab/bash) |
| 223 | + |
| 224 | +```azurecli |
| 225 | +az containerapp create \ |
| 226 | + --name $FRONTEND_NAME \ |
| 227 | + --resource-group $RESOURCE_GROUP \ |
| 228 | + --environment $ENVIRONMENT \ |
| 229 | + --image $ACR_NAME.azurecr.io/albumapp-ui \ |
| 230 | + --target-port 3000 \ |
| 231 | + --env-vars API_BASE_URL=https://$API_BASE_URL \ |
| 232 | + --ingress 'external' \ |
| 233 | + --registry-server $ACR_NAME.azurecr.io \ |
| 234 | + --query configuration.ingress.fqdn |
| 235 | +``` |
| 236 | + |
| 237 | +# [PowerShell](#tab/powershell) |
| 238 | + |
| 239 | +```azurecli |
| 240 | +az containerapp create ` |
| 241 | + --name $FRONTEND_NAME ` |
| 242 | + --resource-group $RESOURCE_GROUP ` |
| 243 | + --environment $ENVIRONMENT ` |
| 244 | + --image $ACR_NAME.azurecr.io/albumapp-ui ` |
| 245 | + --env-vars API_BASE_URL=https://$API_BASE_URL ` |
| 246 | + --target-port 3000 ` |
| 247 | + --ingress 'external' ` |
| 248 | + --registry-server "$ACR_NAME.azurecr.io" ` |
| 249 | + --query configuration.ingress.fqdn |
| 250 | +``` |
| 251 | + |
| 252 | +--- |
| 253 | + |
| 254 | +By adding the argument `--env-vars "API_BASE_URL=https://$API_ENDPOINT"` to `az containerapp create`, you define an environment variable for your front end application. With this syntax, the environment variable named `API_BASE_URL` is set to the API's FQDN. |
| 255 | + |
| 256 | +## View website |
| 257 | + |
| 258 | +The `az containerapp create` CLI command returns the fully qualified domain name (FQDN) of your album UI container app. Open this location in a browser to navigate to the web application resembling the following screenshot. |
| 259 | + |
| 260 | +:::image type="content" source="media/communicate-between-microservices/azure-container-apps-album-ui.png" alt-text="Screenshot of album list UI microservice."::: |
| 261 | + |
| 262 | +## Clean up resources |
| 263 | + |
| 264 | +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. |
| 265 | + |
| 266 | +# [Bash](#tab/bash) |
| 267 | + |
| 268 | +```azurecli |
| 269 | +az group delete --name $RESOURCE_GROUP |
| 270 | +``` |
| 271 | + |
| 272 | +# [PowerShell](#tab/powershell) |
| 273 | + |
| 274 | +```powershell |
| 275 | +az group delete --name $RESOURCE_GROUP |
| 276 | +``` |
| 277 | + |
| 278 | +--- |
| 279 | + |
| 280 | +> [!TIP] |
| 281 | +> Having issues? Let us know on GitHub by opening an issue in the [Azure Container Apps repo](https://github.com/microsoft/azure-container-apps). |
| 282 | +
|
| 283 | +## Next steps |
| 284 | + |
| 285 | +> [!div class="nextstepaction"] |
| 286 | +> [Environments in Azure Container Apps](environment.md) |
0 commit comments