|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | +trap 'echo "[ERROR] Failed during Private Endpoint or DNS setup." >&2' ERR |
| 4 | + |
| 5 | +SUBSCRIPTION_ID=$1 |
| 6 | +LOCATION=$2 |
| 7 | +RG=$3 |
| 8 | +SA1_NAME=$4 # from previous script (storage account 1) |
| 9 | +SA2_NAME=$5 # from previous script (storage account 2) |
| 10 | +VNET_A1="cx_vnet_a1" |
| 11 | + |
| 12 | +SUBNET_PE_A1="pe" |
| 13 | +PE_NAME="${SA1_NAME}-pe" |
| 14 | +PRIVATE_DNS_ZONE="privatelink.blob.core.windows.net" |
| 15 | +LINK_NAME="${VNET_A1}-link" |
| 16 | + |
| 17 | +echo "==> Creating Private DNS zone: $PRIVATE_DNS_ZONE" |
| 18 | +az network private-dns zone create -g "$RG" -n "$PRIVATE_DNS_ZONE" --output none \ |
| 19 | + && echo "[OK] DNS zone $PRIVATE_DNS_ZONE created." |
| 20 | + |
| 21 | +echo "==> Linking DNS zone $PRIVATE_DNS_ZONE to VNet $VNET_A1" |
| 22 | +az network private-dns link-vnet create \ |
| 23 | + -g "$RG" -n "$LINK_NAME" \ |
| 24 | + --zone-name "$PRIVATE_DNS_ZONE" \ |
| 25 | + --virtual-network "$VNET_A1" \ |
| 26 | + --registration-enabled false --output none \ |
| 27 | + && echo "[OK] Linked DNS zone to $VNET_A1." |
| 28 | + |
| 29 | +echo "==> Creating Private Endpoint for Storage Account: $SA1_NAME" |
| 30 | +SA1_ID=$(az storage account show -g "$RG" -n "$SA1_NAME" --query id -o tsv) |
| 31 | +az network private-endpoint create \ |
| 32 | + -g "$RG" -n "$PE_NAME" -l "$LOCATION" \ |
| 33 | + --vnet-name "$VNET_A1" --subnet "$SUBNET_PE_A1" \ |
| 34 | + --private-connection-resource-id "$SA1_ID" \ |
| 35 | + --group-id blob \ |
| 36 | + --connection-name "${PE_NAME}-conn" \ |
| 37 | + --output none \ |
| 38 | + && echo "[OK] Private Endpoint $PE_NAME created for $SA1_NAME." |
| 39 | + |
| 40 | +echo "==> Linking Private Endpoint to DNS zone" |
| 41 | +NIC_ID=$(az network private-endpoint show -g "$RG" -n "$PE_NAME" --query 'networkInterfaces[0].id' -o tsv) |
| 42 | +FQDN=$(az storage account show -g "$RG" -n "$SA1_NAME" --query 'primaryEndpoints.blob' -o tsv | sed 's#https://##; s#/##') |
| 43 | +PRIVATE_IP=$(az network nic show --ids "$NIC_ID" --query 'ipConfigurations[0].privateIpAddress' -o tsv) |
| 44 | + |
| 45 | +az network private-dns record-set a add-record \ |
| 46 | + -g "$RG" -z "$PRIVATE_DNS_ZONE" -n "$FQDN" -a "$PRIVATE_IP" --output none \ |
| 47 | + && echo "[OK] Added Private DNS record for $SA1_NAME → $PRIVATE_IP" |
| 48 | + |
| 49 | +echo "Private Endpoint setup complete for $SA1_NAME (accessible only within VNet A1)." |
0 commit comments