Create Azure Storage Programmatically
This document provides guidance on how to programmatically create Azure Storage accounts using various SDKs and tools. Creating storage accounts programmatically is essential for automating cloud infrastructure deployment and management.
Prerequisites:
- An Azure subscription.
- Permissions to create resources within your subscription.
- Appropriate SDKs or tools installed (e.g., Azure CLI, PowerShell, Azure SDKs for your language).
Using Azure CLI
The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources. You can create a storage account with a single command.
az storage account create \
--name mystorageaccountname \
--resource-group myresourcegroup \
--location westus2 \
--sku Standard_LRS \
--kind StorageV2
Explanation:
--name: A globally unique name for your storage account.--resource-group: The name of the resource group where the storage account will be created.--location: The Azure region where the storage account will reside.--sku: The performance and replication tier (e.g.,Standard_LRS,Premium_LRS,Standard_GRS).--kind: The type of storage account (e.g.,StorageV2for general-purpose v2 accounts).
Using Azure PowerShell
Azure PowerShell provides cmdlets for managing Azure resources. The New-AzStorageAccount cmdlet is used for creating storage accounts.
New-AzStorageAccount `
-Name "mystorageaccountname" `
-ResourceGroupName "myresourcegroup" `
-Location "West US 2" `
-SkuName "Standard_LRS" `
-Kind "StorageV2"
Using Azure SDKs
The Azure SDKs offer libraries for various programming languages, allowing you to integrate storage account creation into your applications.
.NET Example
This example demonstrates creating a storage account using the Azure SDK for .NET.
using Azure.Identity;
using Azure.ResourceManager.Storage;
using Azure.ResourceManager.Storage.Models;
// ...
var subscriptionId = "your-subscription-id";
var client = new StorageManagementClient(subscriptionId, new DefaultAzureCredential());
var storageAccountData = new StorageAccountCreateParameters
{
Location = "westus2",
Sku = new Sku(StorageAccountSkuName.StandardLRS),
Kind = StorageAccountKind.StorageV2,
Tags = new Dictionary<string, string>
{
{ "environment", "development" }
}
};
var operation = await client.StorageAccounts.BeginCreateAsync(
"myresourcegroup",
"mystorageaccountname",
storageAccountData
);
var result = await operation.UpdateStatusAsync();
Console.WriteLine($"Storage account created: {result.Data.Name}");
Python Example
This example shows how to create a storage account using the Azure SDK for Python.
from azure.identity import DefaultAzureCredential
from azure.mgmt.storage import StorageManagementClient
subscription_id = "your-subscription-id"
credential = DefaultAzureCredential()
client = StorageManagementClient(credential, subscription_id)
storage_account_params = {
"location": "westus2",
"sku": {"name": "Standard_LRS"},
"kind": "StorageV2",
"tags": {"environment": "development"}
}
poller = client.storage_accounts.begin_create(
"myresourcegroup",
"mystorageaccountname",
storage_account_params
)
result = poller.result()
print(f"Storage account created: {result.name}")
Using ARM Templates / Bicep
For declarative infrastructure as code, you can use Azure Resource Manager (ARM) templates or Bicep files to define and deploy your storage accounts.
ARM Template Snippet
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"metadata": {
"description": "Name of the storage account"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for the storage account"
}
},
"skuName": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_RAGRS",
"Standard_ZRS",
"Premium_LRS",
"Premium_ZRS"
],
"metadata": {
"description": "Storage account SKU name"
}
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-09-01",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('skuName')]"
},
"kind": "StorageV2",
"properties": {}
}
]
}
Considerations
- Naming Conventions: Storage account names must be globally unique, lowercase, and between 3 and 24 characters long.
- SKUs: Choose the SKU that best fits your performance and redundancy requirements.
- Kind:
StorageV2is recommended for most scenarios. - Resource Groups: Organize your storage accounts within resource groups for better management.
- Access Control: Implement appropriate access control mechanisms (e.g., role-based access control, shared access signatures) after creation.
By leveraging these programmatic methods, you can efficiently create and manage Azure Storage resources as part of your cloud automation workflows.