Skip to content

Commit 6c1669c

Browse files
committed
Pre-main merge instrumentation
1 parent f857421 commit 6c1669c

24 files changed

+277
-111
lines changed

data-collection/deploy/account-collector.yaml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ Resources:
159159
global MODULE
160160
logger.info(f"Incoming event: {event}")
161161
sub_uuid = {"lambda-request-id": context.aws_request_id, "lambda-log-group": context.log_group_name, "lambda-log-stream": context.log_stream_name}
162-
account_type = event.get("Type", '').lower()
163-
MODULE = event.get("Module", '').lower()
164-
main_exe_uuid = event.get("MainExeUuid", str(uuid.uuid4()))
165-
params = event.get("Params", "")
162+
account_type = event.get("type", '').lower()
163+
MODULE = event.get("module", '').lower()
164+
main_exe_uuid = event.get("main_exe_uuid", str(uuid.uuid4()))
165+
params = event.get("params", "")
166+
stack_version = event.get("stack_version", "")
166167
try:
167168
# need to confirm that the Lambda concurrency limit is sufficient to avoid throttling
168169
lambda_limit = boto3.client('lambda').get_account_settings()['AccountLimit']['ConcurrentExecutions']
@@ -182,6 +183,8 @@ Resources:
182183
account_type = None
183184
raise Exception(STATUS_NOT_ACCEPTABLE) #pylint: disable=broad-exception-raised
184185
186+
context = "linked" if account_type == "linked" or account_type == "euc" else "payer"
187+
185188
account_iterator = functions[account_type]
186189
logger.info(f"Looking for accounts")
187190
with open(TMP_FILE, "w", encoding='utf-8') as f:
@@ -195,6 +198,7 @@ Resources:
195198
account['dc_region'] = DC_REGION
196199
account['params'] = params
197200
account['prefix'] = PREFIX
201+
account['stack_version'] = stack_version
198202
if count > 0:
199203
f.write(",\n")
200204
f.write(json.dumps(account))
@@ -209,7 +213,7 @@ Resources:
209213
s3.upload_file(TMP_FILE, Bucket=BUCKET, Key=key)
210214
location = f"s3://{BUCKET}/{key}"
211215
log_entry = create_log_entry(module_function='account-collector-lambda', params=params, region="us-east-1", record_count=count,
212-
location=location, main_exe_uuid=main_exe_uuid, record_context="account", sub_uuid=sub_uuid)
216+
location=location, main_exe_uuid=main_exe_uuid, record_context=f"{context} account(s)", sub_uuid=sub_uuid, stack_version=stack_version)
213217
return {'statusCode': 200, 'accountList': key, 'bucket': BUCKET, 'logEntry': log_entry['logEntry']}
214218
except Exception as exc: #pylint: disable=broad-exception-caught
215219
exc_msg = str(exc)
@@ -227,7 +231,8 @@ Resources:
227231
else:
228232
status_code = None
229233
description = None
230-
create_log_entry(module_function='account-collector-lambda', params=params, region="us-east-1", status_code=status_code, description=description, error=exc, main_exe_uuid=main_exe_uuid, sub_uuid=sub_uuid)
234+
create_log_entry(module_function='account-collector-lambda', params=params, region="us-east-1", status_code=status_code,
235+
description=description, error=exc, main_exe_uuid=main_exe_uuid, sub_uuid=sub_uuid, stack_version=stack_version)
231236
raise exc
232237
233238
def get_all_payers():
@@ -244,8 +249,6 @@ Resources:
244249
account_id = ssm.get_parameter(Name=ssm_key)['Parameter']['Value']
245250
except ssm.exceptions.ParameterNotFound:
246251
logger.info(f'Not found ssm parameter {ssm_key}. Will use Management Account Id {payer_id}')
247-
yield {"account": json.dumps({'account_id': account_id, 'account_name': '', 'payer_id': payer_id})}
248-
logger.warning(f'Not found ssm parameter {ssm_key}. Will use Management Account Id {payer_id}')
249252
yield {"account": json.dumps({'account_id': account_id, 'account_name': '', 'payer_id': payer_id}), "main_exe_uuid": ""}
250253
251254
def iterate_linked_accounts():
@@ -334,7 +337,7 @@ Resources:
334337
)
335338
336339
def create_log_entry(payer_id="", account_id=None, status_code=None, region="", module_function="data-collection-lambda", sub_code="",
337-
params="", record_count=0, record_context="", description=None, location="", error=None, main_exe_uuid="", sub_uuid={}, is_summary=False, store_it=True): # pylint: disable=too-many-locals
340+
params="", record_count=0, record_context="", description=None, location="", error=None, main_exe_uuid="", sub_uuid={}, is_summary=False, store_it=True, stack_version="", subversion=""): # pylint: disable=too-many-locals
338341
"""Format log entry for logging."""
339342
status_code, description = status_handler(error, record_count, is_summary, status_code, description, record_context)
340343
log_entry = {
@@ -354,7 +357,9 @@ Resources:
354357
"DataLocation": location if record_count > 0 else "",
355358
"MainExeUuid": main_exe_uuid,
356359
"SubUuid": sub_uuid,
357-
"Service": "Lambda"
360+
"Service": "Lambda",
361+
"StackVersion": stack_version,
362+
"SubVersion": subversion
358363
}
359364
if status_code >= 400:
360365
logger.error(description)

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ Mappings:
120120
eu-west-3: {CodeBucket: aws-managed-cost-intelligence-dashboards-eu-west-3 }
121121
sa-east-1: {CodeBucket: aws-managed-cost-intelligence-dashboards-sa-east-1 }
122122
us-east-1: {CodeBucket: aws-managed-cost-intelligence-dashboards-us-east-1 }
123-
us-east-2: {CodeBucket: aws-managed-cost-intelligence-dashboards-us-east-2 }
123+
us-east-2: {CodeBucket: aws-managed-cost-intel ligence-dashboards-us-east-2 }
124124
us-west-1: {CodeBucket: aws-managed-cost-intelligence-dashboards-us-west-1 }
125125
us-west-2: {CodeBucket: aws-managed-cost-intelligence-dashboards-us-west-2 }
126126
StepFunctionCode:
127127
main-state-machine-v4: {TemplatePath: cfn/data-collection/source/step-functions/main-state-machine-v4.json}
128128
crawler-v2: {TemplatePath: cfn/data-collection/source/step-functions/crawler-state-machine-v2.json}
129129
standalone-v2: {TemplatePath: cfn/data-collection/source/step-functions/standalone-state-machine-v2.json}
130-
health-detail-state-machine-v2-1: {TemplatePath: cfn/data-collection/source/step-functions/health-detail-state-machine-v2-1.json}
130+
health-detail-state-machine-v2: {TemplatePath: cfn/data-collection/source/step-functions/health-detail-state-machine-v2.json}
131131

