Python

Get started with Azure libraries (SDK) for Python on LocalStack

Introduction

Azure SDK for Python is a set of libraries that allow you to interact with Azure services using Python. This guide will show you how to use the Azure SDK for Python to interact with LocalStack. The Azure SDK for Python needs the correct SSL certificate before it trusts the LocalStack emulator proxy. You can download the CA certificate from the running emulator // TODO

Getting started

This guide is designed for users who are new to LocalStack for Azure emulator and assumes basic knowledge of the Azure SDK for Python. We will demonstrate how to create an Azure resource group and update it with tags using Python.

Install the packages

Run the following command to install the required packages:

$ pip install azure-mgmt-resource azure-identity

Create a Python file

You can now use the Azure SDK for Python to interact with LocalStack. Create a Python file named provision_rg.py. The code will perform the following actions:

  • Specifies mock credentials for the Azure SDK.
  • Creates a resource group.
  • Updates the resource group with tags.

Paste the following code into the file:

from azure.mgmt.resource import ResourceManagementClient
from azure.identity import ClientSecretCredential

def get_credentials():
    return ClientSecretCredential(tenant_id="tenant-id", client_id="client_id", client_secret="client_secret")

def create_or_update_resource_group(resource_client, group_name, location, tags=None):
    # Provision or update the resource group
    rg_result = resource_client.resource_groups.create_or_update(
        group_name, 
        {"location": location, "tags": tags if tags else {}}
    )
    # Logging the action taken
    action = "Updated" if tags else "Provisioned"
    print(f"{action} resource group {rg_result.name} in the {rg_result.location} region with tags {tags}")

# Setup credentials and client
credential = get_credentials()
subscription_id = "sub-id"
resource_client = ResourceManagementClient(credential, subscription_id)

# Create or update resource groups
create_or_update_resource_group(resource_client, "PythonAzureExample-rg", "centralus")
create_or_update_resource_group(resource_client, "PythonAzureExample-rg", "centralus", {"environment": "test", "department": "tech"})

Run the Python file

You can now run the Python file. Make sure to set the HTTPS_PROXY environment variable to http://localhost:4566 and the REQUESTS_CA_BUNDLE environment variable to the path of the CA certificate.

$ REQUESTS_CA_BUNDLE="path/to/localstack.crt" HTTPS_PROXY="http://localhost:4566" python3 provision_rg.py

The following output will be displayed:

Provisioned resource group PythonAzureExample-rg in the centralus region with tags None
Updated resource group PythonAzureExample-rg in the centralus region with tags {'environment': 'test', 'department': 'tech'}