Skip to content

Commit 853b8b1

Browse files
author
Kamil Sobol
authored
Add log groups to e2e resource cleanup script (#2142)
* cover log groups in e2e test cleanup * tmp change * Revert "tmp change" This reverts commit 0cc56b8. * condition * condition
1 parent 2c0d316 commit 853b8b1

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@actions/github": "^6.0.0",
5858
"@aws-sdk/client-amplify": "^3.624.0",
5959
"@aws-sdk/client-cloudformation": "^3.624.0",
60+
"@aws-sdk/client-cloudwatch-logs": "^3.624.0",
6061
"@aws-sdk/client-cognito-identity-provider": "^3.624.0",
6162
"@aws-sdk/client-dynamodb": "^3.624.0",
6263
"@aws-sdk/client-iam": "^3.624.0",

scripts/cleanup_e2e_resources.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import {
66
StackStatus,
77
StackSummary,
88
} from '@aws-sdk/client-cloudformation';
9+
import {
10+
CloudWatchLogsClient,
11+
DeleteLogGroupCommand,
12+
DescribeLogGroupsCommand,
13+
DescribeLogGroupsCommandOutput,
14+
LogGroup,
15+
} from '@aws-sdk/client-cloudwatch-logs';
916
import {
1017
Bucket,
1118
DeleteBucketCommand,
@@ -70,6 +77,9 @@ const amplifyClient = new AmplifyClient({
7077
const cfnClient = new CloudFormationClient({
7178
maxAttempts: 5,
7279
});
80+
const cloudWatchClient = new CloudWatchLogsClient({
81+
maxAttempts: 5,
82+
});
7383
const cognitoClient = new CognitoIdentityProviderClient({
7484
maxAttempts: 5,
7585
});
@@ -91,6 +101,7 @@ const TEST_CDK_RESOURCE_PREFIX = 'test-cdk';
91101

92102
/**
93103
* Stacks are considered stale after 2 hours.
104+
* Log groups are considered stale after 7 days. For troubleshooting purposes.
94105
* Other resources are considered stale after 3 hours.
95106
*
96107
* Stack deletion triggers asynchronous resource deletion while this script is running.
@@ -100,6 +111,7 @@ const TEST_CDK_RESOURCE_PREFIX = 'test-cdk';
100111
*/
101112
const stackStaleDurationInMilliseconds = 2 * 60 * 60 * 1000; // 2 hours in milliseconds
102113
const staleDurationInMilliseconds = 3 * 60 * 60 * 1000; // 3 hours in milliseconds
114+
const logGroupStaleDurationInMilliseconds = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds
103115

104116
const isStackStale = (
105117
stackSummary: StackSummary | undefined
@@ -113,6 +125,17 @@ const isStackStale = (
113125
);
114126
};
115127

128+
const isLogGroupStale = (
129+
logGroup: LogGroup | undefined
130+
): boolean | undefined => {
131+
if (!logGroup?.creationTime) {
132+
return;
133+
}
134+
return (
135+
now.getTime() - logGroup.creationTime > logGroupStaleDurationInMilliseconds
136+
);
137+
};
138+
116139
const isStale = (creationDate: Date | undefined): boolean | undefined => {
117140
if (!creationDate) {
118141
return;
@@ -546,3 +569,47 @@ for (const staleDynamoDBTable of allStaleDynamoDBTables) {
546569
);
547570
}
548571
}
572+
573+
const listAllStaleTestLogGroups = async (): Promise<Array<LogGroup>> => {
574+
let nextToken: string | undefined = undefined;
575+
const logGroups: Array<LogGroup> = [];
576+
do {
577+
const listLogGroupsResponse: DescribeLogGroupsCommandOutput =
578+
await cloudWatchClient.send(
579+
new DescribeLogGroupsCommand({
580+
nextToken,
581+
})
582+
);
583+
nextToken = listLogGroupsResponse.nextToken;
584+
listLogGroupsResponse.logGroups
585+
?.filter(
586+
(logGroup) =>
587+
(logGroup.logGroupName?.startsWith(TEST_AMPLIFY_RESOURCE_PREFIX) ||
588+
logGroup.logGroupName?.startsWith(
589+
`/aws/lambda/${TEST_AMPLIFY_RESOURCE_PREFIX}`
590+
)) &&
591+
isLogGroupStale(logGroup)
592+
)
593+
.forEach((item) => {
594+
logGroups.push(item);
595+
});
596+
} while (nextToken);
597+
return logGroups;
598+
};
599+
600+
const allStaleLogGroups = await listAllStaleTestLogGroups();
601+
for (const logGroup of allStaleLogGroups) {
602+
try {
603+
await cloudWatchClient.send(
604+
new DeleteLogGroupCommand({
605+
logGroupName: logGroup.logGroupName,
606+
})
607+
);
608+
console.log(`Successfully deleted ${logGroup.logGroupName} log group`);
609+
} catch (e) {
610+
const errorMessage = e instanceof Error ? e.message : '';
611+
console.log(
612+
`Failed to delete ${logGroup.logGroupName} log group. ${errorMessage}`
613+
);
614+
}
615+
}

0 commit comments

Comments
 (0)