Skip to content

Commit cc01543

Browse files
authored
fix(aws-elasticache): wrong dimensionsMap key for serverless cluster (#68)
* fix(aws-elasticache): wrong dimensionsMap key for serverless cluster * update integ test * update snapshot * apply lint
1 parent 59b284f commit cc01543

File tree

10 files changed

+34781
-20
lines changed

10 files changed

+34781
-20
lines changed

src/aws-elasticache/serverless-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ abstract class ServerlessCacheBase extends Resource implements IServerlessCache
298298
return new Metric({
299299
namespace: 'AWS/ElastiCache',
300300
dimensionsMap: {
301-
CacheClusterId: this.serverlessCacheName,
301+
clusterId: this.serverlessCacheName,
302302
},
303303
metricName,
304304
...props,

test/aws-elasticache/integ.elasticache-serverless-cache.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
1+
import { IntegTest, ExpectedResult, AwsApiCall } from '@aws-cdk/integ-tests-alpha';
22
import * as cdk from 'aws-cdk-lib';
3+
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
34
import { Construct } from 'constructs';
45
import * as ocf from '../../src';
56
import { DailySnapshotTime, Engine, MajorVersion } from '../../src/aws-elasticache';
67

78
class ElastiCacheStack extends cdk.Stack {
9+
public readonly alarms: cloudwatch.IAlarm[] = [];
10+
811
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
912
super(scope, id, props);
1013

@@ -80,11 +83,11 @@ class ElastiCacheStack extends cdk.Stack {
8083
userGroup,
8184
});
8285

83-
serverlessCache.metric('CacheHits', {}).createAlarm(this, 'CacheHitsAlarm', {
86+
const cacheHitsAlarm = serverlessCache.metric('CacheHits', {}).createAlarm(this, 'CacheHitsAlarm', {
8487
threshold: 50,
8588
evaluationPeriods: 1,
8689
});
87-
serverlessCache.metricBytesUsedForCache().createAlarm(this, 'BytesUsedForCacheAlarm', {
90+
const bytesUsedAlarm = serverlessCache.metricBytesUsedForCache().createAlarm(this, 'BytesUsedForCacheAlarm', {
8891
threshold: 50,
8992
evaluationPeriods: 1,
9093
});
@@ -93,6 +96,10 @@ class ElastiCacheStack extends cdk.Stack {
9396
evaluationPeriods: 1,
9497
});
9598

99+
// Add the alarms to the array to assert on them later
100+
// ElastiCacheProcessingUnitsAlarm is not added because ElastiCacheProcessingUnits metric data is not available without executing commands
101+
this.alarms.push(cacheHitsAlarm, bytesUsedAlarm);
102+
96103
const role = new cdk.aws_iam.Role(this, 'TestRole', { assumedBy: new cdk.aws_iam.AccountRootPrincipal() });
97104
iamUser.grantConnect(role);
98105
importedIamUser.grantConnect(role);
@@ -104,6 +111,31 @@ const app = new cdk.App();
104111

105112
const testCase = new ElastiCacheStack(app, 'ElastiCacheServerlessCacheStack');
106113

107-
new IntegTest(app, 'ElastiCacheServerlessCacheTest', {
114+
const integ = new IntegTest(app, 'ElastiCacheServerlessCacheTest', {
108115
testCases: [testCase],
109116
});
117+
118+
const describeAlarmsCall = integ.assertions
119+
.awsApiCall(
120+
'cloudwatch',
121+
'DescribeAlarmsCommand',
122+
{
123+
AlarmNames: testCase.alarms.map(a => a.alarmName),
124+
},
125+
testCase.alarms.map((_, i) => `MetricAlarms.${i}.StateValue`),
126+
)
127+
.waitForAssertions({
128+
totalTimeout: cdk.Duration.minutes(2),
129+
}) as AwsApiCall;
130+
131+
// In the current version of aws-cdk-lib, awsApiCall cannot generate the correct policy for CloudWatch API calls
132+
// https://github.com/aws/aws-cdk/pull/33078
133+
describeAlarmsCall.waiterProvider?.addToRolePolicy({
134+
Effect: 'Allow',
135+
Action: ['cloudwatch:DescribeAlarms'],
136+
Resource: ['*'],
137+
});
138+
139+
for (const [i, _] of testCase.alarms.entries()) {
140+
describeAlarmsCall.assertAtPath(`MetricAlarms.${i}.StateValue`, ExpectedResult.stringLikeRegexp('OK'));
141+
}

test/aws-elasticache/integ.elasticache-serverless-cache.ts.snapshot/ElastiCacheServerlessCacheStack.assets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
}
1515
}
1616
},
17-
"7d17558a1f0e522b0468782535824aa34653fc3af184543bd2aebed790e5ea9c": {
17+
"d64b11b9e7a58a88f37eced4fb01e4e1930be8cfc214dd9339b0146a929ed9c3": {
1818
"source": {
1919
"path": "ElastiCacheServerlessCacheStack.template.json",
2020
"packaging": "file"
2121
},
2222
"destinations": {
2323
"current_account-current_region": {
2424
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
25-
"objectKey": "7d17558a1f0e522b0468782535824aa34653fc3af184543bd2aebed790e5ea9c.json",
25+
"objectKey": "d64b11b9e7a58a88f37eced4fb01e4e1930be8cfc214dd9339b0146a929ed9c3.json",
2626
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
2727
}
2828
}

test/aws-elasticache/integ.elasticache-serverless-cache.ts.snapshot/ElastiCacheServerlessCacheStack.template.json

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@
732732
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
733733
"Dimensions": [
734734
{
735-
"Name": "CacheClusterId",
735+
"Name": "clusterId",
736736
"Value": {
737737
"Ref": "ElastiCacheServerlessCluster09082A11"
738738
}
@@ -752,7 +752,7 @@
752752
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
753753
"Dimensions": [
754754
{
755-
"Name": "CacheClusterId",
755+
"Name": "clusterId",
756756
"Value": {
757757
"Ref": "ElastiCacheServerlessCluster09082A11"
758758
}
@@ -772,7 +772,7 @@
772772
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
773773
"Dimensions": [
774774
{
775-
"Name": "CacheClusterId",
775+
"Name": "clusterId",
776776
"Value": {
777777
"Ref": "ElastiCacheServerlessCluster09082A11"
778778
}
@@ -910,6 +910,24 @@
910910
}
911911
}
912912
},
913+
"Outputs": {
914+
"ExportsOutputRefCacheHitsAlarm8EBEC2EA7A07A227": {
915+
"Value": {
916+
"Ref": "CacheHitsAlarm8EBEC2EA"
917+
},
918+
"Export": {
919+
"Name": "ElastiCacheServerlessCacheStack:ExportsOutputRefCacheHitsAlarm8EBEC2EA7A07A227"
920+
}
921+
},
922+
"ExportsOutputRefBytesUsedForCacheAlarm18D5884D881686E0": {
923+
"Value": {
924+
"Ref": "BytesUsedForCacheAlarm18D5884D"
925+
},
926+
"Export": {
927+
"Name": "ElastiCacheServerlessCacheStack:ExportsOutputRefBytesUsedForCacheAlarm18D5884D881686E0"
928+
}
929+
}
930+
},
913931
"Parameters": {
914932
"BootstrapVersion": {
915933
"Type": "AWS::SSM::Parameter::Value<String>",

test/aws-elasticache/integ.elasticache-serverless-cache.ts.snapshot/ElastiCacheServerlessCacheTestDefaultTestDeployAssertB226C8E2.assets.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
{
22
"version": "36.0.0",
33
"files": {
4-
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
4+
"4984c845346313a408899c8ff361d3b7b97953a9d4202e47694ef2a101f4b5c3": {
5+
"source": {
6+
"path": "asset.4984c845346313a408899c8ff361d3b7b97953a9d4202e47694ef2a101f4b5c3.bundle",
7+
"packaging": "zip"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "4984c845346313a408899c8ff361d3b7b97953a9d4202e47694ef2a101f4b5c3.zip",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
},
17+
"dbfafc122f2e9b8a060842904d5df7178d0e17369c63a7e472a779e2fe222876": {
518
"source": {
619
"path": "ElastiCacheServerlessCacheTestDefaultTestDeployAssertB226C8E2.template.json",
720
"packaging": "file"
821
},
922
"destinations": {
1023
"current_account-current_region": {
1124
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
25+
"objectKey": "dbfafc122f2e9b8a060842904d5df7178d0e17369c63a7e472a779e2fe222876.json",
1326
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1427
}
1528
}

0 commit comments

Comments
 (0)