通过 CDK 为 ElasticCache (Redis) 创建 CloudWatch 警报

问题描述 投票:0回答:1

我有一个工作正常的现有 Redis“实例”,由 CDK 代码创建:

redis = new elasticache.CfnReplicationGroup(this, id + 'RedisCluster', {
            port: 6379,
            clusterMode: 'disabled',
            replicasPerNodeGroup: 1,
            cacheNodeType: cache.t4g.micro,
            engine: 'Redis',
            multiAzEnabled: true,
            autoMinorVersionUpgrade: false,
            cacheParameterGroupName: parameterGroup.ref,
            engineVersion: '7.0',
            cacheSubnetGroupName: subnetGroup.cacheSubnetGroupName,
            securityGroupIds: [securityGroup.securityGroupId],
            replicationGroupDescription: 'My Redis',
        });

现在我想添加一个 CloudWatch 警报,我正在尝试这样的事情:

const alarm = new cloudwatch.Alarm(this, 'MyAlarm', {
        metric: new cloudwatch.Metric({
            namespace: "AWS/ElastiCache",
            metricName: "DatabaseMemoryUsagePercentage",
            period: Duration.minutes(1),
            unit: cloudwatch.Unit.PERCENT,
            statistic: cloudwatch.Stats.AVERAGE,
            dimensionsMap: {
                CacheClusterId: << WHAT VAUE GOES HERE >>,             
                // CacheNodeId: << IS THIS NEEDED? >>,
            },
        }),
        threshold: 60, // Memory Utilisation percentage
        evaluationPeriods: 1,
        datapointsToAlarm: 1,
    });

我不知道该用什么

CacheClusterId

我正在尝试从

redis
变量获取值,例如

    console.log(redis.replicationGroupId); // Returns null
    console.log(redis.primaryClusterId); // Returns null
    console.log(redis.node); // Returns some data, but can't seem to find a unique identifier that I would need

如何向此 Redis“实例”添加 CloudWatch 警报

redis aws-cloudformation aws-cdk cloudwatch-alarms elastic-cache
1个回答
0
投票

我想出了以下解决方案:

const redis = new elasticache.CfnReplicationGroup( /* ... */ );

const alarm = new cloudwatch.Alarm( /* ... */ ,
    {  // props
       namespace: "AWS/ElastiCache",
       dimensionsMap: {
           CacheClusterId: redis.ref + '-001',
       },
       /* ... */
    }

因为我也有一个只读副本,所以我需要为另一个节点重复此代码,(例如

CacheClusterId: redis.ref + '-002'

我不喜欢我的解决方案,因为

-001
-002
是硬编码的。如果有人知道避免对这些值进行硬编码的巧妙方法,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.