RAM Python SDK call example
Call the GetRole operation of Resource Access Management (RAM) by using the Python SDK to query the details of a RAM role.
Step 1: Review the API documentation
In the API overview, find the GetRole operation for querying RAM role details. Review its request parameters and required permissions.
Step 2: Create a RAM user and grant permissions
Identity
You can call this operation as an Alibaba Cloud account, a RAM user, or a RAM role. For more information, see Identities.
This example uses a RAM user to call the operation.
Log on to the RAM console and create a RAM user. For detailed instructions, see Create a RAM user.
Credential
Go to the details page of the target RAM user. On the Authentication tab, in the User AccessKeys section, click Create AccessKey. For detailed instructions, see Create an AccessKey pair.
An AccessKey secret is displayed only upon creation and cannot be retrieved later. Save it securely.
Authorization
-
Go to the Users page. In the Actions column for the target RAM user, click Grant Permissions.
-
In the search box, enter the keyword
ramand select the appropriate permission policy based on the principle of least privilege.-
AliyunRAMFullAccess: Grants full management permissions on RAM.
-
AliyunRAMReadOnlyAccess: Grants read-only permissions on RAM.
NoteIf the system policies do not meet your requirements, you can create a custom policy. For more information, see Create a custom policy and Grant permissions to a RAM user.
This example selects the AliyunRAMReadOnlyAccess system policy.
-
-
Click OK.
Step 3: Call the API
The following example uses the Python SDK. The usage is similar for SDKs in other languages. For more information, see RAM SDK. For other call methods, see Methods to call an API.
Prepare the Python environment
Download and install Python 3.
After installation, run python3 --version in your terminal to verify the installation.
Configure environment variables
To avoid hard-coding your AccessKey pair, this example reads credentials from environment variables. For detailed instructions, see Configure environment variables on Linux, macOS, and Windows.
Download the sample code
-
In OpenAPI Explorer, go to the debug page for GetRole.
-
Enter the request parameters and click Send Request. Verify that the call is successful.
-
On the SDK Sample tab, click the Python tab, and then click Download Project.
-
Decompress the sample code package and navigate to the alibabacloud_sample directory.
Install dependencies
We recommend that you create a virtual Python environment in the project directory and install the dependencies within it.
Bash
cd alibabacloud_sample
# Create virtual environment
python3 -m venv .venv
# Activate virtual environment
source .venv/bin/activate
# Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_ram20150501
pip install alibabacloud_tea_console
PowerShell
cd alibabacloud_sample
# Create virtual environment
python3 -m venv .venv
# Activate virtual environment
.\.venv\Scripts\Activate.ps1
# Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_ram20150501
pip install alibabacloud_tea_console
Run the sample code
Run the following command:
python3 sample.py
The command returns the following output:
{
"headers": {
"date": "Thu, 17 Aug 2023 09:51:21 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "541",
"connection": "keep-alive",
"keep-alive": "timeout=25",
"access-control-allow-origin": "*",
"access-control-expose-headers": "*",
"x-acs-request-id": "7970371F-BADA-50A5-AA51-30E80A994F98",
"x-acs-trace-id": "6853c1607c7a59f156b84f77926df218",
"etag": "59sNsxjTg3R+qgFD/KOl/tQ1"
},
"statusCode": 200,
"body": {
"RequestId": "7970371F-BADA-50A5-AA51-30E80A994F98",
"Role": {
"Arn": "acs:ram::151266687691****:role/test",
"AssumeRolePolicyDocument": "{\n \"Statement\": [{\n \"Action\": \"sts:AssumeRole\",\n \"Effect\": \"Allow\",\n \"Principal\": {\"Service\": [\"fc.aliyuncs.com\"]}}],\n \"Version\": \"1\"}",
"CreateDate": "2020-10-21T06:41:25Z",
"Description": "Service Role for FC to operate other resource",
"MaxSessionDuration": 3600,
"RoleId": "32852457160891****",
"RoleName": "test",
"UpdateDate": "2020-10-21T06:41:25Z"
}
}
}