Si necesitas automatizar la creación de API Management con Azure CLI, y es un requisito que este esté integrada con una red, este artículo es para ti 😚
Crear una red virtual
Para este ejemplo, he creado una red virtual, con dos subnets: una para un AKS y otra para nuestro API Management:
VNET_NAME="aks-and-apim-vnet"
AKS_SUBNET_NAME="aks-subnet"
APIM_SUBNET_NAME="apim-subnet"
echo -e "${GREEN}Creating resource group ${RESOURCE_GROUP} in ${LOCATION}..."
az group create --name ${RESOURCE_GROUP} --location ${LOCATION}
echo -e "${GREEN}Creating vnet ${VNET_NAME} in ${RESOURCE_GROUP}..."
az network vnet create \
--resource-group ${RESOURCE_GROUP} \
--name ${VNET_NAME} \
--address-prefixes 192.168.0.0/16 \
--subnet-name ${AKS_SUBNET_NAME} \
--subnet-prefix 192.168.1.0/24
echo -e "${GREEN}Creating apim subnet ${APIM_SUBNET_NAME} in ${RESOURCE_GROUP}..."
az network vnet subnet create \
--resource-group ${RESOURCE_GROUP} \
--vnet-name ${VNET_NAME} \
--name ${APIM_SUBNET_NAME} \
--address-prefixes 192.168.2.0/24
En este ejemplo no he tenido en cuenta el número de IPs necesarias para APIM pero puedes revisar aquí cuál sería el CIDR mínimo que te haría falta en base al escalado que puedas necesitar de tu instancia.
Crear una instancia de API Management
Lo siguiente que necesitas es crear tu instancia de API Management:
RESOURCE_GROUP="aks-and-apim-demo"
LOCATION="westeurope"
APIM_NAME="apim-demo-${RANDOM}"
time az apim create \
--resource-group ${RESOURCE_GROUP} \
--name ${APIM_NAME} \
--location ${LOCATION} \
--publisher-email "gisela.torres@returngis.net" \
--publisher-name "return(GiS);" \
--sku-name Developer
Este comando puede tardar hasta una hora en completarse.
Actualizar la configuración de red de API Management
Ahora que ya tienes tu instancia creada, lo útlimo que te queda es asociarle la subnet que quieras. El propio subcomando de API Management no soporta como tal esta opción, pero puedes conseguirlo con este otro:
echo -e "${GREEN} Connecting API Management to the virtual network ${NC}"
# Get API Management resource id
APIM_ID=$(az apim show -n ${APIM_NAME} -g ${RESOURCE_GROUP} --query "id")
APIM_SUBNET_ID=$(az network vnet subnet show -g ${RESOURCE_GROUP} -n $APIM_SUBNET_NAME --vnet-name $VNET_NAME --query "id")
time az resource update -n ${APIM_NAME} -g ${RESOURCE_GROUP} \
--resource-type Microsoft.ApiManagement/service \
--set properties.virtualNetworkType=External properties.virtualNetworkConfiguration.subnetResourceId=$APIM_SUBNET_ID
Cuando el proceso finalice, asegúrate de que en la pestaña Network Status está todo Ok.

De no ser así, es posible que debas adecuar el network security group asociado a la subnet, donde ahora reside tu API Management, para habilitar los puertos que correspondan. Por ejemplo, los puertos de entrada para el control plane (3443) y para la comunicación con el gateway (80 y 443):
# https://learn.microsoft.com/en-us/azure/api-management/virtual-network-reference?tabs=stv2
# Fix inbound ports for API Management
NSG_RESOURCE_ID=$(az network vnet subnet show --name $APIM_SUBNET_NAME --vnet-name $VNET_NAME --resource-group $RESOURCE_GROUP --query networkSecurityGroup.id -o tsv)
# Get the name of the resouce by the resource Id
NSG_NAME=$(az resource show --ids $NSG_RESOURCE_ID --query name -o tsv)
# Add ngs rules for Api Management control plane inbound
az network nsg rule create \
--name "AllowApiManagementControlPlaneInbound" \
--nsg-name $NSG_NAME \
--resource-group $RESOURCE_GROUP \
--access Allow \
--direction Inbound \
--destination-port-ranges 3443 \
--priority 1000 \
--protocol Tcp
# Add ngs rules for allow client communication to api management 80
az network nsg rule create \
--name "AllowClientCommunicationToAPIManagement_80" \
--nsg-name $NSG_NAME \
--resource-group $RESOURCE_GROUP \
--access Allow \
--direction Inbound \
--destination-port-ranges 80 \
--priority 1001 \
--protocol Tcp
# Add ngs rules for allow client communication to api management 443
az network nsg rule create \
--name "AllowClientCommunicationToAPIManagement_443" \
--nsg-name $NSG_NAME \
--resource-group $RESOURCE_GROUP \
--access Allow \
--direction Inbound \
--destination-port-ranges 443 \
--priority 1002 \
--protocol Tcp
¡Saludos!