Home > Net >  How to run Python scripts on Azure?
How to run Python scripts on Azure?

Time:02-08

I am trying to run some Python code in Azure, for the first time ever. I Googled 'how to run Python scripts in Azure' and came across the following link.

https://docs.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual-python-3

Essentially, I need to do this.

Open the textual editor by selecting Edit on the MyFirstRunbook-Python3 pane.

Add the following code to authenticate to Azure:

import os
from azure.mgmt.compute import ComputeManagementClient
import azure.mgmt.resource
import automationassets

def get_automation_runas_credential(runas_connection):
    from OpenSSL import crypto
    import binascii
    from msrestazure import azure_active_directory
    import adal

    # Get the Azure Automation RunAs service principal certificate
    cert = automationassets.get_automation_certificate("AzureRunAsCertificate")
    pks12_cert = crypto.load_pkcs12(cert)
    pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM,pks12_cert.get_privatekey())

    # Get run as connection information for the Azure Automation service principal
    application_id = runas_connection["ApplicationId"]
    thumbprint = runas_connection["CertificateThumbprint"]
    tenant_id = runas_connection["TenantId"]

    # Authenticate with service principal certificate
    resource ="https://management.core.windows.net/"
    authority_url = ("https://login.microsoftonline.com/" tenant_id)
    context = adal.AuthenticationContext(authority_url)
    return azure_active_directory.AdalAuthentication(
    lambda: context.acquire_token_with_client_certificate(
            resource,
            application_id,
            pem_pkey,
            thumbprint)
    )

# Authenticate to Azure using the Azure Automation RunAs service principal
runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")
azure_credential = get_automation_runas_credential(runas_connection)

When I run that code, I get this error.

Failed
Traceback (most recent call last):  File "C:\WPy64-3800\python-3.8.0.amd64\lib\automationassets.py", line 155, in _issue_request    response = urllib.request.urlopen(request)  File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 222, in urlopen    return opener.open(url, data, timeout)  File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 531, in open    response = meth(req, response)  File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 640, in http_response    response = self.parent.error(  File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 569, in error    return self._call_chain(*args)  File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 502, in _call_chain    result = func(*args)  File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 649, in http_error_default    raise HTTPError(req.full_url, code, msg, hdrs, fp)urllib.error.HTTPError: HTTP Error 404: Not FoundDuring handling of the above exception, another exception occurred:Traceback (most recent call last):  File "C:\Temp\nmcijfgi.bro\a761c289-67d9-493a-99b2-3ba7d46a7cd9", line 36, in <module>    runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")  File "C:\WPy64-3800\python-3.8.0.amd64\lib\automationassets.py", line 232, in get_automation_connection    connection = _client.get_connection_asset(name)  File "C:\WPy64-3800\python-3.8.0.amd64\lib\automationassets.py", line 187, in get_connection_asset    return self._issue_request(url, method=self._GET)  File "C:\WPy64-3800\python-3.8.0.amd64\lib\automationassets.py", line 158, in _issue_request    raise AutomationAssetNotFound()automationassets.AutomationAssetNotFound

Do I need 'cert', 'pks12_cert', or 'pem_pkey'? I don't think I have those. I'm not even sure what those things are. Again, I am trying to setup my Azure environment to run a Python script, which works totally fine on my desktop (Spyder).

CodePudding user response:

Most likely you haven't created an Azure Run As Account. Follow the steps here to do so.

  •  Tags:  
  • Related