Skip to content

Commit 088d882

Browse files
run snapshot only in ASEA managed accounts (#1282)
1 parent 515f5ad commit 088d882

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

reference-artifacts/Custom-Scripts/lza-upgrade/src/snapshot.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class Snapshot {
2525
private readonly aseaConfigRepositoryName: string;
2626
private readonly localConfigFilePath: string | undefined;
2727
private readonly preMigrationSnapshot: boolean;
28+
private readonly parametersTableName: string;
2829

2930
constructor(config: Config) {
3031
this.aseaPrefix = config.aseaPrefix ?? 'ASEA-';
@@ -34,6 +35,7 @@ export class Snapshot {
3435
this.aseaConfigRepositoryName = config.repositoryName;
3536
this.localConfigFilePath = config.localConfigFilePath;
3637
this.preMigrationSnapshot = false;
38+
this.parametersTableName = config.parametersTableName;
3739
}
3840

3941
async pre() {
@@ -50,6 +52,7 @@ export class Snapshot {
5052
this.aseaPrefix,
5153
true,
5254
aseaConfig,
55+
this.parametersTableName
5356
);
5457
}
5558

@@ -67,6 +70,7 @@ export class Snapshot {
6770
this.aseaPrefix,
6871
this.preMigrationSnapshot,
6972
aseaConfig,
73+
this.parametersTableName
7074
);
7175
}
7276

reference-artifacts/Custom-Scripts/lza-upgrade/src/snapshot/snapshotConfiguration.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
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';
1515
import { AssumeRoleCommand, AssumeRoleCommandOutput, GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts';
1616
import { AwsCredentialIdentity } from '@aws-sdk/types';
1717

1818
import { TableOperations } from './common/dynamodb';
1919
import { snapshotAccountResources } from './snapshotAccountResources';
2020
import { snapshotGlobalResources } from './snapshotGlobalResources';
21+
import { DynamoDB } from '../common/aws/dynamodb';
22+
import { loadAccounts } from '../common/utils/accounts';
2123
import { snapshotRegionResources } from './snapshotRegionalResources';
2224
import { 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

Comments
 (0)