Skip to content

Valkey cache with CDK

0

Hello team,

I’m planning to create an ElastiCache Valkey cache with AWS CDK to store simple key-value pairs (strings). I wanted to check if ElastiCache Valkey caches are supported by AWS CDK, so I can proceed with creating it using CDK.

Thanks in advance!

2 Answers
1
Accepted Answer

Hello.

You can also create an ElastiCache for Valkey using the CDK using the L1 constructor.
https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_elasticache/CfnReplicationGroup.html

import * as elasticache from "aws-cdk-lib/aws-elasticache";

export class CacheConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    const description = "valkey-cache";
    new elasticache.CfnReplicationGroup(this, "ReplicationGroup", {
      replicationGroupDescription: description,
      engine: "valkey",
      engineVersion: "7.2",
      cacheNodeType: "cache.t3.micro",
      cacheSubnetGroupName: new elasticache.CfnSubnetGroup(this, "SubnetGroup", {
        description,
        subnetIds: vpc.selectSubnets({ subnetGroupName: "private-subnet" }).subnetIds,
      }).ref,
      cacheParameterGroupName: new elasticache.CfnParameterGroup(this, "ParameterGroup", {
        description,
        cacheParameterGroupFamily: "valkey7",
      }).ref,
      numNodeGroups: 1,
      replicasPerNodeGroup: 1,
      securityGroupIds: [mySecurityGroup.securityGroupId],
      atRestEncryptionEnabled: true,
      transitEncryptionEnabled: true,
    });
  }
}
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

1

As of now, there is no direct support for ElastiCache Valkey caches in the AWS Cloud Development Kit (CDK). The AWS CDK does not currently have hand-written (L2) constructs specifically for ElastiCache, including Valkey caches.

However, this doesn't mean you can't use CDK to create an ElastiCache Valkey cache. You have a couple of options:

  1. You can use the automatically generated L1 constructs in CDK. These L1 constructs allow you to work with ElastiCache resources in the same way you would use CloudFormation directly. This means you can create and manage your Valkey cache using CDK, but you'll need to specify the resources and properties as you would in CloudFormation.

  2. You can search the Construct Hub for third-party ElastiCache construct libraries that might support Valkey caches. These community-created constructs could potentially offer more convenient ways to work with ElastiCache Valkey in CDK.

It's worth noting that ElastiCache for Valkey is a relatively new offering, announced in October 2024. It provides support for Valkey 7.2 and is available in all AWS regions where ElastiCache is supported. While CDK doesn't have specific constructs for it yet, you can still create and manage Valkey caches using the more low-level constructs available in CDK.

If you decide to proceed with CDK, you'll need to refer to the CloudFormation documentation for AWS::ElastiCache to understand the available resources and properties for creating your Valkey cache. This approach will require more manual configuration compared to using high-level constructs, but it will allow you to create your Valkey cache using CDK.
Sources
aws-cdk-lib.aws_elasticache module · AWS CDK
Announcing Amazon ElastiCache for Valkey - AWS

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.