This topic describes how to use ActionTrail SDK for Python to call the LookupEvents operation to query the details of an audit event.
Step 1: View the API documentation
To call the LookupEvents operation, we recommend that you refer to LookupEvents. For more information about the parameters and permissions that are required to call the operation, see List of operations by function
Step 2: Create a RAM user and grant permissions to the RAM user
Identities
You can use an Alibaba Cloud account, a Resource Access Management (RAM) user, or a RAM role to call this operation. For more information about the differences between the identities, see Identity.
In this example, a RAM user is used to call the operation.
Log on to the RAM console and create a RAM user. For more information about how to create a RAM user, see Create a RAM user.
Credentials
Go to the details page of the RAM user. In the AccessKey section of the Authentication tab, click Create AccessKey to create an AccessKey pair. For more information, see Create an AccessKey pair.
The AccessKey secret of a RAM user is displayed only when the RAM user is created. You cannot view the AccessKey secret after the RAM user is created. Store the AccessKey secret in a secure location.
Authorization
On the Users page in the RAM console, find the RAM user that you want to manage and click Add Permissions in the Actions column.
Enter the keyword
actiontrail
in the search box of the Policy section and select a policy based on the principle of least privilege.AliyunActionTrailFullAccess: grants the required permissions to manage ActionTrail.
AliyunActionTrailReadOnlyAccess: grants read-only permissions on ActionTrail.
NoteIf the system policies do not meet your requirements, you can create a custom policy. For more information, see Create a custom policy and RAM authorization.
In this example, AliyunActionTrailReadOnlyAccess is selected.
Click Grant permissions to complete the authorization.
Step 3: Call an API operation
In this topic, ActionTrail SDK for Python is used to call this operation. You can use SDKs for other programming languages in a similar way. For more information, see ActionTrail SDK. You can use other methods to call operations based on your business requirements. For more information, see Overview.
Prepare a Python environment
Download and install Python.
For more information about how to download Python 3, visit the official website of Python.
Check the version of Python.
Linux or macOS
Open the terminal and run the
python --version
command.Windows
In the Python installation directory, double-click python.exe.
Open the Command Prompt.
Press the Win+R keys to open the Run dialog box, enter python, and then click OK.
Configure environment variables
In this example, environment variables are configured to manage the AccessKey pair and prevent security risks that are caused by hard-coding the AccessKey pair into your business code. For more information about how to configure environment variables, see Configure environment variables in Linux, macOS, and Windows.
Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_actiontrail20200706==2.1.0
pip install alibabacloud_tea_console
Download the sample code.
In OpenAPI Explorer, call the LookupEventsLookupEvents operation.
Click Initiate Call without configuring parameters. The LookupEvents operation does not have required parameters.
Click the SDK Sample Code tab. Then, go to the Languages section and click Python.
The system provides the sample code of ActionTrail SDK for Python.
Click Download Project to download the sample code package.
Decompress the sample code package on your computer and go to the alibabacloud_sample directory.
Run the sample code
Run the following command:
python sample.py
The following command output is displayed:
{
"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"
}