Skip to content

Commit e810143

Browse files
MassimoCAniruddh25
andauthored
docs and samples : running on Azure Container Apps (#1457)
## Why make this change? - Closes #1339 ## What is this change? - update the running-in-azure.md to cover the Azure Container Apps case - added the Azure Container Apps case to the samples ## How was this tested? - [x] Integration Tests (running the samples script) ## Sample Request(s) - the new script (azure-container-apps-deploy.sh) uses the same approach as to the existing azure-deploy.sh. Same naming convention, logging, etc... --------- Co-authored-by: Aniruddh Munde <[email protected]>
1 parent dd6b21a commit e810143

File tree

4 files changed

+223
-3
lines changed

4 files changed

+223
-3
lines changed

docs/running-in-azure.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Running Data API builder in Azure
22

3-
Data API builder can be run in Azure in two different ways: using Azure Static Web Apps or using Azure Container Instances or Azure App Service.
3+
Data API builder can be run in Azure in two different ways: using Azure Static Web Apps or using any container service like Azure Container Instances, Azure App Service, Azure Container Apps, etc.
44

55
## Use Azure Static Web Apps
66

77
When running Data API builder in Azure Static Web Apps, you don't have to worry about the infrastructure as it will be managed by Azure. When running Data API builder in Azure Container Instances or Azure App Service, you will have to manage the infrastructure yourself.
88

99
To learn how to use Data API builder with Azure Static Web Apps, refer to the Azure Static Web Apps documentation: [Connecting to a database with Azure Static Web Apps](https://learn.microsoft.com/en-us/azure/static-web-apps/database-overview).
1010

11-
## Use a Container
11+
## Use Azure Container Instance
1212

1313
If you prefer to manage the infrastructure yourself, you can deploy the Data API builder container in Azure. Data API builder image is available on the Microsoft Container Registry: https://mcr.microsoft.com/en-us/product/azure-databases/data-api-builder/about
1414

@@ -30,3 +30,30 @@ On first run, the script will create an `.env` file that you will have to fill o
3030
- `DAB_CONFIG_FILE`: the configuration file you want to use (eg: `./my-dab-config.json`). Please note the the file must be in the same folder where the `./azure-deploy.sh` script is located.
3131

3232
After the script has finished running, it will return the public container IP address. Use your favorite REST or GraphQL client to access the Data API builder exposed endpoints as configured in the configuration file you provided.
33+
34+
## Use Azure Container Apps
35+
36+
If you prefer to manage the infrastructure yourself, you can deploy the Data API builder container in Azure. Data API builder image is available on the Microsoft Container Registry: https://mcr.microsoft.com/en-us/product/azure-databases/data-api-builder/about
37+
38+
To run Data API builder in Azure Container Apps, you need to
39+
40+
- Create a resource group
41+
- Create a storage account, with File Share enabled
42+
- Upload the `dab-config.json` file to the storage account
43+
- Create the Azure Container Apps environment and mount the storage account file share so that it can be accessed by the containers running in the environment.
44+
- Create the Azure Container Apps application, using the image from the Microsoft Container Registry and mounting the storage account file share so that it can accessed by Data API builder.
45+
46+
A sample shell script that can be run on Linux (using the [Cloud Shell](https://learn.microsoft.com/en-us/azure/cloud-shell/overview) if you don't have a Linux machine or WSL installed) is available in `/samples/azure` folder.
47+
48+
On first run, the script will create an `.env` file that you will have to fill out with the correct values for your environment.
49+
50+
- `RESOURCE_GROUP`: name of the resource group you are using (eg: `my-dab-rg`)
51+
- `STORAGE_ACCOUNT`: the name for the Storage Account you want to create (eg: `dabstorage`)
52+
- `LOCATION`: the region where you want to create the resources (eg: `westus2`)
53+
- `LOG_ANALYTICS_WORKSPACE`: the name of the Log Analytics Workspace you want to create (eg: `dablogging`)
54+
- `CONTAINERAPPS_ENVIRONMENT`: the name of the Container Apps environment you want to create (eg: `dm-dab-aca-env`)
55+
- `CONTAINERAPPS_APP_NAME`: the name of the Container Apps application you want to create (eg: `dm-dab-aca-app`)
56+
- `DAB_CONFIG_FILE`: the configuration file you want to use (eg: `./my-dab-config.json`). Please note the the file must be in the same folder where the `./azure-container-apps-deploy.sh` script is located.
57+
58+
After the script has finished running, it will return the FQDN of Azure Container Apps. Use your favorite REST or GraphQL client to access the Data API builder exposed endpoints as configured in the configuration file you provided.
59+
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/bash
2+
3+
# Strict mode, fail on any error
4+
set -euo pipefail
5+
6+
# Azure configuration
7+
FILE=".env"
8+
if [[ -f $FILE ]]; then
9+
echo "loading from .env"
10+
export $(egrep . $FILE | xargs -n1)
11+
else
12+
cat << EOF > .env
13+
RESOURCE_GROUP=""
14+
STORAGE_ACCOUNT=""
15+
LOCATION=""
16+
LOG_ANALYTICS_WORKSPACE=""
17+
CONTAINERAPPS_ENVIRONMENT="dm-dab-aca-env"
18+
CONTAINERAPPS_APP_NAME="dm-dab-aca-app"
19+
DAB_CONFIG_FILE="./dab-config.json"
20+
EOF
21+
echo "Enviroment file (.env) not detected."
22+
echo "Please configure values for your environment in the created .env file and run the script again."
23+
echo "Read the docs/running-in-azure.md to get info on needed enviroment variables."
24+
exit 1
25+
fi
26+
27+
echo "starting"
28+
cat << EOF > log.txt
29+
EOF
30+
31+
FILE_SHARE="dabconfig"
32+
DAB_CONFIG_FILE_NAME="dab-config.json"
33+
DABCONFIGFOLDER="./${FILE_SHARE}/${DAB_CONFIG_FILE_NAME}"
34+
35+
echo "creating resource group '$RESOURCE_GROUP'" | tee -a log.txt
36+
az group create --name $RESOURCE_GROUP --location $LOCATION \
37+
-o json >> log.txt
38+
39+
echo "creating storage account: '$STORAGE_ACCOUNT'" | tee -a log.txt
40+
az storage account create --name $STORAGE_ACCOUNT --resource-group $RESOURCE_GROUP --location $LOCATION --sku Standard_LRS \
41+
-o json >> log.txt
42+
43+
echo "retrieving storage connection string" | tee -a log.txt
44+
STORAGE_CONNECTION_STRING=$(az storage account show-connection-string --name $STORAGE_ACCOUNT -g $RESOURCE_GROUP -o tsv)
45+
46+
echo 'creating file share' | tee -a log.txt
47+
az storage share create -n $FILE_SHARE --connection-string $STORAGE_CONNECTION_STRING \
48+
-o json >> log.txt
49+
50+
echo "uploading configuration file '$DAB_CONFIG_FILE'" | tee -a log.txt
51+
az storage file upload --source $DAB_CONFIG_FILE --path $DAB_CONFIG_FILE_NAME --share-name $FILE_SHARE --connection-string $STORAGE_CONNECTION_STRING \
52+
-o json >> log.txt
53+
54+
echo "create log analytics workspace '$LOG_ANALYTICS_WORKSPACE'" | tee -a log.txt
55+
az monitor log-analytics workspace create \
56+
--resource-group $RESOURCE_GROUP \
57+
--location $LOCATION \
58+
--workspace-name $LOG_ANALYTICS_WORKSPACE
59+
60+
echo "retrieving log analytics client id" | tee -a log.txt
61+
LOG_ANALYTICS_WORKSPACE_CLIENT_ID=$(az monitor log-analytics workspace show \
62+
--resource-group "$RESOURCE_GROUP" \
63+
--workspace-name "$LOG_ANALYTICS_WORKSPACE" \
64+
--query customerId \
65+
--output tsv | tr -d '[:space:]')
66+
67+
echo "retrieving log analytics secret" | tee -a log.txt
68+
LOG_ANALYTICS_WORKSPACE_CLIENT_SECRET=$(az monitor log-analytics workspace get-shared-keys \
69+
--resource-group "$RESOURCE_GROUP" \
70+
--workspace-name "$LOG_ANALYTICS_WORKSPACE" \
71+
--query primarySharedKey \
72+
--output tsv | tr -d '[:space:]')
73+
74+
echo "retrieving storage key" | tee -a log.txt
75+
STORAGE_KEY=$(az storage account keys list -g $RESOURCE_GROUP -n $STORAGE_ACCOUNT --query '[0].value' -o tsv)
76+
77+
echo "creating container apps environment: '$CONTAINERAPPS_ENVIRONMENT'" | tee -a log.txt
78+
az containerapp env create \
79+
--resource-group $RESOURCE_GROUP \
80+
--location $LOCATION \
81+
--name "$CONTAINERAPPS_ENVIRONMENT" \
82+
--logs-workspace-id "$LOG_ANALYTICS_WORKSPACE_CLIENT_ID" \
83+
--logs-workspace-key "$LOG_ANALYTICS_WORKSPACE_CLIENT_SECRET"
84+
85+
echo "waiting to finalize the ACA environment" | tee -a log.txt
86+
while [ "$(az containerapp env show -n $CONTAINERAPPS_ENVIRONMENT -g $RESOURCE_GROUP --query properties.provisioningState -o tsv | tr -d '[:space:]')" != "Succeeded" ]; do sleep 10; done
87+
88+
echo "get ACA environment id" | tee -a log.txt
89+
CONTAINERAPPS_ENVIRONMENTID=$(az containerapp env show -n "$CONTAINERAPPS_ENVIRONMENT" -g "$RESOURCE_GROUP" --query id -o tsv |sed 's/\r$//')
90+
91+
echo "mount storage account on azure container apps environment" | tee -a log.txt
92+
RES=$(az containerapp env storage set --name $CONTAINERAPPS_ENVIRONMENT \
93+
--resource-group $RESOURCE_GROUP \
94+
--storage-name $FILE_SHARE \
95+
--azure-file-account-name $STORAGE_ACCOUNT \
96+
--azure-file-account-key $STORAGE_KEY \
97+
--azure-file-share-name $FILE_SHARE \
98+
--access-mode ReadWrite)
99+
100+
101+
echo "creating container app : '$CONTAINERAPPS_APP_NAME' on the environment : '$CONTAINERAPPS_ENVIRONMENT'" | tee -a log.txt
102+
az deployment group create \
103+
-g $RESOURCE_GROUP \
104+
-f ./bicep/dab-on-aca.bicep \
105+
-p appName=$CONTAINERAPPS_APP_NAME dabConfigFileName=$DAB_CONFIG_FILE_NAME mountedStorageName=$FILE_SHARE environmentId=$CONTAINERAPPS_ENVIRONMENTID
106+
107+
echo "get the azure container app FQDN" | tee -a log.txt
108+
ACA_FQDN=$(az containerapp show -n $CONTAINERAPPS_APP_NAME -g $RESOURCE_GROUP --query properties.configuration.ingress.fqdn -o tsv | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
109+
110+
echo "you can now try out the API at the following address : https://${ACA_FQDN}/api/<your-entity-name>"
111+
112+
echo "done" | tee -a log.txt

samples/azure/bicep/dab-on-aca.bicep

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
param appName string
2+
param dabConfigFileName string = 'dab-config.json'
3+
param mountedStorageName string = 'dabconfig'
4+
param isExternalIngress bool = true
5+
param location string = resourceGroup().location
6+
param environmentId string
7+
param tag string = 'latest'
8+
param env array = [
9+
{
10+
name: 'DOTNET_ENVIRONMENT'
11+
value: 'Production'
12+
}
13+
{
14+
name: 'ASPNETCORE_ENVIRONMENT'
15+
value: 'Production'
16+
}
17+
]
18+
19+
var dabConfigFilePath='--ConfigFileName=./${mountedStorageName}/${dabConfigFileName}'
20+
21+
resource containerApp 'Microsoft.App/containerApps@2022-10-01' = {
22+
name: appName
23+
location: location
24+
properties: {
25+
environmentId: environmentId
26+
configuration: {
27+
activeRevisionsMode:'Single'
28+
ingress: {
29+
allowInsecure:true
30+
external: isExternalIngress
31+
targetPort: 5000
32+
transport: 'auto'
33+
}
34+
}
35+
template: {
36+
containers: [
37+
{
38+
image: 'mcr.microsoft.com/azure-databases/data-api-builder:${tag}'
39+
name: appName
40+
env: env
41+
args: [dabConfigFilePath]
42+
volumeMounts: [
43+
{
44+
volumeName: 'azure-file-volume'
45+
mountPath: '/App/${mountedStorageName}'
46+
}
47+
]
48+
resources:{
49+
cpu: json('0.5')
50+
memory:'1Gi'
51+
}
52+
}
53+
]
54+
scale: {
55+
minReplicas: 0
56+
maxReplicas: 2
57+
rules:[
58+
{
59+
name: 'http'
60+
http:{
61+
metadata:{
62+
concurrentRequests: '200'
63+
}
64+
}
65+
}
66+
]
67+
}
68+
volumes:[
69+
{
70+
name:'azure-file-volume'
71+
storageType:'AzureFile'
72+
storageName: mountedStorageName
73+
}
74+
]
75+
}
76+
}
77+
}
78+
79+
output fqdn string = containerApp.properties.configuration.ingress.fqdn

samples/azure/readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Azure Deploy Sample
22

3-
Use the provided `azure-deploy.sh` script to deploy Data API builder in an Azure Container Instance as described in [Running Data API Builder in Azure](/docs/running-in-azure.md)
3+
Use the provided `azure-deploy.sh` script to deploy Data API builder in an Azure Container Instance as described in [Running Data API Builder in Azure](/docs/running-in-azure.md)
4+
5+
Use the provided `azure-container-apps-deploy.sh` script to deploy Data API builder in an Azure Container Apps as described in [Running Data API Builder in Azure](/docs/running-in-azure.md)

0 commit comments

Comments
 (0)