Creating Subscriptions with ARM Templates

Published 04-28-2021 05:00 AM 2,248 Views
Microsoft

 

As more and more enterprises embrace Azure, complete end-to-end automation for standing up workloads in the cloud is one of the most important steps to running at scale.  The latest release (2020-09-01) of the Microsoft.Subscription resource provider enables subscription creation via templates.  To get started you first need to ensure billing agreements are in place and you can find details on that process here.  Once this is done, a new subscription can be created for the proper workload and billing account.  Once created, the subscription can be referred to with an alias throughout your code or templates.

 

Here's a look at a subscription resource in a template:

 

"resources": [
    {
        "scope": "/",
        "type": "Microsoft.Subscription/aliases",
        "apiVersion": "2020-09-01",
        "name": "[parameters('subscriptionAlias')]",
        "properties": {
            "workload": "[parameters('subscriptionWorkload')]",
            "displayName": "[parameters('subscriptionDisplayName')]",
            "billingScope": "[tenantResourceId('Microsoft.Billing/billingAccounts/enrollmentAccounts', parameters('billingAccount'), parameters('enrollmentAccount'))]"
        }
    }
]

 

 

Of particular note is the "scope" property.  Subscriptions are a tenant level resource in Azure and must be PUT to the tenant scope which the property allows.

 

The prereqs for creating subscriptions is to identify the billing scope for the subscription.  You can find more information about billing scopes in this doc.  A sample script for looking up billing information can be found here.

 

The next step, when using templates for subscription creation, is to determine the scope for the template deployment itself.  All templates are deployed to a specific scope; most commonly this is a resourceGroup, but template deployments can be done at the subscription, managementGroup or tenant scope as well.  The scope of the deployment does not need to match the scope of the resources that are deployed though it often does.  For our template sample, we’ll describe a scenario, or rather tenant, where no subscriptions (or resourceGroups) exist and we’ll deploy to a managementGroup.  It doesn’t matter which managementGroup we choose for the deployment because the subscription itself will be created at the tenant scope and placed in the default managementGroup, unless a different one is specified.  If this is still a little confusing, just focus on the subscription resource itself.  This resource must be deployed to the tenant scope and the examples will show how to use the "scope" property to indicate that.

 

Note, that you must have permissions to create template deployments at the scope you target.  Also, the permission to deploy a template at a scope does not automatically give permissions to create any other resource, so you do need to ensure that you have the necessary permissions to create the resources in your template, if any, as well.  That should cover everything about permission.

 

A QuickStart sample for deploying a subscription can be found here.  The command for deploying this template is just like deploying any other template and following our example would be:

 

New-AzManagementGroupDeployment -ManagementGroupId (Get-AzContext).Tenant.id -Location westeurope -TemplateFile azuredeploy.json -TemplateParameterFile myParameters.json

 

 

This will deploy the template to the "root" managementGroup for the tenant.  Again, remember that you must have permission to deploy to that scope, in this case the root managementGroup.  If you don't have that permission, you can deploy the template to any other managementGroup, subscription or resourceGroup.  Also a reminder that even though the subscription is created at the tenant scope, the template deployment does not need to match that scope.

 

So far, this is a very simple example, but you can also create a subscription and deploy resources to that subscription in the same template.  There is a little more orchestration required here because you’re actually targeting multiple scopes within the same template.  And, in order to target the subscription, you need the subscriptionId or GUID that was assigned to the subscription when it was created.  This next sample will perform each of these steps:

 

  1. Create the subscription (this is shown in the previous sample)
  2. Retrieve the subscriptionId from the newly created alias

 

"outputs": {
    "subscriptionId": {
        "type": "string",
        "value": "[reference(parameters('subscriptionAlias')).subscriptionId]"
    }
}

 

 

3. Pass that subscriptionId to the next deployment in the template

 