132132
Parameters:
133133
DestinationBucket:
@@ -897,14 +897,26 @@ Resources:
897897
- s3:GetObject
898898
- s3:PutObject
899899
Resource: !Sub '${S3Bucket.Arn}/*'
900-
- PolicyName: "CloudWatch-ErrorLogging" #Used for logging processing errors to CloudWatch
900+
- PolicyName: "CloudWatchErrorLogging" #Used for logging processing errors to CloudWatch
901901
PolicyDocument:
902902
Version: "2012-10-17"
903903
Statement:
904904
- Effect: "Allow"
905905
Action:
906906
- cloudwatch:PutMetricData
907907
Resource: '*'
908+
- PolicyName: "CloudFormationStackVersionRead" #Used for reading the deployed version number of the stack
909+
PolicyDocument:
910+
Version: "2012-10-17"
911+
Statement:
912+
- Effect: "Allow"
913+
Action:
914+
- cloudformation:ListStacks
915+
Resource: '*'
916+
- Effect: "Allow"
917+
Action:
918+
- cloudformation:DescribeStacks
919+
Resource: !Ref "AWS::StackId"
908920

909921
StepFunctionExecutionRoleInvokeAccountCollectorPolicy:
910922
Type: 'AWS::IAM::Policy'
@@ -984,6 +996,8 @@ Resources:
984996
- { Name: mainexeuuid, Type: string }
985997
- { Name: subuuid, Type: string }
986998
- { Name: service, Type: string }
999+
- { Name: stackversion, Type: string }
1000+
- { Name: subversion, Type: string }
9871001
InputFormat: org.apache.hadoop.mapred.TextInputFormat
9881002
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
9891003
Location: !Sub "s3://${DestinationBucket}${AWS::AccountId}/logs/modules/"
@@ -1022,6 +1036,7 @@ Resources:
10221036
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
10231037
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
10241038
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1039+
StackID: !Ref "AWS::StackId"
10251040

