Skip to content

Instantly share code, notes, and snippets.

@SaschaMoellering
Created January 23, 2015 19:36
Show Gist options
  • Save SaschaMoellering/89b5046eeb776ca233b0 to your computer and use it in GitHub Desktop.
Save SaschaMoellering/89b5046eeb776ca233b0 to your computer and use it in GitHub Desktop.
Clustered Redis in multiple AZs in a VPC with replication
def create_redis_cluster(region, infrastructure_name, security_groups, subnet_ids, availability_zones,
cache_node_type):
cluster_name = "{0}-cache-cluster".format(infrastructure_name)
replication_group = "{0}-rg".format(infrastructure_name)
cluster_subnet = "{0}-cluster-subnet".format(infrastructure_name)
print "Creating Redis cluster {0} using security groups {1} and subnet {2} in AZs {3} with cache node type {4}"\
.format(cluster_name, security_groups, subnet_ids, availability_zones, cache_node_type)
conn = boto.elasticache.connect_to_region(region)
cache_subnet_group_name = "{0}-cache-sgn".format(infrastructure_name)
conn.create_cache_subnet_group(
cache_subnet_group_name=cache_subnet_group_name,
cache_subnet_group_description=cluster_subnet,
subnet_ids=subnet_ids
)
cnt = 1
for az in availability_zones:
print "Create cluster node in AZ {0}".format(az)
cluster_id = "{0}00{1}".format(infrastructure_name, cnt)
if cnt == 1:
# Creating Redis master
conn.create_cache_cluster(cache_cluster_id=cluster_id, num_cache_nodes=1, cache_node_type=cache_node_type,
engine="redis", cache_subnet_group_name=cache_subnet_group_name,
security_group_ids=security_groups, auto_minor_version_upgrade=True,
preferred_availability_zone=az)
else:
conn.create_cache_cluster(cache_cluster_id=cluster_id,
replication_group_id=replication_group)
counter = 0
while counter < 35:
counter += 1
cluster_desc = conn.describe_cache_clusters(cache_cluster_id=cluster_id)
cluster_status = cluster_desc["DescribeCacheClustersResponse"]["DescribeCacheClustersResult"]["CacheClusters"][0]["CacheClusterStatus"]
if "available" not in cluster_status:
time.sleep(10)
elif "available" in cluster_status:
break
if cnt == 1:
conn.create_replication_group(replication_group_id=replication_group, primary_cluster_id=cluster_id,
replication_group_description="Redis-Replication-Group")
time.sleep(20)
cnt += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment