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.