How to create Azure Subscription using python SDK

Ahsan Javed 0 Reputation points
2023-04-15T16:30:05.36+00:00

Hello, I want to create an Azure subscription using the Python SDK. I am looking for a python Class that I can use in my script to create a new subscription. Any help will be appreciated. Thanks, Ahsan

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-04-16T22:55:48.8433333+00:00
    from azure.mgmt.resource import SubscriptionClient
    from azure.common.credentials import ServicePrincipalCredentials
    
    # Replace with your own values
    subscription_id = 'your-subscription-id'
    tenant_id = 'your-tenant-id'
    client_id = 'your-client-id'
    client_secret = 'your-client-secret'
    
    # Authenticate with Azure using a service principal
    credentials = ServicePrincipalCredentials(
        client_id=client_id,
        secret=client_secret,
        tenant=tenant_id
    )
    
    # Create a SubscriptionClient using the authenticated credentials
    subscription_client = SubscriptionClient(credentials)
    
    # Create a new subscription
    subscription = subscription_client.subscriptions.create(
        subscription_id,
        {
            'displayName': 'My New Subscription',
            'offerType': 'MS-AZR-0017P',
            'owners': [
                {
                    'objectId': 'your-object-id'
                }
            ]
        }
    )
    
    print(subscription)
    

    SubscriptionClient uses your client; can be created with the Azure CLI command az ad sp create-for-rbac command, to sign into Azure. Note, that you will need to replace the values for subscription_id, tenant_id, client_id, client_secret, and objectId with your own values. Also, make sure that the service principal you are using has the necessary permissions to create subscriptions.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.