skip to Main Content

I am trying to create an ElastiCache (Redis) cluster and a Lambda function that will write to this Redis cluster. Both these 2 resources will be created as a part of the same CDK stack written in typescript language and in their own constructs (i.e one construct for the Redis cluster and the other for the lambda function)

I am able to create a Redis cluster in the VPC using the below CDK code… This gets deployed fine.

export class RedisCluster extends Construct {
  private redisCluster: CfnCacheCluster;
    constructor(scope: Construct, config: BuildConfig, id: string) {
        super(scope, id);

        const subnetGrpIdAndName     = `dev-subnet-grp`;
        const paramGrpId             = `dev-param-grp`;
        const redisClusterIdAndName  = `dev-redis`;

        const targetVpc =  Vpc.fromLookup(this, "VPC", {
            isDefault:false,
            vpcName: config.vpcName,
        });

        const privateSubnet = targetVpc.selectSubnets({
          subnetType:SubnetType.PRIVATE
        });

        const securityGroup = SecurityGroup.fromSecurityGroupId(this, 'XXXXX',config.securityGroupId ,{
          mutable: false
        });

        const cacheSubnetGroup = new CfnSubnetGroup(this,`${subnetGrpIdAndName}`,{
          description:`${redisClusterIdAndName}`,
          subnetIds: privateSubnet.subnetIds,
          cacheSubnetGroupName: `${subnetGrpIdAndName}`,
        });

        const cacheParameterGroup = new CfnParameterGroup(this,`${paramGrpId}`,{
          description:`${redisClusterIdAndName}`,
          cacheParameterGroupFamily: "redis6.x",
          properties: {"timeout":"15"}
        });

         this.redisCluster = new CfnCacheCluster(this, `${redisClusterIdAndName}` , {
          clusterName: `${redisClusterIdAndName}`,
          engine: "redis",
          cacheNodeType: "cache.t3.medium",
          engineVersion: "6.x",
          numCacheNodes: 1,
          cacheSubnetGroupName:cacheSubnetGroup.ref,
          cacheParameterGroupName: cacheParameterGroup.ref,
          vpcSecurityGroupIds: [securityGroup.securityGroupId],
         });
           
  }

  public getRedisCluster(): CfnCacheCluster {
      return this.redisCluster;
  }
}

Now I want to create a lambda function in its own construct and connect it to the Redis cluster that will be created above. (Important note – Both the Redis cluster and lambda function will need to be deployed as a part of the same CDK stack and thus the Redis cluster host URL will not be available at the deployment time and I do not want to hardcode the Redis host URL in the lambda environment variable and instead refer the Redis cluster typescript instance to supply those value)

Below is the part of the construct to create the lambda function and you can see that the Redis host is provided as an environment variable. Since the Redis cluster is getting created as a part of the same CDK stack thus I want that Lambda constructs instance to use the Redis cluster instance and get the parameter it needs to be able to connect to the Redis cluster. I am hoping that at the time of deployment the Redis construct instance will provide some way to access the deployed configuration values like host name.

 const lambdaFunction = new lambda.Function(this, 'XXXXX', {
      functionName: `XXXXX',
      runtime: lambda.Runtime.PYTHON_3_8,
      handler: config.handler,
      role: customXXXRole,
      code: lambda.Code.fromAsset('xxx.zip'),
      timeout: Duration.seconds(config.timeout),
      memorySize: config.memory_size,
      reservedConcurrentExecutions: config.reserved_concurrent_executions,
      layers: [layer],
      environment: {     
        redis_host: XXXXXXX.abc.com/test:5555
      }
    });

I am new to AWS CDK world and trying to follow AWS suggested best practices (https://docs.aws.amazon.com/cdk/latest/guide/best-practices.html) for creating the CDK stack.

Thank you.

2

Answers


  1. You can use the AWS parameter store to store your environment-specific configurations and access from the Lamda function using role-based access.

    Try this ,
    https://aws.amazon.com/premiumsupport/knowledge-center/lambda-common-environment-variables/

    Login or Signup to reply.
  2. This should be the code you are looking for to set up the Lambda

    const lambdaFunction = new lambda.Function(this, 'XXXXX', {
          functionName: `XXXXX',
          runtime: lambda.Runtime.PYTHON_3_8,
          handler: config.handler,
          role: customXXXRole,
          code: lambda.Code.fromAsset('xxx.zip'),
          timeout: Duration.seconds(config.timeout),
          memorySize: config.memory_size,
          reservedConcurrentExecutions: config.reserved_concurrent_executions,
          layers: [layer],
          environment: {     
            redis_host: `${this.redisCluster.attrRedisEndpointAddress}:${this.redisCluster.attrRedisEndpointPort}`
          }
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search