Search code examples
azureazure-api-managementazure-cli

Azure API Management - How to set the VNET/SubNet using Azure CLI?


I have created my VNET & SubNet as mentioned below

RESOURCE_GROUP="POC-RG"
LOCATION="westus"
APIMNAME="poc-apim-98"
PUBLISHER="Demo"
PUBLISHEREMAIL="myemail@demo.com"
SKU="Premium"
VNETNAME="app-vnet"
APITYPE="External"

az network vnet create \
  --resource-group ${RESOURCE_GROUP} \
  --name ${VNETNAME} \
  --location ${LOCATION}

az network vnet subnet create \
--resource-group ${RESOURCE_GROUP} \
--vnet-name ${VNETNAME} \
--name apim \
--address-prefixes 10.0.5.0/24

and I want to provision the Azure API Management in the apim subnet created above

az apim create --name ${APIMNAME} -g ${RESOURCE_GROUP} -l ${LOCATION} --sku-name ${SKU} --publisher-email ${PUBLISHEREMAIL} --publisher-name ${PUBLISHER} --virtual-network ${APITYPE}

Looks like Azure CLI does not take the subnet parameter while creating the APIM, how do I set the subnet and create the Azure API Management using azure cli?


Solution

  • You are right. For some reason az apim create does not provide option to input a subnet reference of a VNET.

    You have 2 options:

    az group deployment create --resource-group <my-resource-group> --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/201-api-management-create-with-external-vnet/azuredeploy.json
    

    Or,

    • Use az resource command to add subnet reference after you create APIM.
    az apim create --name ${APIMNAME} -g ${RESOURCE_GROUP} -l ${LOCATION} --sku-name ${SKU} --publisher-email ${PUBLISHEREMAIL} --publisher-name ${PUBLISHER} --virtual-network ${APITYPE}
    
    $apimResourceId = az apim show -n ${APIMNAME} -g ${RESOURCE_GROUP} --query 'id' -o json
    
    $subnetResourceId = az network vnet subnet show -g ${RESOURCE_GROUP} -n apim --vnet-name ${VNETNAME} --query 'id' -o json
    
    az resource update --ids $apimResourceId --set properties.virtualNetworkConfiguration.subnetResourceId=$subnetResourceId