Alibaba Cloud Resource Access Management (RAM) SDK for Python

1.2.1 · active · verified Thu Apr 16

The `alibabacloud-ram20150501` library is the official Python SDK for interacting with Alibaba Cloud's Resource Access Management (RAM) service, allowing developers to programmatically manage users, groups, roles, and access policies. It is part of the broader Alibaba Cloud Python SDK ecosystem. The current version is 1.2.1, and the library is actively maintained with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `alibabacloud-ram20150501` client and create a new RAM user. It's recommended to configure your Alibaba Cloud AccessKey ID and Secret as environment variables (`ALIBABA_CLOUD_ACCESS_KEY_ID`, `ALIBABA_CLOUD_ACCESS_KEY_SECRET`). The `CredentialClient` will automatically pick these up for secure authentication. Replace `'your_value'` placeholders with actual data when using other APIs.

import os
import json
from alibabacloud_ram20150501.client import Client as Ram20150501Client
from alibabacloud_credentials.client import Client as CredentialClient
from alibabacloud_tea_openapi.models import Config as OpenApiConfig
from alibabacloud_ram20150501 import models as ram_20150501_models
from alibabacloud_tea_util.models import RuntimeOptions

# It is highly recommended to use environment variables for AccessKey credentials
# For example:
# export ALIBABA_CLOUD_ACCESS_KEY_ID='your_access_key_id'
# export ALIBABA_CLOUD_ACCESS_KEY_SECRET='your_access_key_secret'

# Configure Client
# The CredentialClient will automatically pick up credentials from environment variables
# or other configured sources.
credential = CredentialClient()
config = OpenApiConfig(
    credential=credential,
    endpoint='ram.aliyuncs.com' # Specify the appropriate endpoint for RAM
)

# Initialize the RAM client
client = Ram20150501Client(config)

# Example: Create a user request
create_user_request = ram_20150501_models.CreateUserRequest(
    user_name='example_ram_user_py_sdk',
    display_name='Example RAM User Python SDK',
    comments='Created by Python SDK quickstart'
)

# Set runtime options (optional)
runtime_options = RuntimeOptions()

try:
    # Call the CreateUser API
    response = client.create_user_with_options(create_user_request, runtime_options)
    print("Successfully created RAM user:")
    print(json.dumps(response.to_map(), indent=2, default=str))
except Exception as error:
    print(f"Error creating RAM user: {error.message}")
    if hasattr(error, 'data') and error.data.get('Recommend'):
        print(f"Recommendation: {error.data.get('Recommend')}")

# Clean up (optional: delete the created user)
# Uncomment and provide the actual user_name to delete
# delete_user_request = ram_20150501_models.DeleteUserRequest(
#     user_name='example_ram_user_py_sdk'
# )
# try:
#     client.delete_user_with_options(delete_user_request, runtime_options)
#     print(f"Successfully deleted RAM user: {'example_ram_user_py_sdk'}")
# except Exception as error:
#     print(f"Error deleting RAM user: {error.message}")

view raw JSON →