|
| 1 | +# Azure App Service |
| 2 | + |
| 3 | +Este servicio de Azure nos permite desplegar aplicaciones web y API REST de forma rápida y sencilla. Además, nos permite escalarlas de forma automática y nos ofrece integración con otros servicios de Azure como Azure SQL Database, Azure Cosmos DB, Azure Functions, Azure Storage, Azure Key Vault, Azure Active Directory, etc. |
| 4 | + |
| 5 | +Para este ejemplo, vamos a crear una API REST con ASP.NET Core y la vamos a desplegar en Azure App Service. |
| 6 | + |
| 7 | +Lo primero que necesitas es cargar algunas variables de entorno: |
| 8 | + |
| 9 | +```bash |
| 10 | +# App Service variables |
| 11 | +APP_SVC_PLAN_NAME="tour-of-heroes-plan" |
| 12 | +WEB_API_NAME="tour-of-heroes-api" |
| 13 | +``` |
| 14 | + |
| 15 | +o si estás en Windows: |
| 16 | + |
| 17 | +```pwsh |
| 18 | +# App Service variables |
| 19 | +$APP_SVC_PLAN_NAME="tour-of-heroes-plan" |
| 20 | +$WEB_API_NAME="tour-of-heroes-api" |
| 21 | +``` |
| 22 | + |
| 23 | +## Creando el plan de App Service |
| 24 | + |
| 25 | +Las aplicaciones web alojadas en este servicio necesitan estar dentro de un plan de App Service. Para crear uno, ejecuta el siguiente comando: |
| 26 | + |
| 27 | +```bash |
| 28 | +echo -e "Create Azure App Service for the API 🚀" |
| 29 | + |
| 30 | +az appservice plan create \ |
| 31 | +--name $APP_SVC_PLAN_NAME \ |
| 32 | +--resource-group $RESOURCE_GROUP \ |
| 33 | +--location $LOCATION \ |
| 34 | +--sku S1 |
| 35 | +``` |
| 36 | + |
| 37 | +o si estás en Windows: |
| 38 | + |
| 39 | +```pwsh |
| 40 | +echo -e "Create Azure App Service for the API 🚀" |
| 41 | +
|
| 42 | +az appservice plan create ` |
| 43 | +--name $APP_SVC_PLAN_NAME ` |
| 44 | +--resource-group $RESOURCE_GROUP ` |
| 45 | +--location $LOCATION ` |
| 46 | +--sku S1 |
| 47 | +``` |
| 48 | + |
| 49 | +Este es el que determina el tamaño de las máquinas virtuales que se van a utilizar para alojar las aplicaciones web. En este caso, hemos elegido el plan S1. |
| 50 | + |
| 51 | +## Crear la web app para la API |
| 52 | + |
| 53 | +Una vez que tenemos el plan de App Service, lo siguiente es crear la web app para la API. Para ello, ejecuta el siguiente comando: |
| 54 | + |
| 55 | +```bash |
| 56 | +az webapp create \ |
| 57 | +--name $WEB_API_NAME \ |
| 58 | +--runtime "dotnet:8" \ |
| 59 | +--resource-group $RESOURCE_GROUP \ |
| 60 | +--plan $APP_SVC_PLAN_NAME |
| 61 | +``` |
| 62 | + |
| 63 | +o si estás en Windows: |
| 64 | + |
| 65 | +```pwsh |
| 66 | +az webapp create ` |
| 67 | +--name $WEB_API_NAME ` |
| 68 | +--runtime "dotnet:8" ` |
| 69 | +--resource-group $RESOURCE_GROUP ` |
| 70 | +--plan $APP_SVC_PLAN_NAME |
| 71 | +``` |
| 72 | + |
| 73 | +## Configurar la web app para la API |
| 74 | + |
| 75 | +Una vez que tenemos la web app creada, podemos añadirle algunas configuraciones. Para ello, ejecuta el siguiente comando: |
| 76 | + |
| 77 | +```bash |
| 78 | +az webapp config connection-string set \ |
| 79 | +--name $WEB_API_NAME \ |
| 80 | +--resource-group $RESOURCE_GROUP \ |
| 81 | +--connection-string-type SQLAzure \ |
| 82 | +--settings "DefaultConnection=Server=tcp:$SQL_SERVER_NAME.database.windows.net,1433;Initial Catalog=heroes-db;Persist Security Info=False;User ID=$SQL_USER;Password=$SQL_PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" |
| 83 | + |
| 84 | +az webapp config appsettings set \ |
| 85 | +--name $WEB_API_NAME \ |
| 86 | +--resource-group $RESOURCE_GROUP \ |
| 87 | +--settings "OTEL_SERVICE_NAME=tour-of-heroes-api" |
| 88 | +``` |
| 89 | + |
| 90 | +o si estás en Windows: |
| 91 | + |
| 92 | +```pwsh |
| 93 | +az webapp config connection-string set ` |
| 94 | +--name $WEB_API_NAME ` |
| 95 | +--resource-group $RESOURCE_GROUP ` |
| 96 | +--connection-string-type SQLAzure ` |
| 97 | +--settings "DefaultConnection=Server=tcp:$SQL_SERVER_NAME.database.windows.net,1433;Initial Catalog=heroes-db;Persist Security Info=False;User ID=$SQL_USER;Password=$SQL_PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" |
| 98 | +``` |
| 99 | + |
| 100 | +```pwsh |
| 101 | +az webapp config appsettings set ` |
| 102 | +--name $WEB_API_NAME ` |
| 103 | +--resource-group $RESOURCE_GROUP ` |
| 104 | +--settings "OTEL_SERVICE_NAME=tour-of-heroes-api" |
| 105 | +``` |
| 106 | + |
| 107 | +En este caso, hemos añadido la cadena de conexión a la base de datos y el nombre del servicio para que se pueda identificar en OpenTelemetry/Azure Monitor. |
| 108 | + |
| 109 | +## Desplegar la API |
| 110 | + |
| 111 | +Ahora que ya tenemos un sitio donde desplegar nuestra API lo único que nos queda es desplegarla 😃. |
| 112 | + |
| 113 | +```bash |
| 114 | +echo "Clone the repo..." |
| 115 | +git clone https://github.com/0GiS0/tour-of-heroes-dotnet-api.git |
| 116 | +cd tour-of-heroes-dotnet-api |
| 117 | + |
| 118 | +dotnet publish tour-of-heroes-api.csproj -o ./publish |
| 119 | + |
| 120 | +cd publish |
| 121 | + |
| 122 | +zip -r site.zip * |
| 123 | + |
| 124 | +az webapp deployment source config-zip \ |
| 125 | +--src site.zip \ |
| 126 | +--resource-group $RESOURCE_GROUP \ |
| 127 | +--name $WEB_API_NAME |
| 128 | +``` |
| 129 | + |
| 130 | +o si estás en Windows: |
| 131 | + |
| 132 | +```pwsh |
| 133 | +echo "Clone the repo..." |
| 134 | +git clone https://github.com/0GiS0/tour-of-heroes-dotnet-api.git |
| 135 | +cd tour-of-heroes-dotnet-api |
| 136 | +
|
| 137 | +dotnet publish tour-of-heroes-api.csproj -o ./publish |
| 138 | +
|
| 139 | +cd publish |
| 140 | +
|
| 141 | +Compress-Archive -Path * -DestinationPath site.zip |
| 142 | +
|
| 143 | +az webapp deployment source config-zip ` |
| 144 | +--src site.zip ` |
| 145 | +--resource-group $RESOURCE_GROUP ` |
| 146 | +--name $WEB_API_NAME |
| 147 | +``` |
| 148 | + |
| 149 | +Para comprobar que todo ha ido bien, puedes ejecutar el siguiente comando: |
| 150 | + |
| 151 | +```bash |
| 152 | +echo "API deployed 🚀" |
| 153 | +echo "API URL: https://$WEB_API_NAME.azurewebsites.net" |
| 154 | +``` |
| 155 | + |
| 156 | +Ahora lo único que nos queda por hacer es desplegar el frontal que consuma esta API. Para seguir los pasos puedes continuar en este otro [README](/04-cloud/azure/paas/03-static-web-apps/README.md). |
0 commit comments