All Products
Search
Document Center

ApsaraDB for Redis:Usage example of the ApsaraDB for Redis API

Last Updated:Aug 21, 2023

This topic describes how to use Alibaba Cloud SDK for Python to call the DescribeInstancesOverview operation of ApsaraDB for Redis. You can call this operation to query the basic information of instances.

View the API documentation

Before you call a Tair API operation, we recommend that you read the List of operations by function topic to learn the limits and parameters associated with the operation.

Create a RAM user and grant permissions to the RAM user

If you have created a Resource Access Management (RAM) user and granted permissions to the user, you can skip this step.

  1. Create a RAM user.

    1. Log on to the RAM console, go to the Users page, and then click Create User.

    2. Set the Logon Name parameter to redis-openapi-operator and select OpenAPI Access for the Access Mode parameter.

    3. Click OK and save the AccessKey ID and AccessKey secret.

  2. Grant permissions to the RAM user.

    1. Go to the Users page, find the RAM user that you created, and then click Add Permissions in the Actions column.

    2. In the panel that appears, enter AliyunKvstore in the search box and then select the AliyunKvstoreReadOnlyAccess policy.

      Note

      The AliyunKvstoreReadOnlyAccess policy provides read permissions on Tair instances, and the AliyunKvstoreFullAccess policy provides read and write permissions on Tair instances. You can also specify a custom policy based on your business requirements. For more information, see Authorize RAM users to manage ApsaraDB for Redis instances by using custom policies.

    3. Click OK.

Call an API operation

This section describes how to use Alibaba Cloud SDK for Python to call a Tair API operation. You can use SDKs for other programming languages in a similar way. For more information, see ApsaraDB for Redis SDK.

Prepare a Python environment

Download and install Python 3. If you have installed Python, you can run the python --version command to query its version number.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • Linux and macOS: Set the <access_key_id> and <access_key_secret> parameters in the following commands to your AccessKey ID and AccessKey secret and run the commands on your terminal device.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows: Create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, specify your AccessKey ID and AccessKey secret by using the environment variables, and then restart the system.

Install dependencies

Run the following commands in order on your terminal device to install dependencies:

pip install alibabacloud_credentials
pip install alibabacloud_r_kvstore20150101
pip install alibabacloud_tea_console

Download the sample code

  1. Go to the page of the DescribeInstancesOverview operation in the OpenAPI portal.

  2. On the left-side Parameters tab, specify the parameters. In this example, RegionId is set to cn-hangzhou.

  3. On the right-side SDK Sample Code tab, select Python for the Languages parameter and click Download Project to download the package of sample code.

  4. Decompress the package and go to the alibabacloud_sample directory.

Run the code

Run the following command:

python sample.py

Sample result:

[
    LOG
]{
    "headers": {
        "date": "Tue, 04 Jul 2023 10:03:39 GMT",
        "content-type": "application/json;charset=utf-8",
        "content-length": "1665",
        "connection": "keep-alive",
        "keep-alive": "timeout=25",
        "vary": "Accept-Encoding",
        "access-control-allow-origin": "*",
        "access-control-expose-headers": "*",
        "x-acs-request-id": "F2F4B6CB-3C03-51A0-9300-F78BC94E****",
        "x-acs-trace-id": "4d9ceb1f6cb98086cd8333f0bb30****",
        "etag": "1YlaXvHZ3H5CjZic1Oeo****"
    },
    "statusCode": 200,
    "body": {
        "Instances": [
            {
                "ArchitectureType": "standard",
                "Capacity": 4096,
                "ChargeType": "PostPaid",
                "ConnectionDomain": "r-bp1u7sm6mp6h83****.redis.rds.aliyuncs.com",
                "CreateTime": "2023-02-07T05:49:56Z",
                "EngineVersion": "6.0",
                "InstanceClass": "redis.shard.large.ce",
                "InstanceId": "r-bp1u7sm6mp6h83****",
                "InstanceName": "",
                "InstanceStatus": "Normal",
                "InstanceType": "Redis",
                "NetworkType": "VPC",
                "RegionId": "cn-hangzhou",
                "ResourceGroupId": "rg-acfmz7u4zzr****",
                "VSwitchId": "vsw-bp1ah4djy7ghm3ump****",
                "VpcId": "vpc-bp1p5zk2l6ixtgx6v****",
                "ZoneId": "cn-hangzhou-h"
            },
            {
                "ArchitectureType": "rwsplit",
                "Capacity": 2048,
                "ChargeType": "PostPaid",
                "ConnectionDomain": "r-bp1qsslcssrjeb****.redis.rds.aliyuncs.com",
                "CreateTime": "2023-02-07T05:53:33Z",
                "EngineVersion": "6.0",
                "InstanceClass": "tair.rdb.with.proxy.2g",
                "InstanceId": "r-bp1qsslcssrjeb****",
                "InstanceName": "",
                "InstanceStatus": "Normal",
                "InstanceType": "tair_rdb",
                "NetworkType": "VPC",
                "RegionId": "cn-hangzhou",
                "ResourceGroupId": "rg-acfmz7u4zzr****",
                "VSwitchId": "vsw-bp1ah4djy7ghm3ump****",
                "VpcId": "vpc-bp1p5zk2l6ixtgx6v****",
                "ZoneId": "cn-hangzhou-h"
            }
        ],
        "RequestId": "F2F4B6CB-3C03-51A0-9300-F78BC94EE283",
        "TotalCount": 2
    }
}