|
1 | 1 | /* eslint-disable spellcheck/spell-checker, camelcase, @typescript-eslint/no-explicit-any */ |
2 | | -import { CodeBuild, Account } from 'aws-sdk'; |
| 2 | +import { CodeBuild } from 'aws-sdk'; |
3 | 3 | import { config } from 'dotenv'; |
4 | 4 | import yargs from 'yargs'; |
5 | 5 | import * as aws from 'aws-sdk'; |
@@ -251,45 +251,38 @@ const getStackDetails = async (stackName: string, account: AWSAccountInfo, regio |
251 | 251 | }; |
252 | 252 | }; |
253 | 253 |
|
254 | | -const isRegionEnabled = async (config: unknown, region: string): Promise<boolean> => { |
255 | | - try { |
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'; |
260 | | - } catch (e) { |
261 | | - console.error(`Error checking region status: ${e}`); |
262 | | - throw e; |
263 | | - } |
264 | | -}; |
265 | | - |
266 | 254 | const getStacks = async (account: AWSAccountInfo, region: string): Promise<StackInfo[]> => { |
267 | 255 | const cfnClient = new aws.CloudFormation(getAWSConfig(account, region)); |
268 | | - |
269 | | - const regionEnabled = await isRegionEnabled(getAWSConfig(account), region); |
270 | | - if (!regionEnabled) { |
271 | | - return []; |
| 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 | + } |
272 | 282 | } |
273 | 283 |
|
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 | | - |
290 | 284 | // We are interested in only the root stacks that are deployed by amplify-cli |
291 | 285 | const rootStacks = stacks.StackSummaries.filter(stack => !stack.RootId); |
292 | | - const results: StackInfo[] = []; |
293 | 286 | for (const stack of rootStacks) { |
294 | 287 | try { |
295 | 288 | const details = await getStackDetails(stack.StackName, account, region); |
|
0 commit comments