0

I want to automate a azure resources(ex- start/stop VM) currently I am using Automation Account runbook and its working fine but I need to implement a framework something lie this :

1)Trigger runbook whenever put a new object(excel sheet) in azure bucket. 2)Read the excel sheet for input variables

Below is the runbook code

Somebody please tell me best way to trigger runbook which suits the above framework

""" Azure Automation documentation : https://aka.ms/azure-automation-python-documentation Azure Python SDK documentation : https://aka.ms/azure-python-sdk """ import os import sys 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)

Initialize the compute management client with the RunAs credential and specify the subscription to work against.

compute_client = ComputeManagementClient( azure_credential, str(runas_connection["SubscriptionId"]) )

print('\nStart VM') async_vm_start = compute_client.virtual_machines.start(

'resource1', 'vm1') async_vm_start.wait() ''' print('\nStop VM') async_vm_stop=compute_client.virtual_machines.power_off(resource_group_name, vm_name) async_vm_stop.wait()'''

suman
  • 1

1 Answers1

0

I believe one way to accomplish your requirement of triggering runbook whenever a new blob (or in your words 'object') is added in an Azure Storage container (on in your words 'bucket') is by leveraging Event Subscription (Event Grid). For related information, refer this document.

To illustrate it in a better way, you would have to go to Azure Portal -> your Storage account (that is of StorageV2 kind) -> Events tile -> More options -> Logic Apps -> Have 2 Steps as shown in below screenshot that does validate if a new storage blob is added and then runs the required runbook

You may also add next steps like sending mail after runbook execution is completed, etc.

Hope this helps!

enter image description here

KrishnaG
  • 3,340
  • 2
  • 6
  • 16
  • Hi KrishnaG runbook supports till 2.x python version so I am implementing it using azure function, I deployed function using visual studio but not sure how to proceed further can you give me your mail id and thank you for your help – suman Feb 04 '20 at 12:11