Google Cloud Redis

2.21.0 · active · verified Sat Mar 28

Google Cloud Redis (also known as Memorystore for Redis) is a fully managed Redis service on Google Cloud Platform. This Python client library allows developers to programmatically create, manage, and interact with Redis instances, supporting operations like instance creation, deletion, and listing. The library is actively maintained by Google and receives frequent updates, typically aligning with broader Google Cloud client library releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Google Cloud Redis client and list existing Redis instances within a specified Google Cloud project and location. For authentication, ensure `GOOGLE_APPLICATION_CREDENTIALS` is set up (e.g., pointing to a service account key file, or rely on Application Default Credentials).

import os
from google.cloud import redis_v1

# Your Google Cloud Project ID
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-project-id')
# The region where you want to list instances, e.g., 'us-central1'
location = 'global' # Or a specific region like 'us-central1'

def list_redis_instances(project_id: str, location: str):
    """Lists all Redis instances in a given project and location."""
    client = redis_v1.CloudRedisClient()
    parent = f"projects/{project_id}/locations/{location}"

    try:
        # The `list_instances` method returns an iterable of Instance objects.
        instances = client.list_instances(parent=parent)

        if not instances:
            print(f"No Redis instances found in {location} for project {project_id}.")
            return

        print(f"Redis instances in {location} for project {project_id}:")
        for instance in instances:
            print(f"  Name: {instance.name}")
            print(f"  Host: {instance.host}")
            print(f"  Port: {instance.port}")
            print(f"  State: {instance.state.name}")
            print(f"  Tier: {instance.tier.name}")
            print(f"  Memory size (GB): {instance.memory_size_gb}")
            print("  ---")

    except Exception as e:
        print(f"Error listing Redis instances: {e}")

if __name__ == "__main__":
    # Set GOOGLE_CLOUD_PROJECT environment variable or replace 'your-project-id'
    # Ensure GOOGLE_APPLICATION_CREDENTIALS is set up for authentication
    list_redis_instances(project_id, location)

view raw JSON →