- Newest
- Most votes
- Most comments
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
}
});
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
Relevant content
- asked a year ago
- asked 3 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 5 months ago
- AWS OFFICIALUpdated 4 months 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.