Skip to content

Commit 1756ff5

Browse files
authored
Merge pull request #9 from john0isaac/feat-azd-compatible
Feat azd compatible
2 parents 2b2ce01 + b6a0993 commit 1756ff5

File tree

13 files changed

+147
-48
lines changed

13 files changed

+147
-48
lines changed

.azure/config

Lines changed: 0 additions & 6 deletions
This file was deleted.

.azure/config.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

.azure/translation-telephone/.env

Lines changed: 0 additions & 6 deletions
This file was deleted.

.azure/translation-telephone/config.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ env
55
.coverage
66
node_modules
77
.venv
8+
.azure

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,36 @@ The app is currently hosted on Microsoft Azure. Specifically:
5252
* Azure Database for PostGreSQL flexible server
5353
* Azure Cognitive Services (for Translation)
5454

55-
To deploy your own instance, follow the [tutorial for Flask app + PostGreSQL deployment](https://docs.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app) but using this app instead of the sample app.
55+
To deploy your own instance, follow these steps:
5656

57-
Make sure you specify the following environment variables in the App Service configuration:
57+
1. Sign up for a [free Azure account](https://azure.microsoft.com/free/?WT.mc_id=python-79461-pamelafox)
58+
2. Install the [Azure Dev CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd?WT.mc_id=python-79461-pamelafox). (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.)
59+
3. Initialize a new `azd` environment:
5860

59-
* `DBHOST`, `DBNAME`, `DBPASS`, `DBUSER`: The above linked tutorial shows how to set these.
60-
* `FLASK_APP`: Set to 'src'
61-
* `AZURE_TRANSLATE_API_KEY`: Get this by registering for Azure Cognitive Services.
61+
```shell
62+
azd init
63+
```
6264

63-
You will also need to migrate the database by using the App Service SSH and running `flask db upgrade`.
65+
It will prompt you to provide a name (like "flask-app") that will later be used in the name of the deployed resources.
66+
67+
4. Provision and deploy all the resources:
68+
69+
```shell
70+
azd up
71+
```
72+
73+
It will prompt you to login, pick a subscription, and provide a location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location (like to "centralus") can help, as there may be availability constraints for some of the resources.
74+
75+
5. When azd has finished deploying, you'll see an endpoint URI in the command output. Visit that URI and you should see the website and be able to translate messages.
76+
77+
6. For the website to work fully (i.e. save translations to the database), you must migrate the database. Navigate to the App Service in the Azure Portal, select SSH, and run this command once you're in the SSH terminal:
78+
79+
```shell
80+
flask db upgrade
81+
```
82+
83+
6. When you've made any changes to the app code, you can just run:
84+
85+
```shell
86+
azd deploy
87+
```

azure.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json
22

3-
name: flask-db-quiz-example
3+
name: translation-telephone
44

55
services:
66
web:

infra/app/security.bicep

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
param keyVaultName string
2+
param rgName string
3+
4+
param name string
5+
param cognitiveServiceName string
6+
7+
// Store Cognitive Service Key in Key Vault
8+
module cognitiveServiceSecret '../core/security/keyvault-secret.bicep' = {
9+
name: 'cognitiveServiceKey'
10+
params: {
11+
name: name
12+
keyVaultName : keyVaultName
13+
secretValue: listKeys(resourceId(subscription().subscriptionId, rgName, 'Microsoft.CognitiveServices/accounts', cognitiveServiceName), '2023-05-01').key1
14+
}
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@description('The name of the Cognitive Service.')
2+
param name string
3+
4+
@description('The location into which your Azure resources should be deployed.')
5+
param location string = resourceGroup().location
6+
7+
@description('The tags to apply to each resource.')
8+
param tags object = {}
9+
10+
@description('The kind of Cognitive Service to create. See: https://learn.microsoft.com/en-us/azure/cognitive-services/create-account-bicep for available kinds.')
11+
@allowed([ 'CognitiveServices', 'ComputerVision', 'CustomVision.Prediction', 'CustomVision.Training', 'Face', 'FormRecognizer', 'SpeechServices', 'LUIS', 'QnAMaker', 'TextAnalytics', 'TextTranslation', 'AnomalyDetector', 'ContentModerator', 'Personalizer', 'OpenAI' ])
12+
param kind string
13+
14+
@description('The name of the SKU. Be aware that not all SKUs may be available for your Subscription. See: https://learn.microsoft.com/en-us/rest/api/cognitiveservices/accountmanagement/resource-skus')
15+
@allowed([ 'F0', 'S0', 'S1', 'S2', 'S3', 'S4' ])
16+
param sku string
17+
18+
param publicNetworkAccess string = 'Enabled'
19+
20+
resource cognitiveService 'Microsoft.CognitiveServices/accounts@2023-05-01' = {
21+
name: name
22+
location: location
23+
tags: tags
24+
sku: {
25+
name: sku
26+
}
27+
kind: kind
28+
properties: {
29+
publicNetworkAccess: publicNetworkAccess
30+
31+
}
32+
}
33+
34+
@description('Resource Name')
35+
output name string = cognitiveService.name
36+
37+
@description('Resource Id')
38+
output id string = cognitiveService.id
39+
40+
@description('Endpoint')
41+
output endpoint string = cognitiveService.properties.endpoint

infra/main.bicep

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,36 @@ param name string
99
@description('Primary location for all resources')
1010
param location string
1111

12-
@secure()
13-
@description('PostGreSQL Server administrator password')
14-
param postgresServerPassword string
15-
1612
@description('Id of the user or app to assign application roles')
1713
param principalId string = ''
1814

15+
@secure()
16+
@description('PostGreSQL Server administrator password')
17+
param postgresAdminPassword string
18+
1919
var resourceToken = toLower(uniqueString(subscription().id, name, location))
2020
var tags = { 'azd-env-name': name }
2121
var prefix = '${name}-${resourceToken}'
22-
23-
var postgresServerName = '${prefix}-postgres'
24-
var postgresServerAdmin = 'flaskadmin'
25-
var postgresDatabaseName = 'transtel'
22+
var rgName = '${prefix}-rg'
2623

2724
resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
28-
name: '${prefix}-rg'
25+
name: rgName
2926
location: location
3027
tags: tags
3128
}
3229

30+
module cognitiveService 'core/ai/cognitiveservices.bicep' = {
31+
name: 'cognitiveservice'
32+
scope: resourceGroup
33+
params: {
34+
name: '${prefix}-cognitiveservice'
35+
location: location
36+
sku: 'S1'
37+
kind: 'TextTranslation'
38+
publicNetworkAccess: 'Enabled'
39+
}
40+
}
41+
3342
// Store secrets in a keyvault
3443
module keyVault './core/security/keyvault.bicep' = {
3544
name: 'keyvault'
@@ -42,16 +51,40 @@ module keyVault './core/security/keyvault.bicep' = {
4251
}
4352
}
4453

45-
module keyVaultSecret './core/security/keyvault-secret.bicep' = {
46-
name: 'keyvault-secret'
54+
module postgreSQLDBSecret './core/security/keyvault-secret.bicep' = {
55+
name: 'keyvaultsecret-postgresql'
4756
scope: resourceGroup
4857
params: {
4958
keyVaultName: keyVault.outputs.name
50-
name: 'postgresServerPassword'
51-
secretValue: postgresServerPassword
59+
name: 'postgresAdminPassword'
60+
secretValue: postgresAdminPassword
5261
}
5362
}
5463

64+
module cognitiveServiceSecret './app/security.bicep' = {
65+
name: 'keyvaultsecret-cognitiveservice'
66+
scope: resourceGroup
67+
params: {
68+
rgName: rgName
69+
keyVaultName: keyVault.outputs.name
70+
name: 'cognitiveServiceKey'
71+
cognitiveServiceName: cognitiveService.outputs.name
72+
}
73+
}
74+
75+
module webaccess './core/security/keyvault-access.bicep' = {
76+
name: 'web-keyvault-access'
77+
scope: resourceGroup
78+
params: {
79+
keyVaultName: keyVault.outputs.name
80+
principalId: web.outputs.identityPrincipalId
81+
}
82+
}
83+
84+
var postgresServerName = '${prefix}-postgres'
85+
var postgresServerAdmin = 'admin${uniqueString(resourceGroup.id)}'
86+
var postgresDatabaseName = 'transtel'
87+
5588

5689
module postgresServer 'core/database/postgresql/flexibleserver.bicep' = {
5790
name: 'postgresql'
@@ -68,8 +101,8 @@ module postgresServer 'core/database/postgresql/flexibleserver.bicep' = {
68101
storageSizeGB: 32
69102
}
70103
version: '13'
71-
administratorLogin: 'flaskadmin'
72-
administratorLoginPassword: postgresServerPassword
104+
administratorLogin: postgresServerAdmin
105+
administratorLoginPassword: postgresAdminPassword
73106
databaseNames: [postgresDatabaseName]
74107
allowAzureIPsFirewall: true
75108
}
@@ -91,9 +124,11 @@ module web 'core/host/appservice.bicep' = {
91124
DBHOST: '${postgresServerName}.postgres.database.azure.com'
92125
DBNAME: postgresDatabaseName
93126
DBUSER: postgresServerAdmin
94-
DBPASS: postgresServerPassword
127+
DBPASS: '@Microsoft.KeyVault(VaultName=${keyVault.outputs.name};SecretName=postgresAdminPassword)'
95128
FLASK_APP: 'src'
129+
AZURE_TRANSLATE_API_KEY: '@Microsoft.KeyVault(VaultName=${keyVault.outputs.name};SecretName=cognitiveServiceKey)'
96130
}
131+
keyVaultName: keyVault.outputs.name
97132
}
98133
}
99134

@@ -114,3 +149,4 @@ module appServicePlan 'core/host/appserviceplan.bicep' = {
114149

115150
output WEB_URI string = web.outputs.uri
116151
output AZURE_LOCATION string = location
152+
output AZURE_KEY_VAULT_NAME string = keyVault.outputs.name

0 commit comments

Comments
 (0)