10261041
RightsizeModule:
10271042
Type: AWS::CloudFormation::Stack
@@ -1043,6 +1058,7 @@ Resources:
10431058
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
10441059
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
10451060
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1061+
StackID: !Ref "AWS::StackId"
10461062

10471063
CostAnomalyModule:
10481064
Type: AWS::CloudFormation::Stack
@@ -1065,6 +1081,7 @@ Resources:
10651081
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
10661082
LambdaManageGlueTableARN: !GetAtt LambdaManageGlueTable.Arn
10671083
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1084+
StackID: !Ref "AWS::StackId"
10681085

10691086
SupportCasesModule:
10701087
Type: AWS::CloudFormation::Stack
@@ -1086,6 +1103,7 @@ Resources:
10861103
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
10871104
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
10881105
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1106+
StackID: !Ref "AWS::StackId"
10891107

10901108
BackupModule:
10911109
Type: AWS::CloudFormation::Stack
@@ -1107,6 +1125,7 @@ Resources:
11071125
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
11081126
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
11091127
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1128+
StackID: !Ref "AWS::StackId"
11101129

11111130
InventoryCollectorModule:
11121131
Type: AWS::CloudFormation::Stack
@@ -1134,6 +1153,7 @@ Resources:
11341153
- RegionsInScopeIsEmpty
11351154
- !Sub "${AWS::Region}"
11361155
- !Join [ '', !Split [ ' ', !Ref RegionsInScope ] ] # remove spaces
1156+
StackID: !Ref "AWS::StackId"
11371157

11381158
PricingModule:
11391159
Type: AWS::CloudFormation::Stack
@@ -1157,6 +1177,7 @@ Resources:
11571177
- RegionsInScopeIsEmpty
11581178
- !Sub "${AWS::Region}"
11591179
- !Join [ '', !Split [ ' ', !Ref RegionsInScope ] ] # remove spaces
1180+
StackID: !Ref "AWS::StackId"
11601181

11611182
ComputeOptimizerModule:
11621183
Type: AWS::CloudFormation::Stack
@@ -1182,6 +1203,7 @@ Resources:
11821203
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
11831204
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
11841205
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1206+
StackID: !Ref "AWS::StackId"
11851207

11861208
EcsChargebackModule:
11871209
Type: AWS::CloudFormation::Stack
@@ -1208,6 +1230,7 @@ Resources:
12081230
- RegionsInScopeIsEmpty
12091231
- !Sub "${AWS::Region}"
12101232
- !Join [ '', !Split [ ' ', !Ref RegionsInScope ] ] # remove spaces
1233+
StackID: !Ref "AWS::StackId"
12111234

12121235
RDSUsageModule:
12131236
Type: AWS::CloudFormation::Stack
@@ -1234,6 +1257,7 @@ Resources:
12341257
- RegionsInScopeIsEmpty
12351258
- !Sub "${AWS::Region}"
12361259
- !Join [ '', !Split [ ' ', !Ref RegionsInScope ] ] # remove spaces
1260+
StackID: !Ref "AWS::StackId"
12371261

