11
11
* and limitations under the License.
12
12
*/
13
13
14
- import { Account , OrganizationsClient , paginateListAccounts } from '@aws-sdk/client-organizations' ;
14
+ import { Account , AccountStatus , OrganizationsClient , paginateListAccounts } from '@aws-sdk/client-organizations' ;
15
15
import { AssumeRoleCommand , AssumeRoleCommandOutput , GetCallerIdentityCommand , STSClient } from '@aws-sdk/client-sts' ;
16
16
import { AwsCredentialIdentity } from '@aws-sdk/types' ;
17
17
18
18
import { TableOperations } from './common/dynamodb' ;
19
19
import { snapshotAccountResources } from './snapshotAccountResources' ;
20
20
import { snapshotGlobalResources } from './snapshotGlobalResources' ;
21
+ import { DynamoDB } from '../common/aws/dynamodb' ;
22
+ import { loadAccounts } from '../common/utils/accounts' ;
21
23
import { snapshotRegionResources } from './snapshotRegionalResources' ;
22
24
import { AcceleratorConfig } from '../asea-config' ;
23
25
@@ -31,6 +33,7 @@ export async function snapshotConfiguration(
31
33
prefix : string ,
32
34
preMigration : boolean ,
33
35
aseaConfig : AcceleratorConfig ,
36
+ aseaParametersTableName : string
34
37
) {
35
38
stsClient = new STSClient ( { maxAttempts : 10 } ) ;
36
39
@@ -44,7 +47,8 @@ export async function snapshotConfiguration(
44
47
// process global services
45
48
await snapshotGlobalResources ( tableName , homeRegion , currentAccountId ! , preMigration , undefined ) ;
46
49
47
- const accounts = await getAccountList ( ) ;
50
+ const accounts = await getAccountList ( homeRegion , aseaParametersTableName ) ;
51
+ console . log ( `Running snapshot for ${ accounts . length } accounts` )
48
52
const regions = aseaConfig [ 'global-options' ] [ 'supported-regions' ] ;
49
53
// process account services
50
54
const accountPromises = [ ] ;
@@ -112,7 +116,40 @@ export async function getCredentials(accountId: string, roleName: string): Promi
112
116
}
113
117
}
114
118
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 [ ] > {
116
153
const organizationsClient = new OrganizationsClient ( { region : 'us-east-1' , maxAttempts : 10 } ) ;
117
154
118
155
const accounts : Account [ ] = [ ] ;
0 commit comments