read existing redis elasticache properties in CDK

0

Hi team,

I have an existing Amazon ElastiCache Redis cluster set up via CDK in our AWS account with the following configuration

const redis = new elasticache.CfnReplicationGroup(this, 'cache', {
      replicationGroupDescription: 'xxxxxxxx',
      atRestEncryptionEnabled: true,
      multiAzEnabled: true,
      cacheNodeType: 'cache.t2.micro',
      cacheSubnetGroupName: sng.cacheSubnetGroupName,
      engine: "redis",
      engineVersion: '7.1',
      numNodeGroups: 1, 
      replicasPerNodeGroup: 1, 
      securityGroupIds: [sg_id],
      transitEncryptionEnabled: true, 
    })

In a completely separate CDK project, I need my Lambda function to connect to this existing Redis cluster.

I couldn’t find a way to retrieve the Redis cluster’s host and port using methods like .fromName() or .fromArn().

Is there a way to access the Redis cluster’s endpoint and port in my CDK project so I can inject them as environment variables in my Lambda function?

(The Redis cluster was created way before in another project and now it's running in AWS)

in this new CDK project, I just need to grab it so I can retrieve its host and port to use them in my lambda code to connect to the Elasticache cluster.

Thanks!

2 Answers
0

Hello.

Looking at the properties in the document below, it seems that you can get the endpoint and port number with "attrPrimaryEndPointAddress" and "attrPrimaryEndPointPort".
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_elasticache.CfnReplicationGroup.html

Therefore, if you add the code as shown below, you can output it from the stack, so I think it is possible to reference it from other stacks.

new cdk.CfnOutput(this, 'RedisEndpoint', {
  value: redis.attrPrimaryEndPointAddress
});

new cdk.CfnOutput(this, 'RedisPort', {
  value: redis.attrPrimaryEndPointPort
});

If Lambda is in the same stack, I think you can set the environment variables as follows.
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html

const fn = new lambda.Function(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_18_X,
  handler: 'index.handler',
  code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
  environment:{
    "ENDPOINT": redis.attrPrimaryEndPointAddress,
    "PORT": redis.attrPrimaryEndPointPort
  }
});
profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • yes i did that in the project where the redis was created, but as I said in the question I'm in a completely different project and I the redis cluster already created and exist, so in this new project I need to read the existing redis cluster properties. imagine that the redis was created via the was console and already exist and now in a CDK stack I want to get the host and port from that redis cluster

  • As far as I know, there is no other way than "attrPrimaryEndPointAddress" to obtain an endpoint from an existing ElastiCache using CDK. This means you need to modify your existing ElastiCache CDK code to output the endpoint. If you just output the endpoint, there is almost no need to modify the code.
    If you really want to obtain it using only the code, you will need to use the AWS SDK as it will not be possible to obtain it using only the CDK. In other words, you need to write code that creates a Lambda with a custom resource and retrieves the endpoint from that Lambda. However, it is easier to store in Systems Manager Parameter Store or set as CDK environment variables rather than creating custom resources. https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/welcome.html

  • yes i already did that attrPrimaryEndPointAddress in cdk project1, but I'm in completely separate project I did not see how to read output from cdk project 1 from a completely different project and different stack, the only way I see I that project1 output those in SSM parameter store and I pick the parameter names and put them as vars in projects 2 then read the host and port in project 2 using the ssm parameters names

  • If a parameter is output as a stack output using "cdk.CfnOutput", you can import it to another stack using "cdk.Fn.importValue" as shown below. Therefore, you should be able to reference the endpoint by adding the following code to the Lambda stack.

    const RedisEndpoint = cdk.Fn.importValue('RedisEndpoint');
    
0

Hi,

The proper way to read properties of an AWS resource is not from CDK (which could be out of sync from reality) but from the AWS service API itself via the SDK of your preferred languages.

So, for Python, it would be those in https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/elasticache.html

Best,

Didier

profile pictureAWS
EXPERT
answered a month 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.

Guidelines for Answering Questions