Skip to content

Commit 914a65b

Browse files
committed
chore: try use sdk to get opt status
Signed-off-by: Kevin Shan <[email protected]>
1 parent f13cad8 commit 914a65b

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

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

Lines changed: 34 additions & 27 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';
@@ -251,38 +251,45 @@ const getStackDetails = async (stackName: string, account: AWSAccountInfo, regio
251251
};
252252
};
253253

254-
const getStacks = async (account: AWSAccountInfo, region: string): Promise<StackInfo[]> => {
255-
const cfnClient = new aws.CloudFormation(getAWSConfig(account, region));
256-
const results: StackInfo[] = [];
257-
let stacks;
254+
const isRegionEnabled = async (config: unknown, region: string): Promise<boolean> => {
258255
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();
256+
const account = new Account(config);
257+
const response = await account.getRegionOptStatus({ RegionName: region }).promise();
258+
259+
return response.RegionOptStatus === 'ENABLED' || response.RegionOptStatus === 'ENABLED_BY_DEFAULT';
274260
} 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-
}
261+
console.error(`Error checking region status: ${e}`);
262+
throw e;
263+
}
264+
};
265+
266+
const getStacks = async (account: AWSAccountInfo, region: string): Promise<StackInfo[]> => {
267+
const cfnClient = new aws.CloudFormation(getAWSConfig(account, region));
268+
269+
const regionEnabled = await isRegionEnabled(getAWSConfig(account), region);
270+
if (!regionEnabled) {
271+
return [];
282272
}
283273

274+
const stacks = await cfnClient
275+
.listStacks({
276+
StackStatusFilter: [
277+
'CREATE_COMPLETE',
278+
'ROLLBACK_FAILED',
279+
'DELETE_FAILED',
280+
'UPDATE_COMPLETE',
281+
'UPDATE_ROLLBACK_FAILED',
282+
'UPDATE_ROLLBACK_COMPLETE',
283+
'IMPORT_COMPLETE',
284+
'IMPORT_ROLLBACK_FAILED',
285+
'IMPORT_ROLLBACK_COMPLETE',
286+
],
287+
})
288+
.promise();
289+
284290
// We are interested in only the root stacks that are deployed by amplify-cli
285291
const rootStacks = stacks.StackSummaries.filter(stack => !stack.RootId);
292+
const results: StackInfo[] = [];
286293
for (const stack of rootStacks) {
287294
try {
288295
const details = await getStackDetails(stack.StackName, account, region);

0 commit comments

Comments
 (0)