Creating Azure storage account by using Azure Python SDK and by AZ module in Powershell

Creating Azure storage account by using Azure Python SDK and by AZ module in Powershell

In this example Azure resource group is already created. Also the credentials like Client ID, Subscription, Client Secret and Tenant are exported in the bash shell.

To get information how to use Azure SDK api go to the official api page:

https://docs.microsoft.com/en-us/python/api/overview/azure/?view=azure-python

Azure SDK for Python:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.storage import StorageManagementClient
from azure.mgmt.storage.models import (
    StorageAccountCreateParameters,
    Sku,
    SkuName,
    Kind
)
import os


AZ_TENANT_ID = os.getenv('AZ_TENANT_ID')
AZ_CLIENT_ID = os.getenv('AZ_CLIENT_ID')
AZ_CLIENT_SECRET = os.getenv('AZ_CLIENT_SECRET')
AZ_SUBSCRIPTION = os.getenv('AZ_SUBSCRIPTION')
GROUP_NAME = 'DR-RES1'
STORAGE_ACCOUNT_NAME = 'drstorage343434'
REGION = 'westus'

credentials = ServicePrincipalCredentials(
    client_id=AZ_CLIENT_ID,
    secret=AZ_CLIENT_SECRET,
    tenant=AZ_TENANT_ID
)

subscription = AZ_SUBSCRIPTION
storage_client = StorageManagementClient(credentials, subscription)

try:
    print(f'Creating storage account')
    storage_create = storage_client.storage_accounts.create(
            GROUP_NAME,
            STORAGE_ACCOUNT_NAME,
            StorageAccountCreateParameters(
                sku=Sku(name=SkuName.standard_ragrs),
                kind=Kind.storage,
                location=REGION
            )
        )
    print(f'Storage {STORAGE_ACCOUNT_NAME} has been created')

except Exception as e:
    print(f'Unable to create storage account. \nReason: {e}')

Az module is a new module and it is cross-platform module. Therefore, it has also become a priority for Azure PowerShell to have cross-platform support. 

Creating storage account by using Az module in Powershell:

$ resourcegroup = 'DR-RES1'

$ New-AzStorageAccount -ResourceGroupName $resourcegroup -name "storagepshell3434" -location 'westus' -SkuNam Standard_LRS -kind StorageV2

StorageAccountName ResourceGroupName Location SkuName      Kind      AccessTier CreationTime        ProvisioningState EnableHttpsTrafficOnly
------------------ ----------------- -------- -------      ----      ---------- ------------        ----------------- ----------------------
storagepshell3434  DR-RES1           westus   Standard_LRS StorageV2 Hot        06/26/2019 19:15:05 Succeeded         True

Next go to the Azure portal, resource group that the storage accounts has been created and verify if two storage account have been created:

Leave a Reply