Connecting to Elasticache - Amazon ElastiCache for Redis
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Connecting to Elasticache

The following examples use the Redis client to connect to ElastiCache.

Connecting to a cluster mode disabled cluster

Copy the following program and paste it into a file named ConnectClusterModeDisabled.py.

from redis import Redis import logging logging.basicConfig(level=logging.INFO) redis = Redis(host='primary.xxx.yyyyyy.zzz1.cache.amazonaws.com', port=6379, decode_responses=True, ssl=True, username='myuser', password='MyPassword0123456789') if redis.ping(): logging.info("Connected to Redis")

To run the program, enter the following command:

python ConnectClusterModeDisabled.py

Connecting to a cluster mode enabled cluster

Copy the following program and paste it into a file named ConnectClusterModeEnabled.py.

from rediscluster import RedisCluster import logging logging.basicConfig(level=logging.INFO) redis = RedisCluster(startup_nodes=[{"host": "xxx.yyy.clustercfg.zzz1.cache.amazonaws.com","port": "6379"}], decode_responses=True,skip_full_coverage_check=True) if redis.ping(): logging.info("Connected to Redis")

To run the program, enter the following command:

python ConnectClusterModeEnabled.py