12381262
EUCUsageModule:
12391263
Type: AWS::CloudFormation::Stack
@@ -1260,6 +1284,7 @@ Resources:
12601284
- RegionsInScopeIsEmpty
12611285
- !Sub "${AWS::Region}"
12621286
- !Join [ '', !Split [ ' ', !Ref RegionsInScope ] ] # remove spaces
1287+
StackID: !Ref "AWS::StackId"
12631288

12641289
OrgDataModule:
12651290
Type: AWS::CloudFormation::Stack
@@ -1281,6 +1306,7 @@ Resources:
12811306
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
12821307
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
12831308
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1309+
StackID: !Ref "AWS::StackId"
12841310

12851311
BudgetsModule:
12861312
Type: AWS::CloudFormation::Stack
@@ -1302,6 +1328,7 @@ Resources:
13021328
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
13031329
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
13041330
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1331+
StackID: !Ref "AWS::StackId"
13051332

13061333
TransitGatewayModule:
13071334
Type: AWS::CloudFormation::Stack
@@ -1328,6 +1355,7 @@ Resources:
13281355
- RegionsInScopeIsEmpty
13291356
- !Sub "${AWS::Region}"
13301357
- !Join [ '', !Split [ ' ', !Ref RegionsInScope ] ] # remove spaces
1358+
StackID: !Ref "AWS::StackId"
13311359

13321360
AWSFeedsModule:
13331361
Type: AWS::CloudFormation::Stack
@@ -1347,6 +1375,7 @@ Resources:
13471375
StepFunctionTemplate: !FindInMap [StepFunctionCode, standalone-v2, TemplatePath]
13481376
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
13491377
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1378+
StackID: !Ref "AWS::StackId"
13501379

13511380
HealthEventsModule:
13521381
Type: AWS::CloudFormation::Stack
@@ -1368,7 +1397,8 @@ Resources:
13681397
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
13691398
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
13701399
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1371-
DetailStepFunctionTemplate: !FindInMap [StepFunctionCode, health-detail-state-machine-v2-1, TemplatePath]
1400+
DetailStepFunctionTemplate: !FindInMap [StepFunctionCode, health-detail-state-machine-v2, TemplatePath]
1401+
StackID: !Ref "AWS::StackId"
13721402

13731403
LicenseManagerModule:
13741404
Type: AWS::CloudFormation::Stack
@@ -1390,6 +1420,7 @@ Resources:
13901420
StepFunctionTemplate: !FindInMap [StepFunctionCode, main-state-machine-v4, TemplatePath]
13911421
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
13921422
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1423+
StackID: !Ref "AWS::StackId"
13931424

13941425
ServiceQuotasModule:
13951426
Type: AWS::CloudFormation::Stack
@@ -1416,6 +1447,7 @@ Resources:
14161447
- RegionsInScopeIsEmpty
14171448
- !Sub "${AWS::Region}"
14181449
- !Join [ '', !Split [ ' ', !Ref RegionsInScope ] ] # remove spaces
1450+
StackID: !Ref "AWS::StackId"
14191451

14201452
QuickSightModule:
14211453
Type: AWS::CloudFormation::Stack
@@ -1435,6 +1467,7 @@ Resources:
14351467
StepFunctionTemplate: !FindInMap [StepFunctionCode, standalone-v2, TemplatePath]
14361468
StepFunctionExecutionRoleARN: !GetAtt StepFunctionExecutionRole.Arn
14371469
SchedulerExecutionRoleARN: !GetAtt SchedulerExecutionRole.Arn
1470+
StackID: !Ref "AWS::StackId"
14381471

14391472
AccountCollector:
14401473
Type: AWS::CloudFormation::Stack

