Search code examples
pythonazureazure-log-analyticsazure-runbook

How can I get the shared key from Log Analytics workspace in a Python runbook?


I'm trying to get the primary/shared key from a Log Analytics Workspace in a Python Runbook to use with the Log Collector API for Auth. I've searched everywhere yet can't find anything.


Solution

  • You can use RESTAPI calls to get the Shared Keys for Log Analytics Workspace in Azure Automation Runbook.

    Get Shared Keys from Log Analytics Workspace

    Use below RESTAPI call to fetch the Shared Key for Log Analytics Workspace in a Azure Automation Runbook. (Before calling API call you have to authenticate with your resources using AAD Oauth2)

    # use POST method to get the Shared Key
    https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/sharedKeys?api-version=2020-08-01
    

    MSDOC to access the logs from log analytics workspace

    Steps to use API calls in python from MSFT stack.

    Result

    enter image description here