Python SDK call example

Updated at:

Call the LookupEvents operation with the ActionTrail Python SDK to query audit events.

Step 1: Review the API documentation

Review the LookupEvents reference for parameters and permissions. All operations are listed in the API overview.

Step 2: Create and authorize a RAM user

Identity

This operation supports Alibaba Cloud accounts (root accounts), RAM users, and RAM roles. For details, see Identity.

This example uses a RAM User.

Log on to the RAM console and create a RAM user. Create a RAM user.

Credential

On the target RAM User details page, click the Authentication tab. In the User AccessKey section, click Create AccessKey to create an AccessKey pair. Create an AccessKey pair.

Important

The AccessKey secret is shown only once during creation and cannot be retrieved later. Store it securely.

Permission

  1. On the Users page, find the target RAM user and click Add Permissions in the Actions column.

  2. Search for actiontrail and select a policy based on least privilege.

    • AliyunActionTrailFullAccess: Grants full management permissions for ActionTrail.

    • AliyunActionTrailReadOnlyAccess: Grants read-only permissions for ActionTrail.

    Note

    If system policies are insufficient, create a custom policy. Create a custom policy. Authorization.

    This example uses the AliyunActionTrailReadOnlyAccess system policy.

  3. Click OK.

Step 3: Call the API

This example uses the Python SDK. Other SDKs follow a similar process. For SDK downloads, see ActionTrail SDK. For usage details, see Call methods.

Prepare the Python environment

  1. Download and install Python.

    Download Python 3.

  2. Check your Python version.

    • Linux and macOS

      Open a terminal and run python --version.

    • Windows

      • In the Python installation directory, double-click python.exe.

      • Open Command Prompt.

        Press Win+R to open the Run dialog box, enter python, and click Confirm.

Configure environment variables

Store the AccessKey pair in environment variables to avoid hardcoding credentials. Configure AccessKey environment variables.

Install dependencies

pip install alibabacloud_credentials
pip install alibabacloud_actiontrail20200706==2.1.0
pip install alibabacloud_tea_console

Download the sample code

  1. In OpenAPI Explorer, go to the debugging URL for the LookupEvents operation.

  2. The LookupEvents operation has no required parameters. Leave them blank and click Initiate Call.

  3. Click the SDK Sample tab, and then click the Python tab.

    The sample code for Python is displayed.

  4. Click Download Complete Project.

  5. Extract the package and open the alibabacloud_sample directory.

Run the code

Run the following command:

python sample.py

Sample output:

{
  "EndTime": "2023-07-11T08:13:04Z",
  "NextToken": "eyJhY2NvdW50IjoiMTY5ODI3MjMyODU0ODQ2NyIsImV2ZW50SWQiOiJFNTdGNjFGRS04RjdCLTVDRjAtODA3Ni03NjAwNEQyMkMxQTYiLCJsb2dJZCI6IjQ1LTE2OTgyNzIzMjg1NDg0NjciLCJ0aW1lIjoxNjg5MDYzMTE2MD****",
  "RequestId": "7743F214-925E-5602-936C-3CCCD5FBACD8",
  "Events": [
    {
      "eventId": "33859C72-1C50-55B3-A857-27FAA358****",
      "eventVersion": 1,
      "eventSource": "actiontrail.cn-hangzhou.aliyuncs.com",
      "requestParameters": {
        "AcsProduct": "Actiontrail",
        "UserAgent": "Workbench/1.0",
        "ClientPort": 11880,
        "Region": "cn-hangzhou"
      },
      "sourceIpAddress": "192.168.XX.XX",
      "userAgent": "api.aliyun.com",
      "eventRW": "Read",
      "eventType": "ApiCall",
      "userIdentity": {
        "sessionContext": {
          "attributes": {
            "mfaAuthenticated": "false",
            "creationDate": "2023-07-11T08:12:34Z"
          }
        },
        "accountId": "169827232854****",
        "principalId": "169827232854****",
        "type": "root-account",
        "userName": "root"
      },
      "serviceName": "Actiontrail",
      "additionalEventData": {
        "CallerBid": "2****"
      },
      "apiVersion": "2020-07-06",
      "requestId": "33859C72-1C50-55B3-A857-27FAA358641C",
      "eventTime": "2023-07-11T08:12:34Z",
      "isGlobal": false,
      "acsRegion": "cn-hangzhou",
      "eventName": "LookupEvents"
    }
  ],
  "StartTime": "2023-07-04T08:13:34Z"
}