How to trigger python script on an Azure windows VM using event or messaganing azure services ( event grid , service bus .. )

Anaya, Ines 0 Reputation points
2023-03-06T10:33:05.3566667+00:00

Hello,

I need to trigger a python script execution on an Azure windows VM after receiving a message or an event ( maybe event from event grid or a message from service bus )

How to send event or messages to a VM and trigger with it the script execution ?

For your information I can not use an Azure function because I have some installed packages on the VM that the script needs to run.

Thank you in advance.

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
548 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,158 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,124 questions
Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
319 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Musaif Shaikh 85 Reputation points
    2023-03-06T10:44:08.8533333+00:00

    To trigger a Python script execution on an Azure Windows VM after receiving a message or an event, you can use Azure Event Grid or Azure Service Bus to send the event or message, and then use Azure Automation or Azure Virtual Machine Extension to execute the script.

    Here's a general outline of how you can achieve this:

    1. Create an Azure Event Grid topic or an Azure Service Bus topic/queue.
    2. Create a subscription to the topic/queue that sends events or messages to the VM.
    3. Install Azure Virtual Machine Extension on the VM or create an Azure Automation account and configure it to run the script.
    4. Write a script that listens to the events or messages from the topic/queue and triggers the execution of the desired script on the VM.
    5. Ensure that the script execution is properly configured and secured.

    For example, if you choose to use Azure Event Grid, you can create a subscription that sends events to a webhook on the VM. The webhook can be a URL that points to your Python script. When an event is received, the Python script can then be executed.

    Here's a sample code snippet that listens to events from Azure Event Grid using Python:

    import os
    import sys
    import json
    from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.route('/', methods=['POST'])
    def event_handler():
        event = json.loads(request.data.decode('utf-8'))
        # Trigger the execution of the desired script here
        return '', 204
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
    
    

  2. Musaif Shaikh 85 Reputation points
    2023-03-06T10:44:30.4133333+00:00

    To trigger a Python script execution on an Azure Windows VM after receiving a message or an event, you can use Azure Event Grid or Azure Service Bus to send the event or message, and then use Azure Automation or Azure Virtual Machine Extension to execute the script.

    Here's a general outline of how you can achieve this:

    1. Create an Azure Event Grid topic or an Azure Service Bus topic/queue.
    2. Create a subscription to the topic/queue that sends events or messages to the VM.
    3. Install Azure Virtual Machine Extension on the VM or create an Azure Automation account and configure it to run the script.
    4. Write a script that listens to the events or messages from the topic/queue and triggers the execution of the desired script on the VM.
    5. Ensure that the script execution is properly configured and secured.

    For example, if you choose to use Azure Event Grid, you can create a subscription that sends events to a webhook on the VM. The webhook can be a URL that points to your Python script. When an event is received, the Python script can then be executed.

    Here's a sample code snippet that listens to events from Azure Event Grid using Python:

    import os
    import sys
    import json
    from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.route('/', methods=['POST'])
    def event_handler():
        event = json.loads(request.data.decode('utf-8'))
        # Trigger the execution of the desired script here
        return '', 204
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
    
    
    0 comments No comments

  3. Mike Urnun 9,676 Reputation points Microsoft Employee
    2023-03-15T06:42:47.1333333+00:00

    Hello @Anaya, Ines We received the survey prompt you submitted on this thread. While I see that you had follow-up questions on @Musaif Shaikh 's answer below, I wanted to weigh in and help if I can. Regarding the below statement:

    For your information I can not use an Azure function because I have some installed packages on the VM that the script needs to run.

    I just wanted to better understand why you couldn't run the script via Python Functions. Since Python Functions run only on Linux OS, I assume the particular Python packages your script depends on are only compatible with Windows OS? Could you let me know what those packages are so I can find out why it wasn't possible with Function Apps?

    To answer your broader question, have you considered running the entire thing as Containers instead of Azure VM? That way, you should be able to take advantage both of event-driven programming and running the packages of choice. Another approach, which might be simpler, is to use Logic Apps to trigger on ServiceBus or EventGrid events and call the VM as the downstream action in the workflow. If you're interested in hearing more about how it could be done, let me know which approach resonates more with you and I'd be happy to provide more instructions and corresponding resources.


  4. tbgangav-MSFT 10,386 Reputation points
    2023-04-03T17:09:38.2366667+00:00

    Hi @Anaya, Ines , As explained here / here, you could try to remotely execute a shell script (which internally calls your python script) that are sitting in your VM. High-level steps involved are:

    1. Save your python script in the VM.
    2. Create a shell script in the VM which basically just calls your python script.
    3. Create an Azure Automation runbook as explained in above mentioned reference links.
    4. Configure to trigger the runbook on an event from event grid or message from service bus (by even creating a webhook for the runbook if required).
    0 comments No comments