-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
1867 lines (1634 loc) · 76.1 KB
/
Copy pathdeploy.sh
File metadata and controls
1867 lines (1634 loc) · 76.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Variables
# Every default below can be overridden through the environment, for example
# SUFFIX=<unique> PG_APP_PASSWORD=<password> bash deploy.sh
# The App Configuration store and the Key Vault have globally unique names on Azure, so a run against a
# real subscription normally needs its own SUFFIX (or PREFIX).
PREFIX="${PREFIX:-local}"
SUFFIX="${SUFFIX:-test}"
LOCATION="${LOCATION:-westeurope}"
RESOURCE_GROUP_NAME="${PREFIX}-rg"
LOG_ANALYTICS_NAME="${PREFIX}-log-analytics-${SUFFIX}"
DIAGNOSTIC_SETTINGS_NAME='default'
WEB_APP_SUBNET_NSG_NAME="${PREFIX}-webapp-subnet-nsg-${SUFFIX}"
PE_SUBNET_NSG_NAME="${PREFIX}-pe-subnet-nsg-${SUFFIX}"
NAT_GATEWAY_NAME="${PREFIX}-nat-gateway-${SUFFIX}"
PIP_PREFIX_NAME="${PREFIX}-nat-gateway-pip-prefix-${SUFFIX}"
VIRTUAL_NETWORK_NAME="${PREFIX}-vnet-${SUFFIX}"
VIRTUAL_NETWORK_ADDRESS_PREFIX="10.0.0.0/8"
WEB_APP_SUBNET_NAME="app-subnet"
WEB_APP_SUBNET_PREFIX="10.0.0.0/24"
PE_SUBNET_NAME="pe-subnet"
PE_SUBNET_PREFIX="10.0.1.0/24"
VIRTUAL_NETWORK_LINK_NAME="link-to-vnet"
PRIVATE_DNS_ZONE_GROUP_NAME="default"
POSTGRES_PRIVATE_DNS_ZONE_NAME="privatelink.postgres.database.azure.com"
POSTGRES_PRIVATE_ENDPOINT_NAME="${PREFIX}-postgres-pe-${SUFFIX}"
POSTGRES_PRIVATE_ENDPOINT_GROUP="postgresqlServer"
APP_CONFIG_NAME="${PREFIX}-appconfig-${SUFFIX}"
APP_CONFIG_SKU="${APP_CONFIG_SKU:-Standard}"
APP_CONFIG_PRIVATE_DNS_ZONE_NAME="privatelink.azconfig.io"
APP_CONFIG_PRIVATE_ENDPOINT_NAME="${PREFIX}-appconfig-pe-${SUFFIX}"
APP_CONFIG_PRIVATE_ENDPOINT_GROUP="configurationStores"
KEY_VAULT_NAME="${PREFIX}-keyvault-${SUFFIX}"
KEY_VAULT_PRIVATE_DNS_ZONE_NAME="privatelink.vaultcore.azure.net"
KEY_VAULT_PRIVATE_ENDPOINT_NAME="${PREFIX}-keyvault-pe-${SUFFIX}"
KEY_VAULT_PRIVATE_ENDPOINT_GROUP="vault"
KEY_VAULT_REFERENCE_CONTENT_TYPE="application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8"
PG_USER_SECRET_NAME="pg-user"
PG_PASSWORD_SECRET_NAME="pg-password"
MANAGED_IDENTITY_NAME="${PREFIX}-identity-${SUFFIX}"
APP_CONFIG_DATA_READER_ROLE="App Configuration Data Reader"
KEY_VAULT_SECRETS_USER_ROLE="Key Vault Secrets User"
KEY_VAULT_SECRETS_OFFICER_ROLE="Key Vault Secrets Officer"
APP_SERVICE_PLAN_NAME="${PREFIX}-app-service-plan-${SUFFIX}"
APP_SERVICE_PLAN_SKU="S1"
WEB_APP_NAME="${PREFIX}-webapp-${SUFFIX}"
POSTGRES_SERVER_NAME="${PREFIX}-pgflex-${SUFFIX}"
POSTGRES_VERSION="16"
POSTGRES_SKU_NAME="Standard_B1ms"
POSTGRES_SKU_TIER="Burstable"
POSTGRES_STORAGE_SIZE_GB=32
POSTGRES_BACKUP_RETENTION_DAYS=7
POSTGRES_DATABASE_NAME="${PG_DATABASE_NAME:-PlannerDB}"
PG_ADMIN_USER="${PG_ADMIN_USER:-pgadmin}"
PG_ADMIN_PASSWORD="${PG_ADMIN_PASSWORD:-P@ssw0rd1234!}"
PG_APP_USER="${PG_APP_USER:-testuser}"
PG_APP_PASSWORD="${PG_APP_PASSWORD:-TestP@ssw0rd123}"
FIREWALL_RULE_NAME="AllowAllIPs"
RUNTIME="python"
RUNTIME_VERSION="3.13"
LOGIN_NAME="${LOGIN_NAME:-paolo}"
DEPLOY_APP="${DEPLOY_APP:-1}"
ROLE_ASSIGNMENT_RETRY_COUNT=3
ROLE_ASSIGNMENT_RETRY_SLEEP=5
SECRET_RETRY_COUNT=20
SECRET_RETRY_SLEEP=30
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
ZIPFILE="planner_website.zip"
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
#********************************************
# Functions
#********************************************
# Create a private DNS zone in the resource group, unless it already exists.
# $1: the zone name
ensure_private_dns_zone() {
local zone_name=$1
echo "Checking if [$zone_name] private DNS zone actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az network private-dns zone show \
--name "$zone_name" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$zone_name] private DNS zone actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$zone_name] private DNS zone in the [$RESOURCE_GROUP_NAME] resource group..."
az network private-dns zone create \
--name "$zone_name" \
--resource-group "$RESOURCE_GROUP_NAME" \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$zone_name] private DNS zone successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$zone_name] private DNS zone in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$zone_name] private DNS zone already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
}
# Link a private DNS zone to the sample's virtual network, unless the link already exists.
# $1: the zone name
ensure_private_dns_zone_link() {
local zone_name=$1
echo "Checking if [$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$zone_name] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network actually exists..."
az network private-dns link vnet show \
--name "$VIRTUAL_NETWORK_LINK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--zone-name "$zone_name" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$zone_name] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network actually exists"
echo "Creating [$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$zone_name] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network..."
az network private-dns link vnet create \
--name "$VIRTUAL_NETWORK_LINK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--zone-name "$zone_name" \
--virtual-network "$VIRTUAL_NETWORK_ID" \
--registration-enabled false \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$zone_name] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network successfully created"
else
echo "Failed to create [$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$zone_name] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network"
exit 1
fi
else
echo "[$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$zone_name] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network already exists"
fi
}
# Create a private endpoint in the private endpoint subnet for a target resource, unless it already exists.
# $1: the private endpoint name
# $2: the resource id of the target resource
# $3: the group id (sub-resource) of the target resource
# $4: the connection name
# $5: the description of the target resource, used in messages
ensure_private_endpoint() {
local name=$1
local resource_id=$2
local group_id=$3
local connection_name=$4
local description=$5
local private_endpoint_id
echo "Checking if private endpoint [$name] exists in the [$RESOURCE_GROUP_NAME] resource group..."
private_endpoint_id=$(az network private-endpoint list \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors \
--query "[?name=='$name'].id" \
--output tsv)
if [[ -z $private_endpoint_id ]]; then
echo "Private endpoint [$name] does not exist in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$name] private endpoint for the $description in the [$RESOURCE_GROUP_NAME] resource group..."
az network private-endpoint create \
--name "$name" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--vnet-name "$VIRTUAL_NETWORK_NAME" \
--subnet "$PE_SUBNET_NAME" \
--private-connection-resource-id "$resource_id" \
--group-id "$group_id" \
--connection-name "$connection_name" \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "Private endpoint [$name] successfully created for the $description in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create a private endpoint for the $description in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "Private endpoint [$name] already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
}
# Create the private DNS zone group that registers the private endpoint's A record in a zone, unless it already exists.
# $1: the private endpoint name
# $2: the zone name
# $3: the name of the zone configuration inside the group
ensure_private_dns_zone_group() {
local endpoint_name=$1
local zone_name=$2
local zone_config_name=$3
local current_name
echo "Checking if the private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$endpoint_name] private endpoint already exists..."
current_name=$(az network private-endpoint dns-zone-group show \
--resource-group "$RESOURCE_GROUP_NAME" \
--endpoint-name "$endpoint_name" \
--name "$PRIVATE_DNS_ZONE_GROUP_NAME" \
--query name \
--output tsv \
--only-show-errors 2>/dev/null)
if [[ -z $current_name ]]; then
echo "No private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$endpoint_name] private endpoint actually exists"
echo "Creating private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$endpoint_name] private endpoint..."
az network private-endpoint dns-zone-group create \
--name "$PRIVATE_DNS_ZONE_GROUP_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--endpoint-name "$endpoint_name" \
--private-dns-zone "$zone_name" \
--zone-name "$zone_config_name" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "Private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$endpoint_name] private endpoint successfully created"
else
echo "Failed to create private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$endpoint_name] private endpoint"
exit 1
fi
else
echo "Private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$endpoint_name] private endpoint already exists"
fi
}
# Assign a role to a principal at a scope, unless the assignment already exists. The assignee is given as an
# object id together with its principal type, so no directory lookup happens at creation time.
# $1: the object id of the principal
# $2: the principal type (ServicePrincipal, User or Group)
# $3: the role name
# $4: the scope (resource id)
# $5: the description of the principal, used in messages
# $6: the description of the scope, used in messages
ensure_role_assignment() {
local principal_id=$1
local principal_type=$2
local role=$3
local scope=$4
local principal_description=$5
local scope_description=$6
local current attempt
echo "Checking if the $principal_description has the [$role] role assignment on the $scope_description..."
current=$(az role assignment list \
--assignee "$principal_id" \
--scope "$scope" \
--query "[?roleDefinitionName=='$role'].roleDefinitionName" \
--output tsv \
--only-show-errors 2>/dev/null)
if [[ $current == "$role" ]]; then
echo "The $principal_description already has the [$role] role assignment on the $scope_description"
return 0
fi
echo "Assigning the [$role] role to the $principal_description on the $scope_description..."
for attempt in $(seq 1 "$ROLE_ASSIGNMENT_RETRY_COUNT"); do
if az role assignment create \
--assignee-object-id "$principal_id" \
--assignee-principal-type "$principal_type" \
--role "$role" \
--scope "$scope" \
--only-show-errors 1>/dev/null; then
echo "[$role] role successfully assigned to the $principal_description on the $scope_description"
return 0
fi
if [ "$attempt" -lt "$ROLE_ASSIGNMENT_RETRY_COUNT" ]; then
echo "Attempt $attempt of $ROLE_ASSIGNMENT_RETRY_COUNT to assign the [$role] role failed; retrying in $ROLE_ASSIGNMENT_RETRY_SLEEP seconds..."
sleep "$ROLE_ASSIGNMENT_RETRY_SLEEP"
fi
done
echo "Failed to assign the [$role] role to the $principal_description on the $scope_description"
exit 1
}
# Store a secret in the key vault, unless it already holds the same value. The value is never printed.
# Reads and writes go through the Key Vault data plane, which the deploying principal can use only once its
# Key Vault Secrets Officer assignment has propagated, so the write is retried for a few minutes.
# $1: the secret name
# $2: the secret value
set_secret() {
local secret_name=$1
local secret_value=$2
local current attempt
echo "Checking if the [$secret_name] secret actually exists in the [$KEY_VAULT_NAME] key vault..."
current=$(az keyvault secret show \
--vault-name "$KEY_VAULT_NAME" \
--name "$secret_name" \
--query value \
--output tsv \
--only-show-errors 2>/dev/null)
if [[ -n $current && $current == "$secret_value" ]]; then
echo "The [$secret_name] secret already holds the expected value in the [$KEY_VAULT_NAME] key vault"
return 0
fi
echo "Setting the [$secret_name] secret in the [$KEY_VAULT_NAME] key vault..."
for attempt in $(seq 1 "$SECRET_RETRY_COUNT"); do
if az keyvault secret set \
--vault-name "$KEY_VAULT_NAME" \
--name "$secret_name" \
--value "$secret_value" \
--only-show-errors 1>/dev/null; then
echo "The [$secret_name] secret was successfully set in the [$KEY_VAULT_NAME] key vault"
return 0
fi
if [ "$attempt" -lt "$SECRET_RETRY_COUNT" ]; then
echo "Attempt $attempt of $SECRET_RETRY_COUNT to set the [$secret_name] secret failed (the [$KEY_VAULT_SECRETS_OFFICER_ROLE] assignment may still be propagating); retrying in $SECRET_RETRY_SLEEP seconds..."
sleep "$SECRET_RETRY_SLEEP"
fi
done
echo "Failed to set the [$secret_name] secret in the [$KEY_VAULT_NAME] key vault"
exit 1
}
# Set a plain key-value in the App Configuration store, unless it already holds the same value.
# $1: the key
# $2: the value
set_key_value() {
local key=$1
local value=$2
local current
echo "Checking if the [$key] key actually exists in the [$APP_CONFIG_NAME] App Configuration store..."
current=$(az appconfig kv show \
--name "$APP_CONFIG_NAME" \
--key "$key" \
--query value \
--output tsv \
--only-show-errors 2>/dev/null)
if [[ $? == 0 && $current == "$value" ]]; then
echo "The [$key] key already holds the [$value] value in the [$APP_CONFIG_NAME] App Configuration store"
return 0
fi
echo "Setting the [$key] key to the [$value] value in the [$APP_CONFIG_NAME] App Configuration store..."
az appconfig kv set \
--name "$APP_CONFIG_NAME" \
--key "$key" \
--value "$value" \
--yes \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "The [$key] key was successfully set to the [$value] value"
else
echo "Failed to set the [$key] key in the [$APP_CONFIG_NAME] App Configuration store"
exit 1
fi
}
# Set a Key Vault reference in the App Configuration store, unless it already points at the same secret.
# A Key Vault reference is a key-value whose content type is
# application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8 and whose value is {"uri":"<secret identifier>"}.
# The identifier carries no version, so the reference always follows the latest version of the secret.
# $1: the key
# $2: the secret identifier
set_key_vault_reference() {
local key=$1
local secret_identifier=$2
local current
echo "Checking if the [$key] key actually exists in the [$APP_CONFIG_NAME] App Configuration store..."
current=$(az appconfig kv show \
--name "$APP_CONFIG_NAME" \
--key "$key" \
--query "join('|', [contentType, value])" \
--output tsv \
--only-show-errors 2>/dev/null)
if [[ $current == "$KEY_VAULT_REFERENCE_CONTENT_TYPE|"*"\"$secret_identifier\""* ]]; then
echo "The [$key] key already references the [$secret_identifier] secret"
return 0
fi
echo "Setting the [$key] key as a reference to the [$secret_identifier] secret..."
az appconfig kv set-keyvault \
--name "$APP_CONFIG_NAME" \
--key "$key" \
--secret-identifier "$secret_identifier" \
--yes \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "The [$key] key was successfully set as a reference to the [$secret_identifier] secret"
else
echo "Failed to set the [$key] key in the [$APP_CONFIG_NAME] App Configuration store"
exit 1
fi
}
#********************************************
# Resource group
#********************************************
# Create a resource group
echo "Creating resource group [$RESOURCE_GROUP_NAME]..."
az group create \
--name "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Resource group [$RESOURCE_GROUP_NAME] created successfully."
else
echo "Failed to create resource group [$RESOURCE_GROUP_NAME]."
exit 1
fi
#********************************************
# User-assigned managed identity
#********************************************
# Check if the user-assigned managed identity already exists
echo "Checking if [$MANAGED_IDENTITY_NAME] user-assigned managed identity actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az identity show \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$MANAGED_IDENTITY_NAME] user-assigned managed identity actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$MANAGED_IDENTITY_NAME] user-assigned managed identity in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the user-assigned managed identity the web app uses to reach App Configuration and Key Vault
az identity create \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$MANAGED_IDENTITY_NAME] user-assigned managed identity successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$MANAGED_IDENTITY_NAME] user-assigned managed identity in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$MANAGED_IDENTITY_NAME] user-assigned managed identity already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Retrieve the clientId, principalId and resource id of the user-assigned managed identity
echo "Retrieving the clientId, principalId and resource id of the [$MANAGED_IDENTITY_NAME] managed identity..."
mapfile -t IDENTITY_FIELDS < <(az identity show \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "[clientId, principalId, id]" \
--output tsv \
--only-show-errors)
IDENTITY_CLIENT_ID="${IDENTITY_FIELDS[0]:-}"
IDENTITY_PRINCIPAL_ID="${IDENTITY_FIELDS[1]:-}"
IDENTITY_ID="${IDENTITY_FIELDS[2]:-}"
if [[ -n $IDENTITY_CLIENT_ID && -n $IDENTITY_PRINCIPAL_ID && -n $IDENTITY_ID ]]; then
echo "[$MANAGED_IDENTITY_NAME] managed identity: clientId [$IDENTITY_CLIENT_ID], principalId [$IDENTITY_PRINCIPAL_ID]"
else
echo "Failed to retrieve the clientId, principalId and resource id of the [$MANAGED_IDENTITY_NAME] managed identity"
exit 1
fi
#********************************************
# App Configuration store
#********************************************
# Recover the App Configuration store if a previous run left it soft-deleted.
# Deleting a Standard store only soft-deletes it and the name stays reserved for the retention period, so
# re-running this script after deleting the resource group would fail the create below.
DELETED_APP_CONFIG_LOCATION=$(az appconfig list-deleted \
--query "[?name=='$APP_CONFIG_NAME'].location | [0]" \
--output tsv \
--only-show-errors 2>/dev/null)
if [[ -n $DELETED_APP_CONFIG_LOCATION ]]; then
echo "[$APP_CONFIG_NAME] App Configuration store exists in a soft-deleted state in [$DELETED_APP_CONFIG_LOCATION]"
echo "Recovering the [$APP_CONFIG_NAME] App Configuration store..."
az appconfig recover \
--name "$APP_CONFIG_NAME" \
--location "$DELETED_APP_CONFIG_LOCATION" \
--yes \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$APP_CONFIG_NAME] App Configuration store successfully recovered"
else
echo "Failed to recover the soft-deleted [$APP_CONFIG_NAME] App Configuration store"
echo "Purge it and re-run this script:"
echo " az appconfig purge --name $APP_CONFIG_NAME --location $DELETED_APP_CONFIG_LOCATION --yes"
exit 1
fi
fi
# Check if the App Configuration store already exists
echo "Checking if [$APP_CONFIG_NAME] App Configuration store actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az appconfig show \
--name "$APP_CONFIG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$APP_CONFIG_NAME] App Configuration store actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$APP_CONFIG_NAME] App Configuration store in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the App Configuration store. Access keys stay enabled (the default): the az appconfig kv commands
# below authenticate with them, and the Bicep and Terraform variants write key-values through Azure Resource
# Manager, which in the default Local authentication mode also relies on them.
# Public network access is enabled explicitly: when the property is left unspecified, Azure disables it as
# soon as the store gets a private endpoint, and the data-plane calls of this script and of validate.sh
# (az appconfig kv ...) come from outside the virtual network. The web app reaches the store through the
# private endpoint either way.
az appconfig create \
--name "$APP_CONFIG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--sku "$APP_CONFIG_SKU" \
--enable-public-network true \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$APP_CONFIG_NAME] App Configuration store successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$APP_CONFIG_NAME] App Configuration store in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$APP_CONFIG_NAME] App Configuration store already exists in the [$RESOURCE_GROUP_NAME] resource group"
# Make sure public network access stays enabled on an existing store (see the comment above)
APP_CONFIG_PUBLIC_ACCESS=$(az appconfig show \
--name "$APP_CONFIG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query publicNetworkAccess \
--output tsv \
--only-show-errors)
if [[ $APP_CONFIG_PUBLIC_ACCESS != "Enabled" ]]; then
echo "Enabling public network access on the [$APP_CONFIG_NAME] App Configuration store (currently [$APP_CONFIG_PUBLIC_ACCESS])..."
az appconfig update \
--name "$APP_CONFIG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--enable-public-network true \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "Public network access enabled on the [$APP_CONFIG_NAME] App Configuration store"
else
echo "Failed to enable public network access on the [$APP_CONFIG_NAME] App Configuration store"
exit 1
fi
fi
fi
# Retrieve the endpoint and the resource id of the App Configuration store. The endpoint is read back from
# the service and used verbatim by the web app: https://<store>.azconfig.io on Azure,
# https://<store>.azure.localhost.localstack.cloud:4566 on the emulator.
echo "Retrieving the endpoint and the resource id of the [$APP_CONFIG_NAME] App Configuration store..."
mapfile -t APP_CONFIG_FIELDS < <(az appconfig show \
--name "$APP_CONFIG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "[endpoint, id]" \
--output tsv \
--only-show-errors)
APP_CONFIG_ENDPOINT="${APP_CONFIG_FIELDS[0]:-}"
APP_CONFIG_ID="${APP_CONFIG_FIELDS[1]:-}"
if [[ -n $APP_CONFIG_ENDPOINT && -n $APP_CONFIG_ID ]]; then
echo "[$APP_CONFIG_NAME] App Configuration store endpoint: $APP_CONFIG_ENDPOINT"
else
echo "Failed to retrieve the endpoint and the resource id of the [$APP_CONFIG_NAME] App Configuration store"
exit 1
fi
#********************************************
# Key Vault
#********************************************
# Recover the key vault if a previous run left it soft-deleted.
# Deleting a key vault only soft-deletes it, and the name stays reserved for the retention period, so
# re-running this script after deleting the resource group fails the create below with
# "A vault with the same name already exists in deleted state". Recovering restores the vault with its
# contents, which is what a re-run wants; purging would throw them away.
# The vault's own location, not $LOCATION: a soft-deleted vault stays in the region it was deleted in,
# so recovering (or purging) it with a different region fails.
DELETED_KEY_VAULT_LOCATION=$(az keyvault list-deleted \
--query "[?name=='$KEY_VAULT_NAME'].properties.location | [0]" \
--output tsv \
--only-show-errors 2>/dev/null)
if [[ -n $DELETED_KEY_VAULT_LOCATION ]]; then
echo "[$KEY_VAULT_NAME] key vault exists in a soft-deleted state in [$DELETED_KEY_VAULT_LOCATION]"
echo "Recovering the [$KEY_VAULT_NAME] key vault..."
az keyvault recover \
--name "$KEY_VAULT_NAME" \
--location "$DELETED_KEY_VAULT_LOCATION" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$KEY_VAULT_NAME] key vault successfully recovered"
else
echo "Failed to recover the soft-deleted [$KEY_VAULT_NAME] key vault"
echo "Purge it and re-run this script:"
echo " az keyvault purge --name $KEY_VAULT_NAME --location $DELETED_KEY_VAULT_LOCATION"
exit 1
fi
fi
# Check if the key vault already exists
echo "Checking if [$KEY_VAULT_NAME] key vault actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az keyvault show \
--name "$KEY_VAULT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$KEY_VAULT_NAME] key vault actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$KEY_VAULT_NAME] key vault in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the key vault with the Azure RBAC permission model: the Key Vault Secrets User role assigned to
# the managed identity below only works on vaults that use it.
az keyvault create \
--name "$KEY_VAULT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--enable-rbac-authorization true \
--retention-days 7 \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$KEY_VAULT_NAME] key vault successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$KEY_VAULT_NAME] key vault in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$KEY_VAULT_NAME] key vault already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Retrieve the vault URI and the resource id of the key vault. The URI is read back from the service:
# https://<vault>.vault.azure.net/ on Azure, https://<vault>.vault.azure.localhost.localstack.cloud:4566/ on
# the emulator.
echo "Retrieving the vault URI and the resource id of the [$KEY_VAULT_NAME] key vault..."
mapfile -t KEY_VAULT_FIELDS < <(az keyvault show \
--name "$KEY_VAULT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "[properties.vaultUri, id]" \
--output tsv \
--only-show-errors)
KEY_VAULT_URI="${KEY_VAULT_FIELDS[0]:-}"
KEY_VAULT_ID="${KEY_VAULT_FIELDS[1]:-}"
if [[ -n $KEY_VAULT_URI && -n $KEY_VAULT_ID ]]; then
echo "[$KEY_VAULT_NAME] key vault URI: $KEY_VAULT_URI"
else
echo "Failed to retrieve the vault URI and the resource id of the [$KEY_VAULT_NAME] key vault"
exit 1
fi
# Resolve the object id of the deploying principal. The vault uses the RBAC permission model, so writing
# the two secrets below needs the Key Vault Secrets Officer role on the vault; the script grants it to
# whoever runs it, a user or a service principal. The lookup goes through Microsoft Graph; when it fails
# the script continues and the role becomes a prerequisite (see the README).
ACCOUNT_USER_TYPE=$(az account show --query user.type --output tsv --only-show-errors)
ACCOUNT_USER_NAME=$(az account show --query user.name --output tsv --only-show-errors)
if [[ $ACCOUNT_USER_TYPE == "user" ]]; then
DEPLOYER_PRINCIPAL_TYPE="User"
DEPLOYER_OBJECT_ID=$(az ad signed-in-user show --query id --output tsv --only-show-errors 2>/dev/null)
else
DEPLOYER_PRINCIPAL_TYPE="ServicePrincipal"
DEPLOYER_OBJECT_ID=$(az ad sp show --id "$ACCOUNT_USER_NAME" --query id --output tsv --only-show-errors 2>/dev/null)
fi
# Microsoft Graph lookups need directory permissions a pipeline principal often lacks. Fall back to the oid
# claim of the CLI's own access token, which identifies the same principal without any Graph call.
if [[ -z $DEPLOYER_OBJECT_ID ]]; then
DEPLOYER_OBJECT_ID=$(az account get-access-token --query accessToken --output tsv --only-show-errors 2>/dev/null |
cut -d. -f2 | tr '_-' '/+' | awk '{ pad = length($0) % 4; if (pad == 2) $0 = $0 "=="; else if (pad == 3) $0 = $0 "="; print }' |
base64 -d 2>/dev/null | jq -r '.oid // empty' 2>/dev/null)
fi
if [[ -n $DEPLOYER_OBJECT_ID ]]; then
echo "Deploying principal [$ACCOUNT_USER_NAME] ($DEPLOYER_PRINCIPAL_TYPE) has object id [$DEPLOYER_OBJECT_ID]"
ensure_role_assignment "$DEPLOYER_OBJECT_ID" "$DEPLOYER_PRINCIPAL_TYPE" "$KEY_VAULT_SECRETS_OFFICER_ROLE" "$KEY_VAULT_ID" "deploying principal [$ACCOUNT_USER_NAME]" "[$KEY_VAULT_NAME] key vault"
else
echo "WARNING: could not resolve the object id of the deploying principal [$ACCOUNT_USER_NAME]; the [$KEY_VAULT_SECRETS_OFFICER_ROLE] role on the [$KEY_VAULT_NAME] key vault must already be assigned to it"
fi
# Store the application role credentials as secrets. The web app never sees these values directly: it
# reads them through the App Configuration Key Vault references created further below.
set_secret "$PG_USER_SECRET_NAME" "$PG_APP_USER"
set_secret "$PG_PASSWORD_SECRET_NAME" "$PG_APP_PASSWORD"
PG_USER_SECRET_URI="${KEY_VAULT_URI%/}/secrets/${PG_USER_SECRET_NAME}"
PG_PASSWORD_SECRET_URI="${KEY_VAULT_URI%/}/secrets/${PG_PASSWORD_SECRET_NAME}"
#********************************************
# Role assignments of the managed identity
#********************************************
# The web app authenticates to both stores with the user-assigned managed identity: App Configuration Data
# Reader to read the key-values, Key Vault Secrets User to read the secrets behind the Key Vault references.
ensure_role_assignment "$IDENTITY_PRINCIPAL_ID" "ServicePrincipal" "$APP_CONFIG_DATA_READER_ROLE" "$APP_CONFIG_ID" "[$MANAGED_IDENTITY_NAME] managed identity" "[$APP_CONFIG_NAME] App Configuration store"
ensure_role_assignment "$IDENTITY_PRINCIPAL_ID" "ServicePrincipal" "$KEY_VAULT_SECRETS_USER_ROLE" "$KEY_VAULT_ID" "[$MANAGED_IDENTITY_NAME] managed identity" "[$KEY_VAULT_NAME] key vault"
#********************************************
# PostgreSQL flexible server
#********************************************
# Check if the PostgreSQL flexible server already exists
echo "Checking if [$POSTGRES_SERVER_NAME] PostgreSQL flexible server already exists in the [$RESOURCE_GROUP_NAME] resource group..."
az postgres flexible-server show \
--name "$POSTGRES_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$POSTGRES_SERVER_NAME] PostgreSQL flexible server already exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$POSTGRES_SERVER_NAME] PostgreSQL flexible server in the [$RESOURCE_GROUP_NAME] resource group..."
# Create a PostgreSQL flexible server with public network access
az postgres flexible-server create \
--name "$POSTGRES_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--tier "$POSTGRES_SKU_TIER" \
--sku-name "$POSTGRES_SKU_NAME" \
--version "$POSTGRES_VERSION" \
--storage-size "$POSTGRES_STORAGE_SIZE_GB" \
--backup-retention "$POSTGRES_BACKUP_RETENTION_DAYS" \
--geo-redundant-backup Disabled \
--admin-user "$PG_ADMIN_USER" \
--admin-password "$PG_ADMIN_PASSWORD" \
--public-access Enabled \
--zonal-resiliency Disabled \
--yes \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "[$POSTGRES_SERVER_NAME] PostgreSQL flexible server successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$POSTGRES_SERVER_NAME] PostgreSQL flexible server in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$POSTGRES_SERVER_NAME] PostgreSQL flexible server already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Retrieve the resource id of the PostgreSQL flexible server
echo "Getting [$POSTGRES_SERVER_NAME] PostgreSQL flexible server resource id in the [$RESOURCE_GROUP_NAME] resource group..."
POSTGRES_SERVER_ID=$(az postgres flexible-server show \
--name "$POSTGRES_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv \
--only-show-errors)
if [ -n "$POSTGRES_SERVER_ID" ]; then
echo "PostgreSQL flexible server resource id retrieved successfully: $POSTGRES_SERVER_ID"
else
echo "Failed to retrieve PostgreSQL flexible server resource id."
exit 1
fi
# Retrieve the fullyQualifiedDomainName of the PostgreSQL flexible server
echo "Getting [$POSTGRES_SERVER_NAME] PostgreSQL flexible server FQDN in the [$RESOURCE_GROUP_NAME] resource group..."
POSTGRES_FQDN_FULL=$(az postgres flexible-server show \
--name "$POSTGRES_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "fullyQualifiedDomainName" \
--output tsv \
--only-show-errors)
if [ -n "$POSTGRES_FQDN_FULL" ]; then
echo "PostgreSQL flexible server FQDN retrieved successfully: $POSTGRES_FQDN_FULL"
else
echo "Failed to retrieve PostgreSQL flexible server FQDN."
exit 1
fi
# Split host:port: the LocalStack emulator embeds the dynamically allocated TCP-proxy port
# directly in fullyQualifiedDomainName, mirroring the storage / container registry emulators.
# Real Azure returns just the bare host so PG_PORT defaults to 5432.
POSTGRES_FQDN="${POSTGRES_FQDN_FULL%%:*}"
if [[ "$POSTGRES_FQDN_FULL" == *:* ]]; then
POSTGRES_PORT="${POSTGRES_FQDN_FULL##*:}"
else
POSTGRES_PORT=5432
fi
echo "PostgreSQL host = $POSTGRES_FQDN, port = $POSTGRES_PORT"
# Check if the server-level firewall rule already exists
echo "Checking if [$FIREWALL_RULE_NAME] firewall rule already exists on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server..."
az postgres flexible-server firewall-rule show \
--server-name "$POSTGRES_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$FIREWALL_RULE_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$FIREWALL_RULE_NAME] firewall rule already exists on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server"
echo "Creating [$FIREWALL_RULE_NAME] firewall rule on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server..."
# Create a permissive firewall rule so the deploy machine can run the psql bootstrap.
# The create is retried because this PUT intermittently answers 500 against the emulator while
# the server finishes provisioning, and the Azure CLI's own retries all land within a few seconds.
FIREWALL_RULE_CREATED=0
for attempt in $(seq 1 5); do
if az postgres flexible-server firewall-rule create \
--server-name "$POSTGRES_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$FIREWALL_RULE_NAME" \
--start-ip-address "0.0.0.0" \
--end-ip-address "255.255.255.255" \
--only-show-errors 1>/dev/null; then
FIREWALL_RULE_CREATED=1
break
fi
if [ "$attempt" -lt 5 ]; then
echo "Attempt $attempt of 5 to create the [$FIREWALL_RULE_NAME] firewall rule failed; retrying in 10 seconds..."
sleep 10
fi
done
if [ $FIREWALL_RULE_CREATED -eq 1 ]; then
echo "[$FIREWALL_RULE_NAME] firewall rule successfully created on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server"
else
# Not fatal: the rule governs public network access, which the emulator does not enforce, and
# the psql bootstrap below fails loudly if the server is genuinely unreachable.
echo "WARNING: could not create the [$FIREWALL_RULE_NAME] firewall rule on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server; continuing"
fi
else
echo "[$FIREWALL_RULE_NAME] firewall rule already exists on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server"
fi
# Check if the PostgreSQL database already exists
echo "Checking if [$POSTGRES_DATABASE_NAME] database already exists on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server..."
az postgres flexible-server db show \
--server-name "$POSTGRES_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$POSTGRES_DATABASE_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$POSTGRES_DATABASE_NAME] database already exists on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server"
echo "Creating [$POSTGRES_DATABASE_NAME] database on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server..."
# Create the application database
az postgres flexible-server db create \
--server-name "$POSTGRES_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$POSTGRES_DATABASE_NAME" \
--charset UTF8 \
--collation en_US.utf8 \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "[$POSTGRES_DATABASE_NAME] database successfully created on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server"
else
echo "Failed to create [$POSTGRES_DATABASE_NAME] database on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server"
exit 1
fi
else
echo "[$POSTGRES_DATABASE_NAME] database already exists on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server"
fi
#********************************************
# App Configuration key-values
#********************************************
# The connection settings the web app used to receive as app settings now live in the store: the host, port
# and database name as plain key-values, the application role credentials as Key Vault references to the
# secrets stored above. The keys keep the names of the former environment variables and carry no label.
set_key_value "PG_HOST" "$POSTGRES_FQDN"
set_key_value "PG_PORT" "$POSTGRES_PORT"
set_key_value "PG_DATABASE" "$POSTGRES_DATABASE_NAME"
set_key_vault_reference "PG_USER" "$PG_USER_SECRET_URI"
set_key_vault_reference "PG_PASSWORD" "$PG_PASSWORD_SECRET_URI"
#********************************************
# Networking
#********************************************
# Check if the network security group for the web app subnet already exists
echo "Checking if [$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az network nsg show \
--name "$WEB_APP_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet..."
# Create the network security group for the web app subnet
az network nsg create \
--name "$WEB_APP_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Get the resource id of the network security group for the web app subnet
echo "Getting [$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet resource id in the [$RESOURCE_GROUP_NAME] resource group..."
WEB_APP_SUBNET_NSG_ID=$(az network nsg show \
--name "$WEB_APP_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv \
--only-show-errors)
if [[ -n $WEB_APP_SUBNET_NSG_ID ]]; then
echo "[$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet resource id retrieved successfully: $WEB_APP_SUBNET_NSG_ID"
else
echo "Failed to retrieve [$WEB_APP_SUBNET_NSG_NAME] network security group for the web app subnet resource id in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
# Check if the network security group for the private endpoint subnet already exists
echo "Checking if [$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az network nsg show \
--name "$PE_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet..."
# Create the network security group for the private endpoint subnet
az network nsg create \
--name "$PE_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--tags environment=test iac=az-cli \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Get the resource id of the network security group for the private endpoint subnet
echo "Getting [$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet resource id in the [$RESOURCE_GROUP_NAME] resource group..."
PE_SUBNET_NSG_ID=$(az network nsg show \
--name "$PE_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv \
--only-show-errors)
if [[ -n $PE_SUBNET_NSG_ID ]]; then
echo "[$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet resource id retrieved successfully: $PE_SUBNET_NSG_ID"
else
echo "Failed to retrieve [$PE_SUBNET_NSG_NAME] network security group for the private endpoint subnet resource id in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
# Check if the public IP prefix for the NAT Gateway already exists
echo "Checking if [$PIP_PREFIX_NAME] public IP prefix for the NAT Gateway actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az network public-ip prefix show \
--name "$PIP_PREFIX_NAME" \