Skip to content

Commit 20d23e1

Browse files
committed
fix: cleanup detection of regional availability
Signed-off-by: Kevin Shan <[email protected]>
1 parent d791086 commit 20d23e1

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

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

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable spellcheck/spell-checker, camelcase, @typescript-eslint/no-explicit-any */
2-
import { CodeBuild } from 'aws-sdk';
2+
import { CodeBuild, Account } from 'aws-sdk';
33
import { config } from 'dotenv';
44
import yargs from 'yargs';
55
import * as aws from 'aws-sdk';
@@ -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

@@ -109,6 +104,19 @@ const handleExpiredTokenException = (): void => {
109104
process.exit();
110105
};
111106

107+
/**
108+
* Check if given region is enabled in the account info provided
109+
* @param accountInfo aws account to check region
110+
* @param region aws region to check
111+
* @returns true if region is enabled in that account, false otherwise
112+
*/
113+
const isRegionEnabled = async (accountInfo: AWSAccountInfo, region: string): Promise<boolean> => {
114+
const account = new Account(getAWSConfig(accountInfo, region));
115+
const optStatus = await account.getRegionOptStatus({ RegionName: region }).promise();
116+
117+
return optStatus.RegionOptStatus === 'ENABLED' || optStatus.RegionOptStatus === 'ENABLED_BY_DEFAULT';
118+
};
119+
112120
/**
113121
* We define a resource as viable for deletion if it matches TEST_REGEX in the name, and if it is > STALE_DURATION_MS old.
114122
*/
@@ -175,6 +183,13 @@ const getOrphanTestIamRoles = async (account: AWSAccountInfo): Promise<IamRoleIn
175183
*/
176184
const getAmplifyApps = async (account: AWSAccountInfo, region: string): Promise<AmplifyAppInfo[]> => {
177185
const amplifyClient = new aws.Amplify(getAWSConfig(account, region));
186+
187+
const optStatus = await isRegionEnabled(account, region);
188+
if (!optStatus) {
189+
console.error(`Listing apps for account ${account.accountId}-${region} failed since ${region} is not enabled. Skipping.`);
190+
return [];
191+
}
192+
178193
const amplifyApps = await amplifyClient.listApps({ maxResults: 50 }).promise(); // keeping it to 50 as max supported is 50
179194
const result: AmplifyAppInfo[] = [];
180195
for (const app of amplifyApps.apps) {
@@ -246,6 +261,13 @@ const getStackDetails = async (stackName: string, account: AWSAccountInfo, regio
246261

247262
const getStacks = async (account: AWSAccountInfo, region: string): Promise<StackInfo[]> => {
248263
const cfnClient = new aws.CloudFormation(getAWSConfig(account, region));
264+
265+
const optStatus = await isRegionEnabled(account, region);
266+
if (!optStatus) {
267+
console.error(`Listing stacks for account ${account.accountId}-${region} failed since ${region} is not enabled. Skipping.`);
268+
return [];
269+
}
270+
249271
const stacks = await cfnClient
250272
.listStacks({
251273
StackStatusFilter: [

0 commit comments

Comments
 (0)