Skip to content

Commit 1d6678a

Browse files
Refactor Account Collection for EUC (#321)
Co-authored-by: chodono-aws <chodono@amazon.com>
1 parent 5339003 commit 1d6678a

File tree

4 files changed

+102
-121
lines changed

4 files changed

+102
-121
lines changed

data-collection/deploy/account-collector.yaml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ Parameters:
2222
Type: String
2323
Description: "ARNs of KMS Keys for data buckets and/or Glue Catalog. Comma separated list, no spaces. Keep empty if data Buckets and Glue Catalog are not Encrypted with KMS. You can also set it to '*' to grant decrypt permission for all the keys."
2424
Default: ""
25+
EUCAccountIDs:
26+
Type: String
27+
Description: "Comma-separated list of account IDs that user has identified to have WorkSpaces running, used to collect WorkSpaces metrics from. If left blank, metrics will be checked from all linked accounts in Organization."
28+
Default: ""
29+
2530
Outputs:
2631
LambdaFunctionName:
2732
Value: !Ref LambdaFunction
@@ -133,9 +138,8 @@ Resources:
133138
MANAGEMENT_ACCOUNT_IDS = os.environ.get('MANAGEMENT_ACCOUNT_IDS')
134139
BUCKET = os.environ.get('BUCKET_NAME')
135140
PREDEF_ACCOUNT_LIST_KEY = os.environ.get('PREDEF_ACCOUNT_LIST_KEY')
136-
LINKED_ACCOUNT_LIST_KEY = os.environ.get('LINKED_ACCOUNT_LIST_KEY')
137-
PAYER_ACCOUNT_LIST_KEY = os.environ.get('PAYER_ACCOUNT_LIST_KEY')
138141
EXCLUDED_ACCOUNT_LIST_KEY = os.environ.get('EXCLUDED_ACCOUNT_LIST_KEY')
142+
EUC_ACCOUNTS = os.environ.get('EUC_ACCOUNT_IDS', '').strip()
139143
TMP_FILE = "/tmp/data.json"
140144
141145
logger = logging.getLogger(__name__)
@@ -154,6 +158,7 @@ Resources:
154158
155159
functions = { # keep keys same as boto3 services
156160
'linked': iterate_linked_accounts,
161+
'euc': partial(iterate_accounts_with_filter, EUC_ACCOUNTS),
157162
'payers': partial(iterate_admins_accounts, None),
158163
'organizations': partial(iterate_admins_accounts, 'organizations'),
159164
'compute-optimizer': partial(iterate_admins_accounts, 'compute-optimizer'),
@@ -177,7 +182,7 @@ Resources:
177182
if count == 0:
178183
raise Exception('No accounts found. Check the log.') #pylint: disable=broad-exception-raised
179184
180-
key = LINKED_ACCOUNT_LIST_KEY if account_type == 'linked' else PAYER_ACCOUNT_LIST_KEY
185+
key = f"account-list/{account_type}-account-list.json"
181186
s3 = boto3.client('s3')
182187
s3.upload_file(TMP_FILE, Bucket=BUCKET, Key=key)
183188
@@ -196,7 +201,7 @@ Resources:
196201
try:
197202
account_id = ssm.get_parameter(Name=ssm_key)['Parameter']['Value']
198203
except ssm.exceptions.ParameterNotFound:
199-
logger.warning(f'Not found ssm parameter {ssm_key}. Will use Management Account Id {payer_id}')
204+
logger.info(f'Not found ssm parameter {ssm_key}. Will use Management Account Id {payer_id}')
200205
yield {"account": json.dumps({'account_id': account_id, 'account_name': '', 'payer_id': payer_id})}
201206
202207
def iterate_linked_accounts():
@@ -230,6 +235,15 @@ Resources:
230235
except Exception as exc: #pylint: disable=broad-exception-caught
231236
logger.error( f'{type(exc).__name__}: When trying to build linked account list. {exc} ')
232237
238+
def iterate_accounts_with_filter(filter_accounts):
239+
''' same as iterate_linked_accounts but with additional filtering
240+
filter_accounts: a comma separated list of accounts OR '*'
241+
'''
242+
for acc in iterate_linked_accounts():
243+
account_id = json.loads(acc['account'])['account_id']
244+
if account_id in filter_accounts.split(',') or filter_accounts.strip() == '*':
245+
yield acc
246+
233247
def get_defined_list(bucket, key):
234248
s3 = boto3.client("s3")
235249
exts = [".json", ".csv"]
@@ -282,9 +296,8 @@ Resources:
282296
RESOURCE_PREFIX: !Ref ResourcePrefix
283297
BUCKET_NAME: !Ref DestinationBucket
284298
PREDEF_ACCOUNT_LIST_KEY: "account-list/account-list"
285-
LINKED_ACCOUNT_LIST_KEY: "account-list/linked-account-list.json"
286-
PAYER_ACCOUNT_LIST_KEY: "account-list/payer-account-list.json"
287299
EXCLUDED_ACCOUNT_LIST_KEY: "account-list/excluded-linked-account-list.csv"
300+
EUC_ACCOUNT_IDS: !Ref EUCAccountIDs
288301
Metadata:
289302
cfn_nag:
290303
rules_to_suppress:

data-collection/deploy/deploy-data-collection.yaml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,18 @@ Metadata:
3030
- IncludeInventoryCollectorModule
3131
- IncludeOrgDataModule
3232
- IncludeRDSUtilizationModule
33-
- IncludeEUCUtilizationModule
3433
- IncludeRightsizingModule
3534
- IncludeTAModule
3635
- IncludeTransitGatewayModule
3736
- IncludeAWSFeedsModule
3837
- IncludeLicenseManagerModule
3938
- IncludeQuickSightModule
4039
- IncludeServiceQuotasModule
41-
- Label:
42-
default: 'EUC Module Configuration'
43-
Parameters:
4440
- IncludeEUCUtilizationModule
4541
- Label:
46-
default: 'EUC Module Settings'
42+
default: 'EUC (End User Compute) Module Configuration'
4743
Parameters:
48-
- EUCAccountIDs
44+
- EUCAccountIDs
4945
ParameterLabels:
5046
DestinationBucket:
5147
default: 'Destination S3 bucket prefix'
@@ -224,11 +220,11 @@ Parameters:
224220
Type: String
225221
Description: Collects WorkSpaces CloudWatch metrics from your accounts
226222
AllowedValues: ['yes', 'no']
227-
Default: 'no'
223+
Default: 'no'
228224
EUCAccountIDs:
229225
Type: String
230-
Description: "Optional, If you enable EUC Utilization or Inventory module and you use Amazon WorkSpaces, please provide a comma-separated list of account IDs where WorkSpaces are deployed. If left blank, metrics will be collected from all linked accounts in the Organization."
231-
Default: ""
226+
Description: "Optional, If you enable EUC Utilization or Inventory module and you use Amazon WorkSpaces, please provide a comma-separated list of account IDs where WorkSpaces are deployed. Or you can set * to collect from all linked accounts in the Organization."
227+
Default: "*"
232228
IncludeOrgDataModule:
233229
Type: String
234230
Description: Collects AWS Organizations data such as account Id, account name, organization parent and specified tags
@@ -1203,7 +1199,6 @@ Resources:
12031199
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-v3, TemplatePath]
12041200
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
12051201
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1206-
EUCAccountIDs: !Ref EUCAccountIDs
12071202
RegionsInScope:
12081203
Fn::If:
12091204
- RegionsInScopeIsEmpty
@@ -1396,6 +1391,7 @@ Resources:
13961391
DestinationBucket: !Ref S3Bucket
13971392
DestinationBucketARN: !GetAtt S3Bucket.Arn
13981393
DataBucketsKmsKeysArns: !Ref DataBucketsKmsKeysArns
1394+
EUCAccountIDs: !Ref EUCAccountIDs
13991395

14001396
DataCollectionReadAccess:
14011397
Type: AWS::IAM::ManagedPolicy

0 commit comments

Comments
 (0)