11

I have gone through the https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_elasticache.html.

How to create an Elasticache Redis template using AWS-CDK. It would be more helpful if you share the sample code.

Raghavendra
  • 519
  • 1
  • 11
  • 25

2 Answers2

32

sorry for late response but can be usefull for others.

CDK doesn't have an High Level Construct to create a Redis Cluster, but you can create it by using the low level construct api.

For Redis Cluster types you can take a look at this: https://aws.amazon.com/it/blogs/database/work-with-cluster-mode-on-amazon-elasticache-for-redis/

I've created a single Redis (no replication) Cluster using typescript like this:

const subnetGroup = new CfnSubnetGroup(
  this,
  "RedisClusterPrivateSubnetGroup",
  {
    cacheSubnetGroupName: "privata",
    subnetIds: privateSubnets.subnetIds,
    description: "subnet di sviluppo privata"
  }
);
const redis = new CfnCacheCluster(this, `RedisCluster`, {
  engine: "redis",
  cacheNodeType: "cache.t2.small",
  numCacheNodes: 1,
  clusterName: "redis-sviluppo",
  vpcSecurityGroupIds: [vpc.defaultSecurityGroup.securityGroupId],
  cacheSubnetGroupName: subnetGroup.cacheSubnetGroupName
});
redis.addDependsOn(subnetGroup);

If you need a Redis (cluster enabled) Cluster you can you replication group

const redisSubnetGroup = new CfnSubnetGroup(
  this,
  "RedisClusterPrivateSubnetGroup",
  {
    cacheSubnetGroupName: "privata",
    subnetIds: privateSubnets.subnetIds,
    description: "subnet di produzione privata"
  }
);

const redisReplication = new CfnReplicationGroup(
  this,
  `RedisReplicaGroup`,
  {
    engine: "redis",
    cacheNodeType: "cache.m5.xlarge",
    replicasPerNodeGroup: 1,
    numNodeGroups: 3,
    automaticFailoverEnabled: true,
    autoMinorVersionUpgrade: true,
    replicationGroupDescription: "cluster redis di produzione",
    cacheSubnetGroupName: redisSubnetGroup.cacheSubnetGroupName
  }
);
redisReplication.addDependsOn(redisSubnetGroup);

Hope this help.

Stefano Corallo
  • 321
  • 2
  • 3
  • 12
    To ensure that the subnet group gets created before the replication group, I would change cacheSubnetGroupName to this: `cacheSubnetGroupName: redisSubnetGroup.ref`. Otherwise, I was seeing a generated cloudformation template without any cacheSubnetGroupName, so the group will get created in your default subnet or, if you don't have one in your VPC, you'll get the error "The account does not have any default subnets". – Charles Fulton Feb 21 '20 at 22:17
2

I just struggled for hours creating a Redis cluster mode enabled with just one shard but two nodes. If you create a CfnReplicationGroup with num_cache_clusters=2, it will create a primary & replica node.

The trick is to create a CfnReplicationGroup with num_cache_clusters=2 and set the cache_parameter_group_name="default.redis6.x.cluster.on"

Then it will create a redis cache with cluster mode enable, one shard but two nodes

MasterD
  • 128
  • 2
  • 6