@@ -110,7 +110,6 @@ Resources:
110110 import logging
111111 import datetime
112112 from json import JSONEncoder
113- import sys
114113
115114 import boto3
116115
@@ -157,31 +156,36 @@ Resources:
157156 payer_id = account["payer_id"]
158157
159158 logger.info(f"Collecting data for account: {account_id}")
160- budgets_client = assume_role(account_id, "budgets", "us-east-1") # must be us-east-1
161- count = 0
162- with open(TMP_FILE, "w", encoding='utf-8') as f:
163- for budget in budgets_client.get_paginator("describe_budgets").paginate(AccountId=account_id).search('Budgets'):
164- if not budget: continue
165- budget['collection_time'] = collection_time
166-
167- # Fetch tags for the budget using List tag for resource API
168- budget_name = budget['BudgetName']
169- budget_tags = budgets_client.list_tags_for_resource(ResourceARN=f"arn:{aws_partition}:budgets::{account_id}:budget/{budget_name}")
170- budget.update({
171- 'Account_ID': account_id,
172- 'Account_Name': account_name,
173- 'Tags': budget_tags.get('ResourceTags') or []
174- })
175-
176- # Fetch CostFilters if available
177- if not budget.get('CostFilters'):
178- budget.update({'CostFilters': {'Filter': ['None']}})
179-
180- f.write(json.dumps(budget, cls=DateTimeEncoder) + "\n")
181- count += 1
182- logger.info(f"Budgets collected: {count}")
183- s3_upload(account_id, payer_id)
159+ try:
160+ budgets_client = assume_role(account_id, "budgets", "us-east-1") # must be us-east-1
161+ count = 0
162+ with open(TMP_FILE, "w", encoding='utf-8') as f:
163+ for budget in budgets_client.get_paginator("describe_budgets").paginate(AccountId=account_id).search('Budgets'):
164+ if not budget:
165+ continue
166+ budget['collection_time'] = collection_time
167+ # Fetch tags for the budget using List tag for resource API
168+ budget_name = budget['BudgetName']
169+ budget_tags = budgets_client.list_tags_for_resource(ResourceARN=f"arn:{aws_partition}:budgets::{account_id}:budget/{budget_name}")
170+ budget.update({
171+ 'Account_ID': account_id,
172+ 'Account_Name': account_name,
173+ 'Tags': budget_tags.get('ResourceTags') or []
174+ })
175+ # Fetch CostFilters if available
176+ if not budget.get('CostFilters'):
177+ budget.update({'CostFilters': {'Filter': ['None']}})
184178
179+ f.write(json.dumps(budget, cls=DateTimeEncoder) + "\n")
180+ count += 1
181+ logger.info(f"Budgets collected: {count}")
182+ s3_upload(account_id, payer_id)
183+ except Exception as exc: #pylint: disable=broad-exception-caught
184+ if "AccessDenied" in str(exc):
185+ print(f'Failed to assume role {ROLE_NAME} in account {account_id}. Please make sure the role exists. {exc}')
186+ else:
187+ print(f'{exc}. Gracefully exiting from Lambda so we do not break all StepFunction Execution')
188+ return
185189
186190 def s3_upload(account_id, payer_id):
187191 if os.path.getsize(TMP_FILE) == 0:
0 commit comments