"type": "Microsoft.Resources/deployments",
"apiVersion": "2020-10-01",
"name": "[concat('nested-createResourceGroup-', parameters('resourceGroupName'))]",
"location": "[parameters('location')]",
"properties": {
    "expressionEvaluationOptions": {
        "scope": "inner"
    },
    "mode": "Incremental",
    "parameters": {
        "subscriptionId": {
            // this cannot be referenced directly on the subscriptionId property of the deployment so needs to be nested one level
            "value": "[reference(resourceId('Microsoft.Resources/deployments', concat('createSubscription-', parameters('subscriptionAlias')))).outputs.subscriptionId.value]"
        },
...

 

 

4. Create a new deployment in the new subscription that creates the resourceGroup

 

{
    "type": "Microsoft.Resources/deployments",
    "apiVersion": "2020-10-01",
    "name": "[concat('createResourceGroup-', parameters('resourceGroupName'))]",
    "subscriptionId": "[parameters('subscriptionId')]",
    "location": "[parameters('location')]",
    "properties": { ... }
...

 

 

And then finally deploy the resources to that resourceGroup.

 

That's more complex than just creating a subscription because all of the orchestration is handled within a single template. 

 

If your scenario requires a different scope of deployment or more steps then you may not want to include in that single template.  If I wanted to break this down into multiple steps for orchestration in a pipeline it can be as simple as two steps.

 

Step 1 – Create the Subscription

Performing this step separately can be useful if you do not want to provide a user or service principal with permission to create template deployments at a given scope.  Once the subscription is created, the principal that created the subscription is an owner of that subscription and can deploy templates to that newly created subscription.  This means that the only permission the principal needs outside of the subscription, is the permission to create one. 

 

At this writing, the Azure PowerShell does not have a built-in command to create a subscription but you can always invoke any Azure REST api using the Invoke-AzRestMethod.  This script shows how to do that to create a subscription through an alias resource using the following command.

 

 .\Create-SubscriptionAlias.ps1 -aliasName "newSub" -DisplayName "demo subscription" -billingAccount "1234567" -enrollmentAccount "654321" -workLoad DevTest

 

 

You need to set the correct parameter values for the billingAccount and enrollmentAccount which you can discover using this script from the top of this article.

 

Step 2 – Deploy the template.

Next, keeping with our greenfield scenario, where the subscription is created in the same workflow or pipeline that deploys this next template, we’ll create a subscription-level deployment.  If we were running an automated pipeline, this sample would be a good example for the next step.  The sample will create a new resourceGroup in the subscription, lock it and assign a principal access to that resourceGroup.  From here you could deploy resources to the subscription (or resourceGroup) or simply make it available for the principal to use.

 

That's a quick overview of how to leverage this new capability, in just a few scenarios, that you can use to automate new workloads in Azure.  Let me know how it goes or if you have any questions about automating subscription creation in your environments.

 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-1839961%22%20slang%3D%22en-US%22%3ECreating%20Subscriptions%20with%20ARM%20Templates%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1839961%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20more%20and%20more%20enterprises%20embrace%20Azure%2C%20complete%20end-to-end%20automation%20for%20standing%20up%20workloads%20in%20the%20cloud%20is%20one%20of%20the%20most%20important%20steps%20to%20running%20at%20scale.%26nbsp%3B%20The%20latest%20release%20(2020-09-01)%20of%20the%20Microsoft.Subscription%20resource%20provider%20enables%20subscription%20creation%20via%20templates.%26nbsp%3B%20To%20get%20started%20you%20first%20need%20to%20ensure%20billing%20agreements%20are%20in%20place%20and%20you%20can%20find%20details%20on%20that%20process%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fcost-management-billing%2Fmanage%2Fprogrammatically-create-subscription%3Ftabs%3Drest-getEnrollments%252Crest-EA%252Crest-getBillingAccounts%252Crest-getBillingProfiles%252Crest-MCA%252Crest-getBillingAccount-MPA%252Crest-getCustomers%252Crest-getIndirectResellers%252Crest-MPA%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%26nbsp%3B%20Once%20this%20is%20done%2C%20a%20new%20subscription%20can%20be%20created%20for%20the%20proper%20workload%20and%20billing%20account.%26nbsp%3B%20Once%20created%2C%20the%20subscription%20can%20be%20referred%20to%20with%20an%20alias%20throughout%20your%20code%20or%20templates.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere's%20a%20look%20at%20a%20subscription%20resource%20in%20a%20template%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%22resources%22%3A%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22scope%22%3A%20%22%2F%22%2C%0A%20%20%20%20%20%20%20%20%22type%22%3A%20%22Microsoft.Subscription%2Faliases%22%2C%0A%20%20%20%20%20%20%20%20%22apiVersion%22%3A%20%222020-09-01%22%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22%5Bparameters('subscriptionAlias')%5D%22%2C%0A%20%20%20%20%20%20%20%20%22properties%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22workload%22%3A%20%22%5Bparameters('subscriptionWorkload')%5D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22displayName%22%3A%20%22%5Bparameters('subscriptionDisplayName')%5D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22billingScope%22%3A%20%22%5BtenantResourceId('Microsoft.Billing%2FbillingAccounts%2FenrollmentAccounts'%2C%20parameters('billingAccount')%2C%20parameters('enrollmentAccount'))%5D%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%5D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOf%20particular%20note%20is%20the%20%22scope%22%20property.%26nbsp%3B%20Subscriptions%20are%20a%20tenant%20level%20resource%20in%20Azure%20and%20must%20be%20PUT%20to%20the%20tenant%20scope%20which%20the%20property%20allows.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20prereqs%20for%20creating%20subscriptions%20is%20to%20identify%20the%20billing%20scope%20for%20the%20subscription.%20%26nbsp%3BYou%20can%20find%20more%20information%20about%20billing%20scopes%20in%20this%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fcost-management-billing%2Fmanage%2Funderstand-mca-roles%23subscription-billing-roles-and-tasks%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Edoc%3C%2FA%3E.%26nbsp%3B%20A%20sample%20script%20for%20looking%20up%20billing%20information%20can%20be%20found%20%3CA%20title%3D%22Sample%20Script%20-%20Get%20Billing%20Scope%20Information%22%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-quickstart-templates%2Fblob%2Fmaster%2Fmanagementgroup-deployments%2Fcreate-subscription%2FGet-BillingAccountsEA.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20next%20step%2C%20when%20using%20templates%20for%20subscription%20creation%2C%20is%20to%20determine%20the%20scope%20for%20the%20template%20deployment%20itself.%26nbsp%3B%20All%20templates%20are%20deployed%20to%20a%20specific%20scope%3B%20most%20commonly%20this%20is%20a%20resourceGroup%2C%20but%20template%20deployments%20can%20be%20done%20at%20the%20subscription%2C%20managementGroup%20or%20tenant%20scope%20as%20well.%26nbsp%3B%20The%20scope%20of%20the%20deployment%20does%20not%20need%20to%20match%20the%20scope%20of%20the%20resources%20that%20are%20deployed%20though%20it%20often%20does.%26nbsp%3B%20For%20our%20template%20sample%2C%20we%E2%80%99ll%20describe%20a%20scenario%2C%20or%20rather%20tenant%2C%20where%20no%20subscriptions%20(or%20resourceGroups)%20exist%20and%20we%E2%80%99ll%20deploy%20to%20a%20managementGroup.%26nbsp%3B%20It%20doesn%E2%80%99t%20matter%20which%20managementGroup%20we%20choose%20for%20the%20deployment%20because%20the%20subscription%20itself%20will%20be%20created%20at%20the%20tenant%20scope%20and%20placed%20in%20the%20default%20managementGroup%2C%20unless%20a%20different%20one%20is%20specified.%26nbsp%3B%20If%20this%20is%20still%20a%20little%20confusing%2C%20just%20focus%20on%20the%20subscription%20resource%20itself.%26nbsp%3B%20This%20resource%20must%20be%20deployed%20to%20the%20tenant%20scope%20and%20the%20examples%20will%20show%20how%20to%20use%20the%20%22scope%22%20property%20to%20indicate%20that.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%2C%20that%20you%20must%20have%20permissions%20to%20create%20template%20deployments%20at%20the%20scope%20you%20target.%26nbsp%3B%20Also%2C%20the%20permission%20to%20deploy%20a%20template%20at%20a%20scope%20does%20not%20automatically%20give%20permissions%20to%20create%20any%20other%20resource%2C%20so%20you%20do%20need%20to%20ensure%20that%20you%20have%20the%20necessary%20permissions%20to%20create%20the%20resources%20in%20your%20template%2C%20if%20any%2C%20as%20well.%26nbsp%3B%20That%20should%20cover%20everything%20about%20permission.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20QuickStart%20sample%20for%20deploying%20a%20subscription%20can%20be%20found%20%3CA%20title%3D%22Azure%20QuickStart%20Create%20Subscription%20Sample%22%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-quickstart-templates%2Ftree%2Fmaster%2Fmanagementgroup-deployments%2Fcreate-subscription%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%26nbsp%3B%20The%20command%20for%20deploying%20this%20template%20is%20just%20like%20deploying%20any%20other%20template%20and%20following%20our%20example%20would%20be%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ENew-AzManagementGroupDeployment%20-ManagementGroupId%20(Get-AzContext).Tenant.id%20-Location%20westeurope%20-TemplateFile%20azuredeploy.json%20-TemplateParameterFile%20myParameters.json%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20will%20deploy%20the%20template%20to%20the%20%22root%22%20managementGroup%20for%20the%20tenant.%26nbsp%3B%20Again%2C%20remember%20that%20you%20must%20have%20permission%20to%20deploy%20to%20that%20scope%2C%20in%20this%20case%20the%20root%20managementGroup.%26nbsp%3B%20If%20you%20don't%20have%20that%20permission%2C%20you%20can%20deploy%20the%20template%20to%20any%20other%20managementGroup%2C%20subscription%20or%20resourceGroup.%26nbsp%3B%20Also%20a%20reminder%20that%20even%20though%20the%20subscription%20is%20created%20at%20the%20tenant%20scope%2C%20the%20template%20deployment%20does%20not%20need%20to%20match%20that%20scope.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20far%2C%20this%20is%20a%20very%20simple%20example%2C%20but%20you%20can%20also%20create%20a%20subscription%20and%20deploy%20resources%20to%20that%20subscription%20in%20the%20same%20template.%26nbsp%3B%20There%20is%20a%20little%20more%20orchestration%20required%20here%20because%20you%E2%80%99re%20actually%20targeting%20multiple%20scopes%20within%20the%20same%20template.%26nbsp%3B%20And%2C%20in%20order%20to%20target%20the%20subscription%2C%20you%20need%20the%20subscriptionId%20or%20GUID%20that%20was%20assigned%20to%20the%20subscription%20when%20it%20was%20created.%26nbsp%3B%20This%20next%20%3CA%20title%3D%22Azure%20QuickStart%20Sample%20-%20Create%20Subscription%2C%20resourceGroup%2C%20storageAccount%22%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-quickstart-templates%2Ftree%2Fmaster%2Fmanagementgroup-deployments%2Fcreate-subscription-resourcegroup%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Esample%3C%2FA%3E%20will%20perform%20each%20of%20these%20steps%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ECreate%20the%20subscription%20(this%20is%20shown%20in%20the%20previous%20sample)%3C%2FLI%3E%0A%3CLI%3ERetrieve%20the%20subscriptionId%20from%20the%20newly%20created%20alias%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%22outputs%22%3A%20%7B%0A%20%20%20%20%22subscriptionId%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22type%22%3A%20%22string%22%2C%0A%20%20%20%20%20%20%20%20%22value%22%3A%20%22%5Breference(parameters('subscriptionAlias')).subscriptionId%5D%22%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E3.%20Pass%20that%20subscriptionId%20to%20the%20next%20deployment%20in%20the%20template%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%22type%22%3A%20%22Microsoft.Resources%2Fdeployments%22%2C%0A%22apiVersion%22%3A%20%222020-10-01%22%2C%0A%22name%22%3A%20%22%5Bconcat('nested-createResourceGroup-'%2C%20parameters('resourceGroupName'))%5D%22%2C%0A%22location%22%3A%20%22%5Bparameters('location')%5D%22%2C%0A%22properties%22%3A%20%7B%0A%20%20%20%20%22expressionEvaluationOptions%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22scope%22%3A%20%22inner%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22mode%22%3A%20%22Incremental%22%2C%0A%20%20%20%20%22parameters%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22subscriptionId%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20this%20cannot%20be%20referenced%20directly%20on%20the%20subscriptionId%20property%20of%20the%20deployment%20so%20needs%20to%20be%20nested%20one%20level%0A%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22%5Breference(resourceId('Microsoft.Resources%2Fdeployments'%2C%20concat('createSubscription-'%2C%20parameters('subscriptionAlias')))).outputs.subscriptionId.value%5D%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A...%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E4.%20Create%20a%20new%20deployment%20in%20the%20new%20subscription%20that%20creates%20the%20resourceGroup%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%20%20%22type%22%3A%20%22Microsoft.Resources%2Fdeployments%22%2C%0A%20%20%20%20%22apiVersion%22%3A%20%222020-10-01%22%2C%0A%20%20%20%20%22name%22%3A%20%22%5Bconcat('createResourceGroup-'%2C%20parameters('resourceGroupName'))%5D%22%2C%0A%20%20%20%20%22subscriptionId%22%3A%20%22%5Bparameters('subscriptionId')%5D%22%2C%0A%20%20%20%20%22location%22%3A%20%22%5Bparameters('location')%5D%22%2C%0A%20%20%20%20%22properties%22%3A%20%7B%20...%20%7D%0A...%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20then%20finally%20deploy%20the%20resources%20to%20that%20resourceGroup.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat's%20more%20complex%20than%20just%20creating%20a%20subscription%20because%20all%20of%20the%20orchestration%20is%20handled%20within%20a%20single%20template.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20your%20scenario%20requires%20a%20different%20scope%20of%20deployment%20or%20more%20steps%20then%20you%20may%20not%20want%20to%20include%20in%20that%20single%20template.%26nbsp%3B%20If%20I%20wanted%20to%20break%20this%20down%20into%20multiple%20steps%20for%20orchestration%20in%20a%20pipeline%20it%20can%20be%20as%20simple%20as%20two%20steps.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1285647886%22%20id%3D%22toc-hId-1285001128%22%3EStep%201%20%E2%80%93%20Create%20the%20Subscription%3C%2FH3%3E%0A%3CP%3EPerforming%20this%20step%20separately%20can%20be%20useful%20if%20you%20do%20not%20want%20to%20provide%20a%20user%20or%20service%20principal%20with%20permission%20to%20create%20template%20deployments%20at%20a%20given%20scope.%26nbsp%3B%20Once%20the%20subscription%20is%20created%2C%20the%20principal%20that%20created%20the%20subscription%20is%20an%20owner%20of%20that%20subscription%20and%20can%20deploy%20templates%20to%20that%20newly%20created%20subscription.%26nbsp%3B%20This%20means%20that%20the%20only%20permission%20the%20principal%20needs%20outside%20of%20the%20subscription%2C%20is%20the%20permission%20to%20create%20one.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAt%20this%20writing%2C%20the%20Azure%20PowerShell%20does%20not%20have%20a%20built-in%20command%20to%20create%20a%20subscription%20but%20you%20can%20always%20invoke%20any%20Azure%20REST%20api%20using%20the%20Invoke-AzRestMethod.%26nbsp%3B%20This%20%3CA%20title%3D%22Create%20a%20subscription%20via%20PowerShell%22%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-quickstart-templates%2Fblob%2Fmaster%2Fmanagementgroup-deployments%2Fcreate-subscription-resourcegroup%2FCreate-SubscriptionAlias.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Escript%3C%2FA%3E%20shows%20how%20to%20do%20that%20to%20create%20a%20subscription%20through%20an%20alias%20resource%20using%20the%20following%20command.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%20.%5CCreate-SubscriptionAlias.ps1%20-aliasName%20%22newSub%22%20-DisplayName%20%22demo%20subscription%22%20-billingAccount%20%221234567%22%20-enrollmentAccount%20%22654321%22%20-workLoad%20DevTest%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20need%20to%20set%20the%20correct%20parameter%20values%20for%20the%20billingAccount%20and%20enrollmentAccount%20which%20you%20can%20discover%20using%20%3CA%20title%3D%22Sample%20Script%20-%20Get%20Billing%20Scope%20Information%22%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-quickstart-templates%2Fblob%2Fmaster%2Fmanagementgroup-deployments%2Fcreate-subscription%2FGet-BillingAccountsEA.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ethis%3C%2FA%3E%20script%20from%20the%20top%20of%20this%20article.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--521806577%22%20id%3D%22toc-hId--522453335%22%3EStep%202%20%E2%80%93%20Deploy%20the%20template.%3C%2FH3%3E%0A%3CP%3ENext%2C%20keeping%20with%20our%20greenfield%20scenario%2C%20where%20the%20subscription%20is%20created%20in%20the%20same%20workflow%20or%20pipeline%20that%20deploys%20this%20next%20template%2C%20we%E2%80%99ll%20create%20a%20subscription-level%20deployment.%26nbsp%3B%20If%20we%20were%20running%20an%20automated%20pipeline%2C%20this%20%3CA%20title%3D%22Azure%20QuickStart%20-%20Create%20Resource%20Group%20and%20Assign%20Permission%22%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-quickstart-templates%2Ftree%2Fmaster%2Fsubscription-deployments%2Fcreate-rg-lock-role-assignment%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Esample%3C%2FA%3E%20would%20be%20a%20good%20example%20for%20the%20next%20step.%26nbsp%3B%20The%20sample%20will%20create%20a%20new%20resourceGroup%20in%20the%20subscription%2C%20lock%20it%20and%20assign%20a%20principal%20access%20to%20that%20resourceGroup.%26nbsp%3B%20From%20here%20you%20could%20deploy%20resources%20to%20the%20subscription%20(or%20resourceGroup)%20or%20simply%20make%20it%20available%20for%20the%20principal%20to%20use.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat's%20a%20quick%20overview%20of%20how%20to%20leverage%20this%20new%20capability%2C%20in%20just%20a%20few%20scenarios%2C%20that%20you%20can%20use%20to%20automate%20new%20workloads%20in%20Azure.%26nbsp%3B%20Let%20me%20know%20how%20it%20goes%20or%20if%20you%20have%20any%20questions%20about%20automating%20subscription%20creation%20in%20your%20environments.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1839961%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20more%20and%20more%20enterprises%20embrace%20Azure%2C%20complete%20end-to-end%20automation%20for%20standing%20up%20workloads%20in%20the%20cloud%20is%20one%20of%20the%20most%20important%20steps%20to%20running%20at%20scale.%26nbsp%3B%20The%20latest%20release%20(2020-09-01)%20of%20the%20Microsoft.Subscriptions%20resource%20provider%20enables%20subscription%20creation%20via%20templates.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Co-Authors
Version history
Last update:
‎Apr 26 2021 10:25 AM
Updated by: