-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·1460 lines (1286 loc) · 59.7 KB
/
deploy.sh
File metadata and controls
executable file
·1460 lines (1286 loc) · 59.7 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
PREFIX='local'
SUFFIX='test'
LOCATION='westeurope'
RESOURCE_GROUP_NAME="${PREFIX}-rg"
LOG_ANALYTICS_NAME="${PREFIX}-log-analytics-${SUFFIX}"
DIAGNOSTIC_SETTINGS_NAME='default'
MANAGED_IDENTITY_NAME="${PREFIX}-identity-${SUFFIX}"
FUNCTION_APP_SUBNET_NSG_NAME="${PREFIX}-func-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"
FUNCTION_APP_SUBNET_NAME="func-subnet"
FUNCTION_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"
APPLICATION_INSIGHTS_NAME="${PREFIX}-func-${SUFFIX}"
APP_SERVICE_PLAN_NAME="${PREFIX}-plan-${SUFFIX}"
FUNCTION_APP_NAME="${PREFIX}-func-${SUFFIX}"
STORAGE_ACCOUNT_NAME="${PREFIX}storage${SUFFIX}"
SERVICE_BUS_NAMESPACE="${PREFIX}-service-bus-${SUFFIX}"
FUNCTIONS_VERSION="4"
RUNTIME="DOTNET-ISOLATED"
RUNTIME_VERSION="10"
SERVICE_BUS_CONNECTION_STRING=''
INPUT_QUEUE_NAME="input"
OUTPUT_QUEUE_NAME="output"
TAGS='environment=test deployment=azcli'
SUBSCRIPTION_NAME=$(az account show --query name --output tsv)
DEPLOY=1
RETRY_COUNT=3
SLEEP=5
ZIPFILE="functionapp.zip"
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
PRIVATE_DNS_ZONE_NAMES=(
"privatelink.servicebus.windows.net"
"privatelink.blob.core.windows.net"
"privatelink.queue.core.windows.net"
"privatelink.table.core.windows.net"
)
PE_NAMES=(
"${PREFIX}-service-bus-pe-${SUFFIX}"
"${PREFIX}-blob-storage-pe-${SUFFIX}"
"${PREFIX}-queue-storage-pe-${SUFFIX}"
"${PREFIX}-table-storage-pe-${SUFFIX}"
)
PE_GROUP_IDS=("namespace" "blob" "queue" "table")
PE_CONNECTION_NAMES=("servicebus-connection" "blob-connection" "queue-connection" "table-connection")
PE_DNS_ZONES=("privatelink.servicebus.windows.net" "privatelink.blob.core.windows.net" "privatelink.queue.core.windows.net" "privatelink.table.core.windows.net")
PE_DNS_ZONE_LABELS=("servicebus-zone" "blob-zone" "queue-zone" "table-zone")
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Create a resource group
echo "Checking if resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]..."
az group show --name $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]"
echo "Creating resource group [$RESOURCE_GROUP_NAME] in the subscription [$SUBSCRIPTION_NAME]..."
# Create the resource group
az group create \
--name $RESOURCE_GROUP_NAME \
--location $LOCATION \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "Resource group [$RESOURCE_GROUP_NAME] successfully created in the subscription [$SUBSCRIPTION_NAME]"
else
echo "Failed to create resource group [$RESOURCE_GROUP_NAME] in the subscription [$SUBSCRIPTION_NAME]"
exit
fi
else
echo "Resource group [$RESOURCE_GROUP_NAME] already exists in the subscription [$SUBSCRIPTION_NAME]"
fi
# Create a service bus namespace
echo "Checking if [$SERVICE_BUS_NAMESPACE] service bus namespace exists in the [$RESOURCE_GROUP_NAME] resource group..."
az servicebus namespace show \
--name $SERVICE_BUS_NAMESPACE \
--resource-group $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$SERVICE_BUS_NAMESPACE] service bus namespace exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$SERVICE_BUS_NAMESPACE] service bus namespace in the [$RESOURCE_GROUP_NAME] resource group..."
az servicebus namespace create \
--name $SERVICE_BUS_NAMESPACE \
--sku Premium \
--location $LOCATION \
--resource-group $RESOURCE_GROUP_NAME \
--tags $TAGS 1>/dev/null
if [ $? == 0 ]; then
echo "[$SERVICE_BUS_NAMESPACE] service bus namespace successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$SERVICE_BUS_NAMESPACE] service bus namespace in the [$RESOURCE_GROUP_NAME] resource group"
exit
fi
else
echo "[$SERVICE_BUS_NAMESPACE] service bus namespace already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Create the service bus input queue
echo "Checking if [$INPUT_QUEUE_NAME] service bus queue exists in the [$SERVICE_BUS_NAMESPACE] service bus namespace..."
az servicebus queue show \
--name $INPUT_QUEUE_NAME \
--namespace-name $SERVICE_BUS_NAMESPACE \
--resource-group $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$INPUT_QUEUE_NAME] service bus queue exists in the [$SERVICE_BUS_NAMESPACE] service bus namespace"
echo "Creating [$INPUT_QUEUE_NAME] service bus queue in the [$SERVICE_BUS_NAMESPACE] service bus namespace..."
az servicebus queue create \
--name $INPUT_QUEUE_NAME \
--namespace-name $SERVICE_BUS_NAMESPACE \
--resource-group $RESOURCE_GROUP_NAME 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$INPUT_QUEUE_NAME] service bus queue successfully created in the [$SERVICE_BUS_NAMESPACE] service bus namespace"
else
echo "Failed to create [$INPUT_QUEUE_NAME] service bus queue in the [$SERVICE_BUS_NAMESPACE] service bus namespace"
exit
fi
else
echo "[$INPUT_QUEUE_NAME] service bus queue already exists in the [$SERVICE_BUS_NAMESPACE] service bus namespace"
fi
# Create the service bus output queue
echo "Checking if [$OUTPUT_QUEUE_NAME] service bus queue exists in the [$SERVICE_BUS_NAMESPACE] service bus namespace..."
az servicebus queue show \
--name $OUTPUT_QUEUE_NAME \
--namespace-name $SERVICE_BUS_NAMESPACE \
--resource-group $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$OUTPUT_QUEUE_NAME] service bus queue exists in the [$SERVICE_BUS_NAMESPACE] service bus namespace"
echo "Creating [$OUTPUT_QUEUE_NAME] service bus queue in the [$SERVICE_BUS_NAMESPACE] service bus namespace..."
az servicebus queue create \
--name $OUTPUT_QUEUE_NAME \
--namespace-name $SERVICE_BUS_NAMESPACE \
--resource-group $RESOURCE_GROUP_NAME 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$OUTPUT_QUEUE_NAME] service bus queue successfully created in the [$SERVICE_BUS_NAMESPACE] service bus namespace"
else
echo "Failed to create [$OUTPUT_QUEUE_NAME] service bus queue in the [$SERVICE_BUS_NAMESPACE] service bus namespace"
exit
fi
else
echo "[$OUTPUT_QUEUE_NAME] service bus queue already exists in the [$SERVICE_BUS_NAMESPACE] service bus namespace"
fi
# Retrieve and display connection string
SERVICE_BUS_CONNECTION_STRING=$(az servicebus namespace authorization-rule keys list \
--name RootManageSharedAccessKey \
--namespace-name $SERVICE_BUS_NAMESPACE \
--resource-group $RESOURCE_GROUP_NAME \
--query primaryConnectionString \
--output tsv)
echo "Service Bus connection string: $SERVICE_BUS_CONNECTION_STRING"
# Get the Service Bus namespace resource id
echo "Getting [$SERVICE_BUS_NAMESPACE] service bus namespace resource id in the [$RESOURCE_GROUP_NAME] resource group..."
SERVICE_BUS_NAMESPACE_ID=$(az servicebus namespace show \
--name $SERVICE_BUS_NAMESPACE \
--resource-group $RESOURCE_GROUP_NAME \
--query id \
--output tsv)
if [[ -n $SERVICE_BUS_NAMESPACE_ID ]]; then
echo "[$SERVICE_BUS_NAMESPACE] service bus namespace resource id retrieved successfully: $SERVICE_BUS_NAMESPACE_ID"
else
echo "Failed to retrieve [$SERVICE_BUS_NAMESPACE] service bus namespace resource id in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
# 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" &>/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
az identity create \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--tags $TAGS 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 of the user-assigned managed identity
echo "Retrieving clientId for [$MANAGED_IDENTITY_NAME] managed identity..."
CLIENT_ID=$(az identity show \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query clientId \
--output tsv)
if [[ -n $CLIENT_ID ]]; then
echo "[$CLIENT_ID] clientId for the [$MANAGED_IDENTITY_NAME] managed identity successfully retrieved"
else
echo "Failed to retrieve clientId for the [$MANAGED_IDENTITY_NAME] managed identity"
exit 1
fi
# Retrieve the principalId of the user-assigned managed identity
echo "Retrieving principalId for [$MANAGED_IDENTITY_NAME] managed identity..."
PRINCIPAL_ID=$(az identity show \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query principalId \
--output tsv)
if [[ -n $PRINCIPAL_ID ]]; then
echo "[$PRINCIPAL_ID] principalId for the [$MANAGED_IDENTITY_NAME] managed identity successfully retrieved"
else
echo "Failed to retrieve principalId for the [$MANAGED_IDENTITY_NAME] managed identity"
exit 1
fi
# Retrieve the resource id of the user-assigned managed identity
echo "Retrieving resource id for the [$MANAGED_IDENTITY_NAME] managed identity..."
IDENTITY_ID=$(az identity show \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv)
if [[ -n $IDENTITY_ID ]]; then
echo "Resource id for the [$MANAGED_IDENTITY_NAME] managed identity successfully retrieved"
else
echo "Failed to retrieve the resource id for the [$MANAGED_IDENTITY_NAME] managed identity"
exit 1
fi
# Check if the network security group for the function app subnet already exists
echo "Checking if [$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function app subnet actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az network nsg show \
--name "$FUNCTION_APP_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function app subnet actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function app subnet..."
# Create the network security group for the function app subnet
az network nsg create \
--name "$FUNCTION_APP_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function app subnet successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function app subnet in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function app subnet already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Get the resource id of the network security group for the function app subnet
echo "Getting [$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function app subnet resource id in the [$RESOURCE_GROUP_NAME] resource group..."
FUNCTION_APP_SUBNET_NSG_ID=$(az network nsg show \
--name "$FUNCTION_APP_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv \
--only-show-errors)
if [[ -n $FUNCTION_APP_SUBNET_NSG_ID ]]; then
echo "[$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function app subnet resource id retrieved successfully: $FUNCTION_APP_SUBNET_NSG_ID"
else
echo "Failed to retrieve [$FUNCTION_APP_SUBNET_NSG_NAME] network security group for the function 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 $TAGS \
--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" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$PIP_PREFIX_NAME] public IP prefix for the NAT Gateway actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$PIP_PREFIX_NAME] public IP prefix for the NAT Gateway in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the public IP prefix for the NAT Gateway
az network public-ip prefix create \
--name "$PIP_PREFIX_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--length 31 \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$PIP_PREFIX_NAME] public IP prefix for the NAT Gateway successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$PIP_PREFIX_NAME] public IP prefix for the NAT Gateway in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$PIP_PREFIX_NAME] public IP prefix for the NAT Gateway already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Check if the NAT Gateway already exists
echo "Checking if [$NAT_GATEWAY_NAME] NAT Gateway actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az network nat gateway show \
--name "$NAT_GATEWAY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$NAT_GATEWAY_NAME] NAT Gateway actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$NAT_GATEWAY_NAME] NAT Gateway in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the NAT Gateway
az network nat gateway create \
--name "$NAT_GATEWAY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--public-ip-prefixes "$PIP_PREFIX_NAME" \
--idle-timeout 4 \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$NAT_GATEWAY_NAME] NAT Gateway successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$NAT_GATEWAY_NAME] NAT Gateway in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$NAT_GATEWAY_NAME] NAT Gateway already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Check if the virtual network already exists
echo "Checking if [$VIRTUAL_NETWORK_NAME] virtual network actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az network vnet show \
--name "$VIRTUAL_NETWORK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$VIRTUAL_NETWORK_NAME] virtual network actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$VIRTUAL_NETWORK_NAME] virtual network in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the virtual network
az network vnet create \
--name "$VIRTUAL_NETWORK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--address-prefixes "$VIRTUAL_NETWORK_ADDRESS_PREFIX" \
--subnet-name "$FUNCTION_APP_SUBNET_NAME" \
--subnet-prefix "$FUNCTION_APP_SUBNET_PREFIX" \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$VIRTUAL_NETWORK_NAME] virtual network successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$VIRTUAL_NETWORK_NAME] virtual network in the [$RESOURCE_GROUP_NAME] resource group"
exit
fi
# Update the function app subnet to associate it with the NAT Gateway and the NSG
echo "Associating [$FUNCTION_APP_SUBNET_NAME] subnet with the [$NAT_GATEWAY_NAME] NAT Gateway and the [$FUNCTION_APP_SUBNET_NSG_NAME] network security group..."
# Update the function app subnet to associate it with the NAT Gateway and the NSG
az network vnet subnet update \
--name "$FUNCTION_APP_SUBNET_NAME" \
--vnet-name "$VIRTUAL_NETWORK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--nat-gateway "$NAT_GATEWAY_NAME" \
--network-security-group "$FUNCTION_APP_SUBNET_NSG_NAME" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$FUNCTION_APP_SUBNET_NAME] subnet successfully associated with the [$NAT_GATEWAY_NAME] NAT Gateway and the [$FUNCTION_APP_SUBNET_NSG_NAME] network security group"
else
echo "Failed to associate [$FUNCTION_APP_SUBNET_NAME] subnet with the [$NAT_GATEWAY_NAME] NAT Gateway and the [$FUNCTION_APP_SUBNET_NSG_NAME] network security group"
exit 1
fi
else
echo "[$VIRTUAL_NETWORK_NAME] virtual network already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Check if the subnet already exists
echo "Checking if [$PE_SUBNET_NAME] subnet actually exists in the [$VIRTUAL_NETWORK_NAME] virtual network..."
az network vnet subnet show \
--name "$PE_SUBNET_NAME" \
--vnet-name "$VIRTUAL_NETWORK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$PE_SUBNET_NAME] subnet actually exists in the [$VIRTUAL_NETWORK_NAME] virtual network"
echo "Creating [$PE_SUBNET_NAME] subnet in the [$VIRTUAL_NETWORK_NAME] virtual network..."
# Create the subnet
az network vnet subnet create \
--name "$PE_SUBNET_NAME" \
--vnet-name "$VIRTUAL_NETWORK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--address-prefix "$PE_SUBNET_PREFIX" \
--network-security-group "$PE_SUBNET_NSG_NAME" \
--private-endpoint-network-policies "Disabled" \
--private-link-service-network-policies "Disabled" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$PE_SUBNET_NAME] subnet successfully created in the [$VIRTUAL_NETWORK_NAME] virtual network"
else
echo "Failed to create [$PE_SUBNET_NAME] subnet in the [$VIRTUAL_NETWORK_NAME] virtual network"
exit
fi
else
echo "[$PE_SUBNET_NAME] subnet already exists in the [$VIRTUAL_NETWORK_NAME] virtual network"
fi
# Retrieve the virtual network resource id
echo "Getting [$VIRTUAL_NETWORK_NAME] virtual network resource id in the [$RESOURCE_GROUP_NAME] resource group..."
VIRTUAL_NETWORK_ID=$(az network vnet show \
--name "$VIRTUAL_NETWORK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors \
--query id \
--output tsv)
if [[ -n $VIRTUAL_NETWORK_ID ]]; then
echo "[$VIRTUAL_NETWORK_NAME] virtual network resource id retrieved successfully: $VIRTUAL_NETWORK_ID"
else
echo "Failed to retrieve [$VIRTUAL_NETWORK_NAME] virtual network resource id in the [$RESOURCE_GROUP_NAME] resource group"
exit
fi
# Create private DNS zones and virtual network links
for DNS_ZONE_NAME in "${PRIVATE_DNS_ZONE_NAMES[@]}"; do
# Check if the private DNS Zone already exists
echo "Checking if [$DNS_ZONE_NAME] private DNS zone actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az network private-dns zone show \
--name "$DNS_ZONE_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$DNS_ZONE_NAME] private DNS zone actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$DNS_ZONE_NAME] private DNS zone in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the private DNS Zone
az network private-dns zone create \
--name "$DNS_ZONE_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$DNS_ZONE_NAME] private DNS zone successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$DNS_ZONE_NAME] private DNS zone in the [$RESOURCE_GROUP_NAME] resource group"
exit
fi
else
echo "[$DNS_ZONE_NAME] private DNS zone already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Check if the virtual network link already exists
echo "Checking if [$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$DNS_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 "$DNS_ZONE_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$DNS_ZONE_NAME] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network actually exists"
echo "Creating [$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$DNS_ZONE_NAME] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network..."
# Create the virtual network link
az network private-dns link vnet create \
--name "$VIRTUAL_NETWORK_LINK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--zone-name "$DNS_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 [$DNS_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 [$DNS_ZONE_NAME] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network"
exit
fi
else
echo "[$VIRTUAL_NETWORK_LINK_NAME] virtual network link between [$DNS_ZONE_NAME] private DNS zone and [$VIRTUAL_NETWORK_NAME] virtual network already exists"
fi
done
# Create a storage account
echo "Checking if storage account [$STORAGE_ACCOUNT_NAME] exists in the resource group [$RESOURCE_GROUP_NAME]..."
az storage account show \
--name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No storage account [$STORAGE_ACCOUNT_NAME] exists in the [$RESOURCE_GROUP_NAME] resource group."
echo "Creating storage account [$STORAGE_ACCOUNT_NAME] in the [$RESOURCE_GROUP_NAME] resource group..."
az storage account create \
--name $STORAGE_ACCOUNT_NAME \
--location $LOCATION \
--resource-group $RESOURCE_GROUP_NAME \
--sku Standard_LRS \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Storage account [$STORAGE_ACCOUNT_NAME] created successfully in the [$RESOURCE_GROUP_NAME] resource group."
else
echo "Failed to create storage account [$STORAGE_ACCOUNT_NAME] in the [$RESOURCE_GROUP_NAME] resource group."
exit 1
fi
else
echo "Storage account [$STORAGE_ACCOUNT_NAME] already exists in the [$RESOURCE_GROUP_NAME] resource group."
fi
# Get the storage account key
echo "Getting storage account key for [$STORAGE_ACCOUNT_NAME]..."
STORAGE_ACCOUNT_KEY=$(az storage account keys list \
--account-name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--query "[0].value" \
--output tsv)
if [ -n "$STORAGE_ACCOUNT_KEY" ]; then
echo "Storage account key retrieved successfully: [$STORAGE_ACCOUNT_KEY]"
else
echo "Failed to retrieve storage account key."
exit 1
fi
# Construct the storage connection string for LocalStack
STORAGE_CONNECTION_STRING="DefaultEndpointsProtocol=https;AccountName=$STORAGE_ACCOUNT_NAME;AccountKey=$STORAGE_ACCOUNT_KEY;EndpointSuffix=core.windows.net"
echo "Storage connection string constructed: [$STORAGE_CONNECTION_STRING]"
# Get the storage account resource id
echo "Getting [$STORAGE_ACCOUNT_NAME] storage account resource id in the [$RESOURCE_GROUP_NAME] resource group..."
STORAGE_ACCOUNT_ID=$(az storage account show \
--name "$STORAGE_ACCOUNT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv \
--only-show-errors)
if [[ -n $STORAGE_ACCOUNT_ID ]]; then
echo "[$STORAGE_ACCOUNT_NAME] storage account resource id retrieved successfully: $STORAGE_ACCOUNT_ID"
else
echo "Failed to retrieve [$STORAGE_ACCOUNT_NAME] storage account resource id in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
# Create private endpoints and DNS zone groups
PE_RESOURCE_IDS=("$SERVICE_BUS_NAMESPACE_ID" "$STORAGE_ACCOUNT_ID" "$STORAGE_ACCOUNT_ID" "$STORAGE_ACCOUNT_ID")
for i in "${!PE_NAMES[@]}"; do
PE_NAME="${PE_NAMES[$i]}"
PE_GROUP="${PE_GROUP_IDS[$i]}"
PE_RESOURCE_ID="${PE_RESOURCE_IDS[$i]}"
PE_CONNECTION="${PE_CONNECTION_NAMES[$i]}"
PE_DNS_ZONE="${PE_DNS_ZONES[$i]}"
PE_DNS_ZONE_LABEL="${PE_DNS_ZONE_LABELS[$i]}"
# Check if the private endpoint already exists
echo "Checking if private endpoint [$PE_NAME] exists in the [$RESOURCE_GROUP_NAME] resource group..."
privateEndpointId=$(az network private-endpoint list \
--resource-group $RESOURCE_GROUP_NAME \
--only-show-errors \
--query "[?name=='$PE_NAME'].id" \
--output tsv)
if [[ -z $privateEndpointId ]]; then
echo "Private endpoint [$PE_NAME] does not exist in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$PE_NAME] private endpoint in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the private endpoint
az network private-endpoint create \
--name "$PE_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--vnet-name "$VIRTUAL_NETWORK_NAME" \
--subnet "$PE_SUBNET_NAME" \
--private-connection-resource-id "$PE_RESOURCE_ID" \
--group-id "$PE_GROUP" \
--connection-name "$PE_CONNECTION" \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "Private endpoint [$PE_NAME] successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create private endpoint [$PE_NAME] in the [$RESOURCE_GROUP_NAME] resource group"
exit
fi
else
echo "Private endpoint [$PE_NAME] already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Check if the private DNS zone group is already created
echo "Checking if the private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$PE_NAME] private endpoint already exists..."
az network private-endpoint dns-zone-group show \
--resource-group "$RESOURCE_GROUP_NAME" \
--endpoint-name "$PE_NAME" \
--name "$PRIVATE_DNS_ZONE_GROUP_NAME" \
--query name \
--output tsv \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$PE_NAME] private endpoint actually exists"
echo "Creating private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$PE_NAME] private endpoint..."
# Create the private DNS zone group
az network private-endpoint dns-zone-group create \
--name "$PRIVATE_DNS_ZONE_GROUP_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--endpoint-name "$PE_NAME" \
--private-dns-zone "$PE_DNS_ZONE" \
--zone-name "$PE_DNS_ZONE_LABEL" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "Private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$PE_NAME] private endpoint successfully created"
else
echo "Failed to create private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$PE_NAME] private endpoint"
exit
fi
else
echo "Private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$PE_NAME] private endpoint already exists"
fi
done
if [ $DEPLOY -eq 0 ]; then
echo "Deployment flag is not set. Exiting deployment script."
exit 0
fi
# Check if the application insights az extension is already installed
echo "Checking if [application-insights] az extension is already installed..."
az extension show --name application-insights &>/dev/null
if [[ $? == 0 ]]; then
echo "[application-insights] az extension is already installed"
else
echo "[application-insights] az extension is not installed. Installing..."
# Install application-insights az extension
az extension add --name application-insights 1>/dev/null
if [[ $? == 0 ]]; then
echo "[application-insights] az extension successfully installed"
else
echo "Failed to install [application-insights] az extension"
exit
fi
fi
# Check if the application insights component already exists
echo "Checking if [$APPLICATION_INSIGHTS_NAME] Application Insights component exists in the [$RESOURCE_GROUP_NAME] resource group..."
az monitor app-insights component show \
--app "$APPLICATION_INSIGHTS_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$APPLICATION_INSIGHTS_NAME] Application Insights component exists in the [$RESOURCE_GROUP_NAME] resource group."
echo "Creating [$APPLICATION_INSIGHTS_NAME] Application Insights component in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the application insights component
az monitor app-insights component create \
--app "$APPLICATION_INSIGHTS_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--application-type "web" \
--tags $TAGS \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$APPLICATION_INSIGHTS_NAME] Application Insights component created successfully in the [$RESOURCE_GROUP_NAME] resource group."
else
echo "Failed to create [$APPLICATION_INSIGHTS_NAME] Application Insights component in the [$RESOURCE_GROUP_NAME] resource group."
exit 1
fi
else
echo "[$APPLICATION_INSIGHTS_NAME] Application Insights component already exists in the [$RESOURCE_GROUP_NAME] resource group."
fi
# Get the application insights component resource id
echo "Getting [$APPLICATION_INSIGHTS_NAME] Application Insights component resource id in the [$RESOURCE_GROUP_NAME] resource group..."
APPLICATION_INSIGHTS_ID=$(az monitor app-insights component show \
--app "$APPLICATION_INSIGHTS_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv \
--only-show-errors)
if [[ -n $APPLICATION_INSIGHTS_ID ]]; then
echo "[$APPLICATION_INSIGHTS_NAME] Application Insights component resource id retrieved successfully: $APPLICATION_INSIGHTS_ID"
else
echo "Failed to retrieve [$APPLICATION_INSIGHTS_NAME] Application Insights component resource id in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
# Assign the Azure Service Bus Data Owner role to the managed identity with the Service Bus namespace as a scope
ROLE="Azure Service Bus Data Owner"
echo "Checking if the [$MANAGED_IDENTITY_NAME] managed identity has the [$ROLE] role assignment on the Service Bus namespace [$SERVICE_BUS_NAMESPACE]..."
current=$(az role assignment list \
--assignee "$PRINCIPAL_ID" \
--scope "$SERVICE_BUS_NAMESPACE_ID" \
--query "[?roleDefinitionName=='$ROLE'].roleDefinitionName" \
--output tsv 2>/dev/null)
if [[ $current == "$ROLE" ]]; then
echo "[$MANAGED_IDENTITY_NAME] managed identity already has the [$ROLE] role assignment on the Service Bus namespace [$SERVICE_BUS_NAMESPACE]"
else
echo "[$MANAGED_IDENTITY_NAME] managed identity does not have the [$ROLE] role assignment on the Service Bus namespace [$SERVICE_BUS_NAMESPACE]"
echo "Creating role assignment: assigning [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the Service Bus namespace [$SERVICE_BUS_NAMESPACE]..."
ATTEMPT=1
while [ $ATTEMPT -le $RETRY_COUNT ]; do
echo "Attempt $ATTEMPT of $RETRY_COUNT to assign role..."
az role assignment create \
--assignee "$PRINCIPAL_ID" \
--role "$ROLE" \
--scope "$SERVICE_BUS_NAMESPACE_ID" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
break
else
if [ $ATTEMPT -lt $RETRY_COUNT ]; then
echo "Role assignment failed. Waiting [$SLEEP] seconds before retry..."
sleep $SLEEP
fi
ATTEMPT=$((ATTEMPT + 1))
fi
done
if [[ $? == 0 ]]; then
echo "Successfully assigned [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the Service Bus namespace [$SERVICE_BUS_NAMESPACE]"
else
echo "Failed to assign [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the Service Bus namespace [$SERVICE_BUS_NAMESPACE]"
exit
fi
fi
# Get the storage account resource id for role assignments
echo "Getting [$STORAGE_ACCOUNT_NAME] storage account resource id in the [$RESOURCE_GROUP_NAME] resource group..."
STORAGE_ACCOUNT_ID=$(az storage account show \
--name "$STORAGE_ACCOUNT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv \
--only-show-errors)
if [[ -n $STORAGE_ACCOUNT_ID ]]; then
echo "[$STORAGE_ACCOUNT_NAME] storage account resource id retrieved successfully"
else
echo "Failed to retrieve [$STORAGE_ACCOUNT_NAME] storage account resource id in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
# Assign the Storage Account Contributor role to the managed identity with the storage account as a scope
ROLE="Storage Account Contributor"
echo "Checking if the [$MANAGED_IDENTITY_NAME] managed identity has the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]..."
current=$(az role assignment list \
--assignee "$PRINCIPAL_ID" \
--scope "$STORAGE_ACCOUNT_ID" \
--query "[?roleDefinitionName=='$ROLE'].roleDefinitionName" \
--output tsv 2>/dev/null)
if [[ $current == "$ROLE" ]]; then
echo "[$MANAGED_IDENTITY_NAME] managed identity already has the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "[$MANAGED_IDENTITY_NAME] managed identity does not have the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]"
echo "Creating role assignment: assigning [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]..."
ATTEMPT=1
while [ $ATTEMPT -le $RETRY_COUNT ]; do
echo "Attempt $ATTEMPT of $RETRY_COUNT to assign role..."
az role assignment create \
--assignee "$PRINCIPAL_ID" \
--role "$ROLE" \
--scope "$STORAGE_ACCOUNT_ID" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
break
else
if [ $ATTEMPT -lt $RETRY_COUNT ]; then
echo "Role assignment failed. Waiting [$SLEEP] seconds before retry..."
sleep $SLEEP
fi
ATTEMPT=$((ATTEMPT + 1))
fi
done
if [[ $? == 0 ]]; then
echo "Successfully assigned [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "Failed to assign [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]"
exit
fi
fi
# Assign the Storage Blob Data Owner role to the managed identity with the storage account as a scope
ROLE="Storage Blob Data Owner"
echo "Checking if the [$MANAGED_IDENTITY_NAME] managed identity has the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]..."
current=$(az role assignment list \
--assignee "$PRINCIPAL_ID" \
--scope "$STORAGE_ACCOUNT_ID" \
--query "[?roleDefinitionName=='$ROLE'].roleDefinitionName" \
--output tsv 2>/dev/null)
if [[ $current == "$ROLE" ]]; then
echo "[$MANAGED_IDENTITY_NAME] managed identity already has the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "[$MANAGED_IDENTITY_NAME] managed identity does not have the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]"
echo "Creating role assignment: assigning [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]..."
ATTEMPT=1
while [ $ATTEMPT -le $RETRY_COUNT ]; do
echo "Attempt $ATTEMPT of $RETRY_COUNT to assign role..."
az role assignment create \
--assignee "$PRINCIPAL_ID" \
--role "$ROLE" \
--scope "$STORAGE_ACCOUNT_ID" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
break
else
if [ $ATTEMPT -lt $RETRY_COUNT ]; then
echo "Role assignment failed. Waiting [$SLEEP] seconds before retry..."
sleep $SLEEP
fi
ATTEMPT=$((ATTEMPT + 1))
fi
done
if [[ $? == 0 ]]; then
echo "Successfully assigned [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "Failed to assign [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]"
exit
fi
fi
# Assign the Storage Queue Data Contributor role to the managed identity with the storage account as a scope
ROLE="Storage Queue Data Contributor"
echo "Checking if the [$MANAGED_IDENTITY_NAME] managed identity has the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]..."
current=$(az role assignment list \
--assignee "$PRINCIPAL_ID" \
--scope "$STORAGE_ACCOUNT_ID" \
--query "[?roleDefinitionName=='$ROLE'].roleDefinitionName" \
--output tsv 2>/dev/null)
if [[ $current == "$ROLE" ]]; then
echo "[$MANAGED_IDENTITY_NAME] managed identity already has the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "[$MANAGED_IDENTITY_NAME] managed identity does not have the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]"
echo "Creating role assignment: assigning [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]..."
ATTEMPT=1
while [ $ATTEMPT -le $RETRY_COUNT ]; do
echo "Attempt $ATTEMPT of $RETRY_COUNT to assign role..."
az role assignment create \
--assignee "$PRINCIPAL_ID" \
--role "$ROLE" \
--scope "$STORAGE_ACCOUNT_ID" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
break
else
if [ $ATTEMPT -lt $RETRY_COUNT ]; then
echo "Role assignment failed. Waiting [$SLEEP] seconds before retry..."
sleep $SLEEP
fi
ATTEMPT=$((ATTEMPT + 1))
fi
done
if [[ $? == 0 ]]; then
echo "Successfully assigned [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "Failed to assign [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]"
exit
fi
fi
# Assign the Storage Table Data Contributor role to the managed identity with the storage account as a scope
ROLE="Storage Table Data Contributor"
echo "Checking if the [$MANAGED_IDENTITY_NAME] managed identity has the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]..."
current=$(az role assignment list \
--assignee "$PRINCIPAL_ID" \
--scope "$STORAGE_ACCOUNT_ID" \
--query "[?roleDefinitionName=='$ROLE'].roleDefinitionName" \
--output tsv 2>/dev/null)
if [[ $current == "$ROLE" ]]; then
echo "[$MANAGED_IDENTITY_NAME] managed identity already has the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "[$MANAGED_IDENTITY_NAME] managed identity does not have the [$ROLE] role assignment on the storage account [$STORAGE_ACCOUNT_NAME]"
echo "Creating role assignment: assigning [$ROLE] role to [$MANAGED_IDENTITY_NAME] managed identity on the storage account [$STORAGE_ACCOUNT_NAME]..."
ATTEMPT=1
while [ $ATTEMPT -le $RETRY_COUNT ]; do
echo "Attempt $ATTEMPT of $RETRY_COUNT to assign role..."
az role assignment create \
--assignee "$PRINCIPAL_ID" \
--role "$ROLE" \
--scope "$STORAGE_ACCOUNT_ID" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
break
else
if [ $ATTEMPT -lt $RETRY_COUNT ]; then
echo "Role assignment failed. Waiting [$SLEEP] seconds before retry..."
sleep $SLEEP
fi
ATTEMPT=$((ATTEMPT + 1))
fi