1111 * and limitations under the License.
1212 */
1313
14- import { Account , OrganizationsClient , paginateListAccounts } from '@aws-sdk/client-organizations' ;
14+ import { Account , AccountStatus , OrganizationsClient , paginateListAccounts } from '@aws-sdk/client-organizations' ;
1515import { AssumeRoleCommand , AssumeRoleCommandOutput , GetCallerIdentityCommand , STSClient } from '@aws-sdk/client-sts' ;
1616import { AwsCredentialIdentity } from '@aws-sdk/types' ;
1717
1818import { TableOperations } from './common/dynamodb' ;
1919import { snapshotAccountResources } from './snapshotAccountResources' ;
2020import { snapshotGlobalResources } from './snapshotGlobalResources' ;
21+ import { DynamoDB } from '../common/aws/dynamodb' ;
22+ import { loadAccounts } from '../common/utils/accounts' ;
2123import { snapshotRegionResources } from './snapshotRegionalResources' ;
2224import { AcceleratorConfig } from '../asea-config' ;
2325
@@ -31,6 +33,7 @@ export async function snapshotConfiguration(
3133 prefix : string ,
3234 preMigration : boolean ,
3335 aseaConfig : AcceleratorConfig ,
36+ aseaParametersTableName : string
3437) {
3538 stsClient = new STSClient ( { maxAttempts : 10 } ) ;
3639
@@ -44,7 +47,8 @@ export async function snapshotConfiguration(
4447 // process global services
4548 await snapshotGlobalResources ( tableName , homeRegion , currentAccountId ! , preMigration , undefined ) ;
4649
47- const accounts = await getAccountList ( ) ;
50+ const accounts = await getAccountList ( homeRegion , aseaParametersTableName ) ;
51+ console . log ( `Running snapshot for ${ accounts . length } accounts` )
4852 const regions = aseaConfig [ 'global-options' ] [ 'supported-regions' ] ;
4953 // process account services
5054 const accountPromises = [ ] ;
@@ -112,7 +116,40 @@ export async function getCredentials(accountId: string, roleName: string): Promi
112116 }
113117}
114118
115- export async function getAccountList ( ) : Promise < Account [ ] > {
119+ export async function getAccountList ( homeRegion : string , parametersTableName : string ) : Promise < Account [ ] > {
120+ // Get accounts from DynamoDB (ASEA managed accounts)
121+ const dynamodb = new DynamoDB ( undefined , homeRegion ) ;
122+ const aseaAccounts = await loadAccounts ( parametersTableName , dynamodb ) ;
123+
124+ if ( aseaAccounts . length === 0 ) {
125+ console . warn ( `No accounts found in DynamoDB table ${ parametersTableName } .` ) ;
126+ return [ ] ;
127+ }
128+
129+ console . log ( `Retrieved ${ aseaAccounts . length } accounts from DynamoDB table ${ parametersTableName } ` ) ;
130+
131+ // Get all accounts from Organizations to get their current status
132+ const orgAccounts = await getAccountListFromOrganizations ( ) ;
133+ console . log ( `Retrieved ${ orgAccounts . length } accounts from AWS Organizations` ) ;
134+
135+ // Create a map of account IDs to their Organization status
136+ const accountStatusMap = new Map < string , AccountStatus > ( ) ;
137+ for ( const orgAccount of orgAccounts ) {
138+ if ( orgAccount . Id ) {
139+ accountStatusMap . set ( orgAccount . Id , orgAccount . Status || AccountStatus . SUSPENDED ) ;
140+ }
141+ }
142+
143+ // Return only accounts from DynamoDB but with status from Organizations
144+ return aseaAccounts . map ( account => ( {
145+ Id : account . id ,
146+ Name : account . key ,
147+ Email : account . email || '' ,
148+ Status : accountStatusMap . get ( account . id ) || AccountStatus . SUSPENDED // Default to SUSPENDED if not found in Organizations
149+ } ) ) ;
150+ }
151+
152+ async function getAccountListFromOrganizations ( ) : Promise < Account [ ] > {
116153 const organizationsClient = new OrganizationsClient ( { region : 'us-east-1' , maxAttempts : 10 } ) ;
117154
118155 const accounts : Account [ ] = [ ] ;
0 commit comments