Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
446 lines (402 loc) · 15.6 KB
/
Copy pathdeploy.sh
File metadata and controls
446 lines (402 loc) · 15.6 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
#!/bin/bash
# =============================================================================
# Container Apps Guestbook - Deployment Script
#
# Deploys the Guestbook app using three Azure services:
# 1. Azure Blob Storage - Stores guestbook entries as a JSON blob
# 2. Azure Container Registry (ACR) - Hosts the Docker container image
# 3. Azure Container Apps - Runs the containerized Flask app behind the
# managed environment's HTTP ingress
#
# By default the storage connection string is stored as a Container Apps
# secret and injected into the container through a secretref environment
# variable. Set MANAGED_IDENTITY_TYPE to SystemAssigned or UserAssigned to have
# the app reach Blob Storage with a managed identity instead, granted the
# Storage Blob Data Contributor role on the storage account.
# =============================================================================
# Variables
PREFIX="${PREFIX:-local}"
LOCATION='eastus'
RESOURCE_GROUP_NAME="${PREFIX}-aca-rg"
STORAGE_ACCOUNT_NAME="${PREFIX}acastorage"
BLOB_CONTAINER_NAME="guestbook"
ACR_NAME="${PREFIX}acaacr"
ACA_ENV_NAME="${PREFIX}-aca-env"
ACA_APP_NAME="${PREFIX}-aca-guestbook"
IMAGE_NAME="guestbook"
IMAGE_TAG="v1"
MANAGED_IDENTITY_TYPE="${MANAGED_IDENTITY_TYPE:-None}"
MANAGED_IDENTITY_NAME="${PREFIX}-aca-identity"
STORAGE_ROLE="Storage Blob Data Contributor"
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
case "$MANAGED_IDENTITY_TYPE" in
None | SystemAssigned | UserAssigned) ;;
*)
echo "MANAGED_IDENTITY_TYPE must be None, SystemAssigned or UserAssigned, not [$MANAGED_IDENTITY_TYPE]."
exit 1
;;
esac
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Assigns the storage role to a managed identity, retrying while a newly
# created principal is still replicating to Microsoft Entra ID.
assign_storage_role() {
local principal_id="$1"
for attempt in 1 2 3 4 5; do
if az role assignment create \
--assignee-object-id "$principal_id" \
--assignee-principal-type ServicePrincipal \
--role "$STORAGE_ROLE" \
--scope "$STORAGE_ACCOUNT_ID" \
--only-show-errors 1>/dev/null; then
echo "Role [$STORAGE_ROLE] assigned to principal [$principal_id]."
return 0
fi
echo "Role assignment attempt $attempt failed. Retrying in 10 seconds..."
sleep 10
done
echo "Failed to assign role [$STORAGE_ROLE] to principal [$principal_id]."
return 1
}
# =============================================================================
# Step 1: Create Resource Group
# =============================================================================
echo ""
echo "============================================================"
echo "Step 1: Creating resource group [$RESOURCE_GROUP_NAME]..."
echo "============================================================"
az group create \
--name $RESOURCE_GROUP_NAME \
--location $LOCATION \
--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
# =============================================================================
# Step 2: Create Storage Account
# =============================================================================
echo ""
echo "============================================================"
echo "Step 2: Creating storage account [$STORAGE_ACCOUNT_NAME]..."
echo "============================================================"
az storage account create \
--name $STORAGE_ACCOUNT_NAME \
--location $LOCATION \
--resource-group $RESOURCE_GROUP_NAME \
--sku Standard_LRS \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Storage account [$STORAGE_ACCOUNT_NAME] created successfully."
else
echo "Failed to create storage account [$STORAGE_ACCOUNT_NAME]."
exit 1
fi
# =============================================================================
# Step 3: Get Storage Account Key
# =============================================================================
echo ""
echo "============================================================"
echo "Step 3: Retrieving storage account key..."
echo "============================================================"
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."
else
echo "Failed to retrieve storage account key."
exit 1
fi
# =============================================================================
# Step 4: Get Storage Blob Endpoint
# =============================================================================
echo ""
echo "============================================================"
echo "Step 4: Retrieving storage blob endpoint..."
echo "============================================================"
BLOB_ENDPOINT=$(az storage account show \
--name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--query "primaryEndpoints.blob" \
--output tsv \
--only-show-errors)
if [ -n "$BLOB_ENDPOINT" ]; then
echo "Blob endpoint: $BLOB_ENDPOINT"
else
echo "Failed to retrieve blob endpoint."
exit 1
fi
STORAGE_CONN_STRING="DefaultEndpointsProtocol=http;AccountName=${STORAGE_ACCOUNT_NAME};AccountKey=${STORAGE_ACCOUNT_KEY};BlobEndpoint=${BLOB_ENDPOINT}"
echo "Connection string built successfully."
# =============================================================================
# Step 5: Create Blob Container
# =============================================================================
echo ""
echo "============================================================"
echo "Step 5: Creating blob container [$BLOB_CONTAINER_NAME]..."
echo "============================================================"
# Use --connection-string to ensure the correct endpoint is used
# (--account-name constructs its own hostname which may not match LocalStack's cert)
az storage container create \
--name $BLOB_CONTAINER_NAME \
--connection-string "$STORAGE_CONN_STRING" \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Blob container [$BLOB_CONTAINER_NAME] created successfully."
else
echo "Failed to create blob container [$BLOB_CONTAINER_NAME]."
exit 1
fi
# =============================================================================
# Step 5b: Create the User-Assigned Managed Identity (UserAssigned only)
# =============================================================================
STORAGE_ACCOUNT_ID=$(az storage account show \
--name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--query "id" \
--output tsv \
--only-show-errors)
if [[ $MANAGED_IDENTITY_TYPE == "UserAssigned" ]]; then
echo ""
echo "============================================================"
echo "Step 5b: Creating managed identity [$MANAGED_IDENTITY_NAME]..."
echo "============================================================"
read -r IDENTITY_ID IDENTITY_CLIENT_ID IDENTITY_PRINCIPAL_ID < <(az identity create \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--query "[id, clientId, principalId]" \
--output tsv \
--only-show-errors | tr '\n' ' ')
if [ -n "$IDENTITY_PRINCIPAL_ID" ]; then
echo "Managed identity [$MANAGED_IDENTITY_NAME] created. Client ID: $IDENTITY_CLIENT_ID"
else
echo "Failed to create managed identity [$MANAGED_IDENTITY_NAME]."
exit 1
fi
assign_storage_role "$IDENTITY_PRINCIPAL_ID" || exit 1
fi
# =============================================================================
# Step 6: Create Azure Container Registry (ACR)
# =============================================================================
echo ""
echo "============================================================"
echo "Step 6: Creating ACR [$ACR_NAME] with admin user enabled..."
echo "============================================================"
az acr create \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--sku Basic \
--admin-enabled true \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "ACR [$ACR_NAME] created successfully."
else
echo "Failed to create ACR [$ACR_NAME]."
exit 1
fi
# =============================================================================
# Step 7: Get ACR Login Server and Credentials
# =============================================================================
echo ""
echo "============================================================"
echo "Step 7: Retrieving ACR credentials..."
echo "============================================================"
LOGIN_SERVER=$(az acr show \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "loginServer" \
--output tsv \
--only-show-errors)
if [ -z "$LOGIN_SERVER" ]; then
echo "Failed to retrieve ACR login server."
exit 1
fi
echo "ACR Login Server: $LOGIN_SERVER"
ACR_USERNAME=$(az acr credential show \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "username" \
--output tsv \
--only-show-errors)
ACR_PASSWORD=$(az acr credential show \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "passwords[0].value" \
--output tsv \
--only-show-errors)
if [ -n "$ACR_USERNAME" ] && [ -n "$ACR_PASSWORD" ]; then
echo "ACR credentials retrieved successfully. Username: $ACR_USERNAME"
else
echo "Failed to retrieve ACR credentials."
exit 1
fi
# =============================================================================
# Step 8: Build and Push Docker Image to ACR
# =============================================================================
echo ""
echo "============================================================"
echo "Step 8: Building and pushing Docker image to ACR..."
echo "============================================================"
FULL_IMAGE="${LOGIN_SERVER}/${IMAGE_NAME}:${IMAGE_TAG}"
# Build the Docker image
echo "Building Docker image [$IMAGE_NAME:$IMAGE_TAG]..."
docker build -t "${IMAGE_NAME}:${IMAGE_TAG}" ../src/
if [ $? -eq 0 ]; then
echo "Docker image built successfully."
else
echo "Failed to build Docker image."
exit 1
fi
# Tag for ACR
docker tag "${IMAGE_NAME}:${IMAGE_TAG}" "$FULL_IMAGE"
# Login to ACR
echo "Logging in to ACR [$LOGIN_SERVER]..."
echo "$ACR_PASSWORD" | docker login "$LOGIN_SERVER" --username "$ACR_USERNAME" --password-stdin 2>/dev/null
if [ $? -eq 0 ]; then
echo "Logged in to ACR successfully."
else
echo "Warning: Failed to login to ACR. Will attempt push anyway."
fi
# Push to ACR
echo "Pushing image [$FULL_IMAGE]..."
docker push "$FULL_IMAGE" 2>/dev/null
if [ $? -eq 0 ]; then
echo "Image pushed to ACR successfully."
else
echo "Failed to push image to ACR."
exit 1
fi
# =============================================================================
# Step 9: Create Container Apps Managed Environment
# =============================================================================
echo ""
echo "============================================================"
echo "Step 9: Creating Container Apps environment [$ACA_ENV_NAME]..."
echo "============================================================"
# --logs-destination none keeps the CLI from provisioning a Log Analytics
# workspace, which this sample does not use.
az containerapp env create \
--name "$ACA_ENV_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--logs-destination none \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Container Apps environment [$ACA_ENV_NAME] created successfully."
else
echo "Failed to create Container Apps environment [$ACA_ENV_NAME]."
exit 1
fi
# =============================================================================
# Step 10: Create Container App
# =============================================================================
echo ""
echo "============================================================"
echo "Step 10: Creating container app [$ACA_APP_NAME]..."
echo "============================================================"
# The registry credentials are passed explicitly: the CLI only infers them from
# ARM when the server ends in ".azurecr.io", and LocalStack's loginServer is its
# own host. Without a managed identity the storage connection string becomes
# the Container Apps secret [storage-conn], referenced from the container via a
# secretref env var; with one, the app gets the blob endpoint (HTTPS, as a
# token credential requires) instead of the storage secret.
case "$MANAGED_IDENTITY_TYPE" in
None)
STORAGE_ARGS=(--secrets storage-conn="$STORAGE_CONN_STRING")
STORAGE_ENV_VARS=(AZURE_STORAGE_CONNECTION_STRING=secretref:storage-conn)
;;
SystemAssigned)
STORAGE_ARGS=(--system-assigned)
STORAGE_ENV_VARS=(AZURE_STORAGE_ACCOUNT_URL="$BLOB_ENDPOINT")
;;
UserAssigned)
STORAGE_ARGS=(--user-assigned "$IDENTITY_ID")
STORAGE_ENV_VARS=(AZURE_STORAGE_ACCOUNT_URL="$BLOB_ENDPOINT" AZURE_CLIENT_ID="$IDENTITY_CLIENT_ID")
;;
esac
az containerapp create \
--name "$ACA_APP_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--environment "$ACA_ENV_NAME" \
--image "$FULL_IMAGE" \
--container-name "$IMAGE_NAME" \
--registry-server "$LOGIN_SERVER" \
--registry-username "$ACR_USERNAME" \
--registry-password "$ACR_PASSWORD" \
"${STORAGE_ARGS[@]}" \
--env-vars \
"${STORAGE_ENV_VARS[@]}" \
BLOB_CONTAINER_NAME="$BLOB_CONTAINER_NAME" \
APP_REVISION="$IMAGE_TAG" \
--ingress external \
--target-port 8080 \
--transport http \
--allow-insecure \
--revisions-mode multiple \
--revision-suffix "$IMAGE_TAG" \
--cpu 0.5 --memory 1Gi \
--min-replicas 1 --max-replicas 3 \
--scale-rule-name http-scale \
--scale-rule-type http \
--scale-rule-http-concurrency 50 \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Container app [$ACA_APP_NAME] created successfully."
else
echo "Failed to create container app [$ACA_APP_NAME]."
exit 1
fi
# A system-assigned identity only exists once the app does, so it is granted
# access afterwards. Until the role propagates, Blob Storage rejects the app's
# requests; validate.sh retries the round trip for that reason.
if [[ $MANAGED_IDENTITY_TYPE == "SystemAssigned" ]]; then
APP_PRINCIPAL_ID=$(az containerapp show \
--name "$ACA_APP_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "identity.principalId" \
--output tsv \
--only-show-errors)
if [ -z "$APP_PRINCIPAL_ID" ]; then
echo "Container app [$ACA_APP_NAME] has no system-assigned identity."
exit 1
fi
echo "System-assigned identity principal ID: $APP_PRINCIPAL_ID"
assign_storage_role "$APP_PRINCIPAL_ID" || exit 1
fi
FQDN=$(az containerapp show \
--name "$ACA_APP_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "properties.configuration.ingress.fqdn" \
--output tsv \
--only-show-errors)
# =============================================================================
# Summary
# =============================================================================
echo ""
echo "============================================================"
echo "Deployment Complete!"
echo "============================================================"
echo "Resource Group: $RESOURCE_GROUP_NAME"
echo "Storage Account: $STORAGE_ACCOUNT_NAME"
echo "Blob Container: $BLOB_CONTAINER_NAME"
echo "ACR: $ACR_NAME ($LOGIN_SERVER)"
echo "ACA Environment: $ACA_ENV_NAME"
echo "Container App: $ACA_APP_NAME"
echo "Image: $FULL_IMAGE"
echo "Managed Identity: $MANAGED_IDENTITY_TYPE"
echo "Ingress FQDN: $FQDN"
if [[ "$FQDN" == *"localhost.localstack.cloud" ]]; then
echo "App URL: http://${FQDN}:4566/"
else
echo "App URL: https://${FQDN}/"
fi
echo ""
echo "Run 'bash scripts/validate.sh' to verify the deployment."
echo "============================================================"