|
| 1 | +--- |
| 2 | +title: Quickstart - Deploy your first web application to Azure Spring Apps |
| 3 | +description: Describes how to deploy a web application to Azure Spring Apps. |
| 4 | +author: karlerickson |
| 5 | +ms.service: spring-apps |
| 6 | +ms.topic: quickstart |
| 7 | +ms.date: 04/06/2023 |
| 8 | +ms.author: rujche |
| 9 | +ms.custom: devx-track-java, devx-track-azurecli, mode-other, event-tier1-build-2022, engagement-fy23 |
| 10 | +--- |
| 11 | + |
| 12 | +# Quickstart: Deploy your first web application to Azure Spring Apps |
| 13 | + |
| 14 | +> [!NOTE] |
| 15 | +> The first 50 vCPU hours and 100 GB hours of memory are free each month. For more information, see [Price Reduction - Azure Spring Apps does more, costs less!](https://techcommunity.microsoft.com/t5/apps-on-azure-blog/price-reduction-azure-spring-apps-does-more-costs-less/ba-p/3614058) on the [Apps on Azure Blog](https://techcommunity.microsoft.com/t5/apps-on-azure-blog/bg-p/AppsonAzureBlog). |
| 16 | +
|
| 17 | +> [!NOTE] |
| 18 | +> Azure Spring Apps is the new name for the Azure Spring Cloud service. Although the service has a new name, you'll see the old name in some places for a while as we work to update assets such as screenshots, videos, and diagrams. |
| 19 | +
|
| 20 | +**This article applies to:** ✔️ Basic/Standard tier ❌ Enterprise tier |
| 21 | + |
| 22 | +This quickstart shows how to deploy a Spring Boot web application to Azure Spring Apps. The sample project is a simple ToDo application to add tasks, mark when they're complete, and then delete them. The following screenshot shows the application: |
| 23 | + |
| 24 | +:::image type="content" source="./media/quickstart-deploy-web-app/todo-app.png" alt-text="Sceenshot of a sample web application in Azure Spring Apps." lightbox="./media/quickstart-deploy-web-app/todo-app.png"::: |
| 25 | + |
| 26 | +This application is a typical three-layers web application with the following layers: |
| 27 | + |
| 28 | +- A frontend bounded [React](https://reactjs.org/) application. |
| 29 | +- A backend Spring web application that uses Spring Data JPA to access a relational database. |
| 30 | +- A relational database. For localhost, the application uses [H2 Database Engine](https://www.h2database.com/html/main.html). For Azure Spring Apps, the application uses Azure Database for PostgreSQL. For more information about Azure Database for PostgreSQL, see [Flexible Server documentation](../postgresql/flexible-server/overview.md). |
| 31 | + |
| 32 | +The following diagram shows the architecture of the system: |
| 33 | + |
| 34 | +:::image type="content" source="media/quickstart-deploy-web-app/diagram.png" alt-text="Image that shows the architecture of a Spring web application."::: |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +- An Azure subscription. If you don't have a subscription, create a [free account](https://azure.microsoft.com/free/) before you begin. |
| 39 | +- [Azure CLI](/cli/azure/install-azure-cli). Version 2.45.0 or greater. |
| 40 | +- [Git](https://git-scm.com/downloads). |
| 41 | +- [Java Development Kit (JDK)](/java/azure/jdk/), version 17. |
| 42 | + |
| 43 | +## Clone and run the sample project locally |
| 44 | + |
| 45 | +Use the following steps to clone and run the app locally. |
| 46 | + |
| 47 | +1. The sample project is available on GitHub. Use the following command to clone the sample project: |
| 48 | + |
| 49 | + ```bash |
| 50 | + git clone https://github.com/Azure-Samples/ASA-Samples-Web-Application.git |
| 51 | + ``` |
| 52 | + |
| 53 | +1. Use the following command to build the sample project: |
| 54 | + |
| 55 | + ```bash |
| 56 | + cd ASA-Samples-Web-Application |
| 57 | + ./mvnw clean package -DskipTests |
| 58 | + ``` |
| 59 | + |
| 60 | +1. Use the following command to run the sample application by Maven: |
| 61 | + |
| 62 | + ```bash |
| 63 | + java -jar web/target/simple-todo-web-0.0.1-SNAPSHOT.jar |
| 64 | + ``` |
| 65 | + |
| 66 | +1. Go to `http://localhost:8080` in your browser to access the application. |
| 67 | + |
| 68 | +## Prepare the cloud environment |
| 69 | + |
| 70 | +The main resources required to run this sample are an Azure Spring Apps instance and an Azure Database for PostgreSQL instance. This section provides the steps to create these resources. |
| 71 | + |
| 72 | +### Provide names for each resource |
| 73 | + |
| 74 | +Create variables to hold the resource names. Be sure to replace the placeholders with your own values. |
| 75 | + |
| 76 | +```azurecli |
| 77 | +RESOURCE_GROUP=<resource-group-name> |
| 78 | +LOCATION=<location> |
| 79 | +POSTGRESQL_SERVER=<server-name> |
| 80 | +POSTGRESQL_DB=<database-name> |
| 81 | +AZURE_SPRING_APPS_NAME=<Azure-Spring-Apps-service-instance-name> |
| 82 | +APP_NAME=<web-app-name> |
| 83 | +CONNECTION=<connection-name> |
| 84 | +``` |
| 85 | + |
| 86 | +### Create a new resource group |
| 87 | + |
| 88 | +Use the following steps to create a new resource group. |
| 89 | + |
| 90 | +1. Use the following command to sign in to Azure CLI. |
| 91 | + |
| 92 | + ```azurecli |
| 93 | + az login |
| 94 | + ``` |
| 95 | + |
| 96 | +1. Use the following command to set the default location. |
| 97 | + |
| 98 | + ```azurecli |
| 99 | + az configure --defaults location=${LOCATION} |
| 100 | + ``` |
| 101 | + |
| 102 | +1. Set the default subscription. Use the following command to first list all available subscriptions: |
| 103 | + |
| 104 | + ```azurecli |
| 105 | + az account list --output table |
| 106 | + ``` |
| 107 | + |
| 108 | +1. Choose a subscription and set it as the default subscription with the following command: |
| 109 | + |
| 110 | + ```azurecli |
| 111 | + az account set --subscription <subscription-ID> |
| 112 | + ``` |
| 113 | + |
| 114 | +1. Use the following command to create a resource group. |
| 115 | + |
| 116 | + ```azurecli |
| 117 | + az group create --resource-group ${RESOURCE_GROUP} |
| 118 | + ``` |
| 119 | + |
| 120 | +1. Use the following command to set the newly created resource group as the default resource group. |
| 121 | + |
| 122 | + ```azurecli |
| 123 | + az configure --defaults group=${RESOURCE_GROUP} |
| 124 | + ``` |
| 125 | + |
| 126 | +### Create an Azure Spring Apps instance |
| 127 | + |
| 128 | +Azure Spring Apps is used to host the Spring web app. Create an Azure Spring Apps instance and an application inside it. |
| 129 | + |
| 130 | +1. Use the following command to create an Azure Spring Apps service instance. |
| 131 | + |
| 132 | + ```azurecli |
| 133 | + az spring create --name ${AZURE_SPRING_APPS_NAME} |
| 134 | + ``` |
| 135 | + |
| 136 | +1. Use the following command to create an application in the Azure Spring Apps instance. |
| 137 | + |
| 138 | + ```azurecli |
| 139 | + az spring app create \ |
| 140 | + --service ${AZURE_SPRING_APPS_NAME} \ |
| 141 | + --name ${APP_NAME} \ |
| 142 | + --runtime-version Java_17 \ |
| 143 | + --assign-endpoint true |
| 144 | + ``` |
| 145 | + |
| 146 | +### Prepare the PostgreSQL instance |
| 147 | + |
| 148 | +The Spring web app uses H2 for the database in localhost, and Azure Database for PostgreSQL for the database in Azure. |
| 149 | + |
| 150 | +Use the following command to create a PostgreSQL instance: |
| 151 | + |
| 152 | +```azurecli |
| 153 | +az postgres flexible-server create \ |
| 154 | + --name ${POSTGRESQL_SERVER} \ |
| 155 | + --database-name ${POSTGRESQL_DB} \ |
| 156 | + --active-directory-auth Enabled |
| 157 | +``` |
| 158 | + |
| 159 | +To ensure that the application is accessible only by PostgreSQL in Azure Spring Apps, enter `n` to the prompts to enable access to a specific IP address and to enable access for all IP addresses. |
| 160 | + |
| 161 | +```output |
| 162 | +Do you want to enable access to client xxx.xxx.xxx.xxx (y/n) (y/n): n |
| 163 | +Do you want to enable access for all IPs (y/n): n |
| 164 | +``` |
| 165 | + |
| 166 | +### Connect app instance to PostgreSQL instance |
| 167 | + |
| 168 | +After the application instance and the PostgreSQL instance are created, the application instance can't access the PostgreSQL instance directly. The following steps use Service Connector to configure the needed network settings and connection information. For more information about Service Connector, see [What is Service Connector?](../service-connector/overview.md). |
| 169 | + |
| 170 | +1. If you're using Service Connector for the first time, use the following command to register the Service Connector resource provider. |
| 171 | + |
| 172 | + ```azurecli |
| 173 | + az provider register --namespace Microsoft.ServiceLinker |
| 174 | + ``` |
| 175 | + |
| 176 | +1. Use the following command to achieve a passwordless connection: |
| 177 | + |
| 178 | + ```azurecli |
| 179 | + az extension add --name serviceconnector-passwordless --upgrade |
| 180 | + ``` |
| 181 | + |
| 182 | +1. Use the following command to create a service connection between the application and the PostgreSQL database: |
| 183 | + |
| 184 | + ```azurecli |
| 185 | + az spring connection create postgres-flexible \ |
| 186 | + --resource-group ${RESOURCE_GROUP} \ |
| 187 | + --service ${AZURE_SPRING_APPS_NAME} \ |
| 188 | + --app ${APP_NAME} \ |
| 189 | + --client-type springBoot \ |
| 190 | + --target-resource-group ${RESOURCE_GROUP} \ |
| 191 | + --server ${POSTGRESQL_SERVER} \ |
| 192 | + --database ${POSTGRESQL_DB} \ |
| 193 | + --system-identity \ |
| 194 | + --connection ${CONNECTION} |
| 195 | + ``` |
| 196 | + |
| 197 | + The `--system-identity` parameter is required for the passwordless connection. For more information, see [Bind an Azure Database for PostgreSQL to your application in Azure Spring Apps](how-to-bind-postgres.md). |
| 198 | + |
| 199 | +1. After the connection is created, use the following command to validate the connection: |
| 200 | + |
| 201 | + ```azurecli |
| 202 | + az spring connection validate \ |
| 203 | + --resource-group ${RESOURCE_GROUP} \ |
| 204 | + --service ${AZURE_SPRING_APPS_NAME} \ |
| 205 | + --app ${APP_NAME} \ |
| 206 | + --connection ${CONNECTION} |
| 207 | + ``` |
| 208 | + |
| 209 | + The output should appear similar to the following JSON code: |
| 210 | + |
| 211 | + ```json |
| 212 | + [ |
| 213 | + { |
| 214 | + "additionalProperties": {}, |
| 215 | + "description": null, |
| 216 | + "errorCode": null, |
| 217 | + "errorMessage": null, |
| 218 | + "name": "The target existence is validated", |
| 219 | + "result": "success" |
| 220 | + }, |
| 221 | + { |
| 222 | + "additionalProperties": {}, |
| 223 | + "description": null, |
| 224 | + "errorCode": null, |
| 225 | + "errorMessage": null, |
| 226 | + "name": "The target service firewall is validated", |
| 227 | + "result": "success" |
| 228 | + }, |
| 229 | + { |
| 230 | + "additionalProperties": {}, |
| 231 | + "description": null, |
| 232 | + "errorCode": null, |
| 233 | + "errorMessage": null, |
| 234 | + "name": "The configured values (except username/password) is validated", |
| 235 | + "result": "success" |
| 236 | + }, |
| 237 | + { |
| 238 | + "additionalProperties": {}, |
| 239 | + "description": null, |
| 240 | + "errorCode": null, |
| 241 | + "errorMessage": null, |
| 242 | + "name": "The identity existence is validated", |
| 243 | + "result": "success" |
| 244 | + } |
| 245 | + ] |
| 246 | + ``` |
| 247 | + |
| 248 | +## Deploy the app to Azure Spring Apps |
| 249 | + |
| 250 | +Now that the cloud environment is prepared, the application is ready to deploy. |
| 251 | + |
| 252 | +1. Use the following command to deploy the app: |
| 253 | + |
| 254 | + ```azurecli |
| 255 | + az spring app deploy \ |
| 256 | + --service ${AZURE_SPRING_APPS_NAME} \ |
| 257 | + --name ${APP_NAME} \ |
| 258 | + --artifact-path web/target/simple-todo-web-0.0.1-SNAPSHOT.jar |
| 259 | + ``` |
| 260 | + |
| 261 | +1. After the deployment has completed, you can access the app with this URL: `https://${AZURE_SPRING_APPS_NAME}-${APP_NAME}.azuremicroservices.io/`. The page should appear as you saw in localhost. |
| 262 | + |
| 263 | +1. If there's a problem when you deploy the app, check the app's log to investigate by using the following command: |
| 264 | + |
| 265 | + ```azurecli |
| 266 | + az spring app logs \ |
| 267 | + --service ${AZURE_SPRING_APPS_NAME} \ |
| 268 | + --name ${APP_NAME} |
| 269 | + ``` |
| 270 | + |
| 271 | +## Clean up resources |
| 272 | + |
| 273 | +- To avoid unnecessary costs, use the following command to delete the resource group. |
| 274 | + |
| 275 | + ```azurecli |
| 276 | + az group delete --name ${RESOURCE_GROUP} |
| 277 | + ``` |
| 278 | + |
| 279 | +## Next steps |
| 280 | + |
| 281 | +> [!div class="nextstepaction"] |
| 282 | +> [Create a service connection in Azure Spring Apps with the Azure CLI](../service-connector/quickstart-cli-spring-cloud-connection.md) |
| 283 | +
|
| 284 | +For more information, see the following articles: |
| 285 | + |
| 286 | +- [Azure Spring Apps Samples](https://github.com/Azure-Samples/Azure-Spring-Cloud-Samples). |
| 287 | +- [Spring on Azure](/azure/developer/java/spring/) |
| 288 | +- [Spring Cloud Azure](/azure/developer/java/spring-framework/) |
0 commit comments