Automating creation of Azure Subscriptions with AZ CLI

Azure CLI
If you’ve ever tried to create Azure Subscriptions with Terraform, AZ cli or via ARM api you’ll know that it was not possible up until recently. Previously you had to open a different UI to choose a billing model and create a new Subscription (even if it was bound to the same AD Tenant as your existing Subscription). Terrform and AZ work in the context of a Subscription so it was assumed you would have one before you started, however sometimes we need to create these automatically. It looks like Microsoft are addressing this issue and converging their services and have introduced an API (preview) for programmatically creating subscriptions on the fly. According to this blog post in May it should be possible to use the AZ cli. However, there are a few traps for young players which I encountered so the following may help you: Firstly, make sure you are on the latest version of AZ (at time of writing it is 2.0.45):
az --version
and upgrade if necessary. The command to create a Subscription lives in the Account module, however, if you try to run:
az account –help
You’ll probably see there is no “create” command. At first I thought I had an old version of AZ but it turns out that AZ can be extended with extra modules via an extensions package manager. Who knew?! To see what AZ extensions you have installed you can run:
az extension list-available --output table
Your output will look something like the below, depending on what extensions you have installed. azCreateSubscription To install the Subscription module use:
az extension add --name subscription
You should now have the extension installed if you run this again:
az extension list-available --output table
Now if you get help on the AZ account you’ll see the “create” command:
az account –help
AZCreateCommand.png Referencing the docs, you should now be able to create new Azure subscriptions with:
az account create --enrollment-account-name --offer-type {MS-AZR-0017P, MS-AZR-0148P}
You can see your enrolment accounts with AZ billing module:
az billing enrollment-account list
There only seems to be 2 types of Subscriptions available for now – Enterprise and devTest (get better prices on your Azure resources in dev!), but this service is only in preview, so may change. Note: If you are within a large enterprise your mileage may vary. It seems to depend on the EA or CSP agreement that a company has with Microsoft. You can test if you have the ability to create subscriptions by going to the Azure Portal -> Subscriptions and clicking on the Add button. If you can create a Subscription through the UI you should be able to do it via the CLI or api. Yet another note:  Extensions are all open-source as you would expect from the “new” MS. Feel free to enhance them or write your own: To upgrade AZ Extensions you can run:
az extension update --name webapp
Let me know if you have any comments or questions…  
Avatar of Rodney Joyce

What do you think?

Subscribed! We'll let you know when we have new blogs and events...