Skip to content

Commit f13cad8

Browse files
committed
fix: avoid invalid token error
Signed-off-by: Kevin Shan <[email protected]>
1 parent f763085 commit f13cad8

File tree

1 file changed

+58
-40
lines changed

1 file changed

+58
-40
lines changed

packages/amplify-codegen-e2e-tests/src/cleanup-e2e-resources.ts

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,24 @@ import fs from 'fs-extra';
88
import path from 'path';
99
import { deleteS3Bucket, sleep } from '@aws-amplify/amplify-codegen-e2e-core';
1010

11-
// Ensure to update scripts/split-e2e-tests.ts is also updated this gets updated
12-
const AWS_REGIONS_TO_RUN_TESTS = [
13-
'ap-east-1',
14-
'ap-northeast-1',
15-
'ap-northeast-2',
16-
'ap-northeast-3',
17-
'ap-south-1',
18-
'ap-southeast-1',
19-
'ap-southeast-2',
20-
'ca-central-1',
21-
'eu-central-1',
22-
'eu-north-1',
23-
'eu-south-1',
24-
'eu-west-1',
25-
'eu-west-2',
26-
'eu-west-3',
27-
'me-south-1',
28-
'sa-east-1',
29-
'us-east-1',
30-
'us-east-2',
31-
'us-west-1',
32-
'us-west-2',
33-
];
11+
/**
12+
* Supported regions:
13+
* - All Amplify regions, as reported https://docs.aws.amazon.com/general/latest/gr/amplify.html
14+
*
15+
* NOTE: The list of supported regions must be kept in sync amongst all of:
16+
* - the internal pipeline that publishes new lambda layer versions
17+
* - amplify-codegen/scripts/split-canary-tests.ts
18+
* - amplify-codegen/scripts/split-e2e-tests.ts
19+
*/
20+
const REPO_ROOT = path.join(__dirname, '..', '..', '..');
21+
const SUPPORTED_REGIONS_PATH = path.join(REPO_ROOT, 'scripts', 'e2e-test-regions.json');
22+
const AWS_REGIONS_TO_RUN_TESTS_METADATA: TestRegion[] = JSON.parse(fs.readFileSync(SUPPORTED_REGIONS_PATH, 'utf-8'));
23+
const AWS_REGIONS_TO_RUN_TESTS = AWS_REGIONS_TO_RUN_TESTS_METADATA.map(region => region.name);
24+
25+
type TestRegion = {
26+
name: string;
27+
optIn: boolean;
28+
};
3429

3530
const reportPathDir = path.normalize(path.join(__dirname, '..', 'amplify-e2e-reports'));
3631

@@ -175,8 +170,20 @@ const getOrphanTestIamRoles = async (account: AWSAccountInfo): Promise<IamRoleIn
175170
*/
176171
const getAmplifyApps = async (account: AWSAccountInfo, region: string): Promise<AmplifyAppInfo[]> => {
177172
const amplifyClient = new aws.Amplify(getAWSConfig(account, region));
178-
const amplifyApps = await amplifyClient.listApps({ maxResults: 50 }).promise(); // keeping it to 50 as max supported is 50
179173
const result: AmplifyAppInfo[] = [];
174+
let amplifyApps = { apps: [] };
175+
try {
176+
amplifyApps = await amplifyClient.listApps({ maxResults: 50 }).promise(); // keeping it to 50 as max supported is 50
177+
} catch (e) {
178+
if (e?.code === 'UnrecognizedClientException') {
179+
// Do not fail the cleanup and continue
180+
console.log(`Listing apps for account ${account.accountId}-${region} failed with error with code ${e?.code}. Skipping.`);
181+
return result;
182+
} else {
183+
throw e;
184+
}
185+
}
186+
180187
for (const app of amplifyApps.apps) {
181188
const backends: Record<string, StackInfo> = {};
182189
try {
@@ -246,25 +253,36 @@ const getStackDetails = async (stackName: string, account: AWSAccountInfo, regio
246253

247254
const getStacks = async (account: AWSAccountInfo, region: string): Promise<StackInfo[]> => {
248255
const cfnClient = new aws.CloudFormation(getAWSConfig(account, region));
249-
const stacks = await cfnClient
250-
.listStacks({
251-
StackStatusFilter: [
252-
'CREATE_COMPLETE',
253-
'ROLLBACK_FAILED',
254-
'DELETE_FAILED',
255-
'UPDATE_COMPLETE',
256-
'UPDATE_ROLLBACK_FAILED',
257-
'UPDATE_ROLLBACK_COMPLETE',
258-
'IMPORT_COMPLETE',
259-
'IMPORT_ROLLBACK_FAILED',
260-
'IMPORT_ROLLBACK_COMPLETE',
261-
],
262-
})
263-
.promise();
256+
const results: StackInfo[] = [];
257+
let stacks;
258+
try {
259+
stacks = await cfnClient
260+
.listStacks({
261+
StackStatusFilter: [
262+
'CREATE_COMPLETE',
263+
'ROLLBACK_FAILED',
264+
'DELETE_FAILED',
265+
'UPDATE_COMPLETE',
266+
'UPDATE_ROLLBACK_FAILED',
267+
'UPDATE_ROLLBACK_COMPLETE',
268+
'IMPORT_COMPLETE',
269+
'IMPORT_ROLLBACK_FAILED',
270+
'IMPORT_ROLLBACK_COMPLETE',
271+
],
272+
})
273+
.promise();
274+
} catch (e) {
275+
if (e?.code === 'InvalidClientTokenId') {
276+
// Do not fail the cleanup and continue
277+
console.log(`Listing stacks for account ${account.accountId}-${region} failed with error with code ${e?.code}. Skipping.`);
278+
return results;
279+
} else {
280+
throw e;
281+
}
282+
}
264283

265284
// We are interested in only the root stacks that are deployed by amplify-cli
266285
const rootStacks = stacks.StackSummaries.filter(stack => !stack.RootId);
267-
const results: StackInfo[] = [];
268286
for (const stack of rootStacks) {
269287
try {
270288
const details = await getStackDetails(stack.StackName, account, region);

0 commit comments

Comments
 (0)