top of page
Writer's pictureSupport lwplabs

Creating Azure Resources using Python Scripting - Step-By-Step

Updated: May 4, 2023

Step1: Create an Azure Account

Create an Azure an with Azure, you need to create an Azure account. Go to the Azure website and sign up for a free account. You will need to provide some personal information, including your email address and phone number.


Step 2: Install the Azure CLI


Azure CLI is a command line tool that allows you to manage Azure resources.

To install the Azure CLI, open a comman dorterminal window and enter the command:




Step 3: Create a Virtual Environment for Python CLI on your system.


Create a virtual environment for Python

To ensure your Python scriptswork correctly with de pendencies, you must create a virtual environment. Open a command prompt or terminal window and enter the following command:

python3-m venv myenv
Source myenv/bin/Active

This will create and open a virtual environment called do "myenv".


Step 4: Install the Azure SDK for Python

To use Python to interact with Azure services, you must install the Azure SDK for Python. To install the SDK, enter the following command in your command prompt or terminal window:

pip install azure

This will install the Azure SDK for Python and its dependencies.


Step 5: Python Script


Scripting Now that you've setup your Azure account, installed the Azure CLI, and installed the Azure SDK for Python, you can start scripting Python.

In this example we will create a new Azure Storage account. Open the text file and create a new file named "create_storage_account.py".

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

blob_service_client = BlobServiceClient.from_connection_string("DefaultEndpointsProtocol=https;AccountName=<your-storage-account-name>;AccountKey=<your-storage-account-key>;EndpointSuffix=core.windows.net")

container_client = blob_service_client.create_container("<your-container-name>")

blob_client = container_client.upload_blob("<your-blob-name>", b"Hello World!")

Be sure to replace <your-storage-account-name> and <your-storage-account-key> with your actual storage account name and key. Also, replace <your-container-name> and <your-blob-name> with your desired container and blob names.


Step 6: Run the Python script

To run the Python script, open a command or terminal window and navigate to the directory where the script is located. Enter the following command:

python create_storage_account.py

This will enable your Python code and create a new Azure Storage account with the container and blob.


In summary, writing Python with Azure is a powerful way to manage Azure resources. By following these steps and optimizing your content for search engines, you can create a useful and informative guide for others to learn.


33 views0 comments

Comments


bottom of page