1. Dynamic Credential Issuance for Machine Learning Services


    To dynamically issue credentials for Machine Learning services on Azure using Pulumi, we're going to utilize Azure Machine Learning Service resources. We can manage various aspects such as code versions, datasets, models, and endpoints by defining them as infrastructure as code using Pulumi.

    Let's create a practical example where we set up a Machine Learning Workspace and then issue credentials by creating a Service Principal with a role assignment on this workspace. This Service Principal can then be used for authentication from your machine learning services or applications for operations that require authorized access.

    This example will cover creating:

    1. An Azure Resource Group – a container that holds related resources for an Azure solution.
    2. An Azure Machine Learning Workspace – which provides the space where you can easily develop, train, manage, deploy, and track machine learning models.
    3. An Azure Active Directory Service Principal – which is an identity created for use with applications, hosted services, and automated tools to access Azure resources. This can be used for services that need to authenticate without requiring user interaction.
    4. A role assignment to the Service Principal – which provides it with the appropriate permissions to access the Machine Learning Workspace.

    Here's how to do it with Pulumi in Python:

    import pulumi from pulumi_azure_native import resources from pulumi_azure_native import machinelearningservices from pulumi_azuread import Application, ServicePrincipal, ServicePrincipalPassword from pulumi_azure_native.authorization import RoleAssignment from pulumi_random import RandomPassword # Create a new resource group resource_group = resources.ResourceGroup("ml_resource_group") # Create an Azure Machine Learning Workspace ml_workspace = machinelearningservices.Workspace("ml_workspace", resource_group_name=resource_group.name, sku=machinelearningservices.SkuArgs( name="Basic", ), location=resource_group.location, description="Pulumi Azure ML Workspace" ) # Create an Azure AD application for the Service Principal ad_application = Application("ml_ad_application") # Generate a random password for the Service Principal password = RandomPassword("ml_sp_password", length=32, special=True) # Create a Service Principal linked to the Azure AD application service_principal = ServicePrincipal("ml_service_principal", application_id=ad_application.application_id) # Assign a password to the Service Principal sp_password = ServicePrincipalPassword("ml_sp_password", service_principal_id=service_principal.id, value=password.result, end_date="2099-01-01T00:00:00Z") # Assign the Contributor role to the Service Principal for the ML Workspace resource role_assignment = RoleAssignment("ml_sp_role_assignment", principal_id=service_principal.id, role_definition_id="/subscriptions/{subscription_id}/providers/Microsoft.Authorization/roleDefinitions/{role_id}".format( subscription_id=pulumi.config.require("subscriptionId"), # Using 'Contributor' role id, this will need to be updated to the correct role id for Contributor role_id="b24988ac-6180-42a0-ab88-20f7382dd24c" ), scope=ml_workspace.id) # Export the Service Principal credentials pulumi.export("client_id", ad_application.application_id) pulumi.export("client_secret", sp_password.value) pulumi.export("tenant_id", ad_application.application_id.apply(lambda id: id.split('-')[0])) # Assumes application ID starts with tenant ID

    This codeblock does the following:

    • It creates an Azure Resource Group to organize our resources.
    • It establishes an Azure Machine Learning Workspace within that resource group.
    • It sets up an Azure AD Application to represent the identity our Machine Learning services will use.
    • It generates a secure random password for our Service Principal.
    • It links the Service Principal to the Azure AD Application.
    • It gives the Service Principal appropriate access to our Machine Learning Workspace by assigning it a contributor role.
    • Finally, it exports the credentials (tenant ID, client ID, and secret) that you can use in your applications or services to interact with the provisioned Azure resources.

    Make sure to replace the {subscription_id} with your Azure Subscription ID and {role_id} with the actual role ID for the Contributor role if it differs from the example.

    Before running this code with Pulumi, ensure you are logged in to the Azure CLI and have selected the appropriate subscription with az login and az account set -s [Subscription Id]. Then, run pulumi up to create the resources, which will output the Service Principal's credentials upon successful deployment.

    These credentials can then be used securely in your machine learning workflows or applications to authenticate against Azure services, enabling dynamic credential issuance as part of your infrastructure deployment process.