data-collection/deploy/module-aws-feeds.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Description: Restrieves AWS Feeds like what's new, blog posts, youtube videos and security bulletin
33
Parameters:
4+
StackID:
5+
Type: String
6+
Description: Arn of the main stack
47
DatabaseName:
58
Type: String
69
Description: Name of the Athena database to be created

data-collection/deploy/module-backup.yaml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ AWSTemplateFormatVersion: "2010-09-09"
22
Description: Retrieves AWS Backup details across AWS organization
33
Transform: 'AWS::LanguageExtensions'
44
Parameters:
5+
StackID:
6+
Type: String
7+
Description: Arn of the main stack
58
DatabaseName:
69
Type: String
710
Description: Name of the Athena database to be created to hold lambda information
@@ -215,6 +218,7 @@ Resources:
215218
count = 0
216219
region = "us-east-1" #FIXME: what about other regions?
217220
main_exe_uuid = event.get("main_exe_uuid", str(uuid.uuid4()))
221+
stack_version = event.get("stack_version", "")
218222
219223
try:
220224
account = json.loads(event.get("account","{}"))
@@ -254,19 +258,19 @@ Resources:
254258
count, location = store_to_s3(flatten_data_iterator, s3_prefix)
255259
description = f"Searched range from {start_date} to {end_date}"
256260
return create_log_entry(payer_id=payer_id, account_id=account_id, region=region, params=name, record_count=count, location=location,
257-
main_exe_uuid=main_exe_uuid, sub_uuid=sub_uuid, description=description)
261+
main_exe_uuid=main_exe_uuid, sub_uuid=sub_uuid, description=description, stack_version=stack_version)
258262
259263
except Exception as exc: #pylint: disable=broad-exception-caught
260264
is_not_activated = 'Insufficient privileges to perform this action.' in str(exc)
261265
status_code = STATUS_FORBIDDEN if is_not_activated else None
262266
description = None if not is_not_activated else ('You need to activate cross account jobs monitoring. '
263267
'See https://docs.aws.amazon.com/aws-backup/latest/devguide/manage-cross-account.html#enable-cross-account')
264268
create_log_entry(payer_id=payer_id, account_id=account_id, region=region, params=name, record_count=count,
265-
main_exe_uuid=main_exe_uuid, sub_uuid=sub_uuid, description=description, status_code=status_code, error=exc)
269+
main_exe_uuid=main_exe_uuid, sub_uuid=sub_uuid, description=description, status_code=status_code, error=exc, stack_version=stack_version)
266270
raise exc
267271
268272
def create_log_entry(payer_id="", account_id=None, status_code=None, region="", module_function="data-collection-lambda", sub_code="",
269-
params="", record_count=0, description=None, location="", error=None, main_exe_uuid="", sub_uuid={}, is_summary=False, store_it=True): # pylint: disable=too-many-locals
273+
params="", record_count=0, record_context="", description=None, location="", error=None, main_exe_uuid="", sub_uuid={}, is_summary=False, store_it=True, stack_version="", subversion=""): # pylint: disable=too-many-locals
270274
"""Format log entry for logging."""
271275
status_code, description = status_handler(error, record_count, is_summary, status_code, description)
272276
log_entry = {
@@ -286,7 +290,9 @@ Resources:
286290
"DataLocation": location if record_count > 0 else "",
287291
"MainExeUuid": main_exe_uuid,
288292
"SubUuid": sub_uuid,
289-
"Service": "Lambda"
293+
"Service": "Lambda",
294+
"StackVersion": stack_version,
295+
"SubVersion": subversion
290296
}
291297
if status_code >= 400:
292298
logger.error(description)
@@ -403,6 +409,7 @@ Resources:
403409
Account: !Ref AWS::AccountId
404410
Prefix: !Ref ResourcePrefix
405411
Bucket: !Ref DestinationBucket
412+
DataCollectionStackID: !Ref StackID
406413
'RefreshSchedule${AwsObject}':
407414
Type: AWS::Scheduler::Schedule
408415
Properties:

0 commit comments

Comments
 (0)