def lambda_handler()

in rebase-budgets/app.py [0:0]


def lambda_handler(event,context):
    logger.info(json.dumps(event))
    account_id = os.environ['AccountId']
    try:
        # Fetch available business units from database
        business_entities = get_business_entities()
        # Check for request from S3
        if 'Records' in event : 
            for record in event['Records']:
                key = record['s3']['object']['key']
                # Look for manifest file only, it may be the case that there are multiple files uploaded by CUR
                # we do not want to rebase multiple times
                if key.split(".")[-1] == "json" :
                    # fetch pricing and save the data to ddb
                    logger.info("Pricing Manifest file found at {}".format(key))
                    for entity in business_entities:
                        logger.info("Processing Budget for Entity {}".format(entity))
                        budget_name = entity['budgetName']
                        range_key = entity['rangeKey']
                        budget_info = get_budget_details(account_id, budget_name)
                        budget_amt = Decimal(budget_info['Budget']['BudgetLimit']['Amount'])
                        actual_spend = Decimal(budget_info['Budget']['CalculatedSpend']['ActualSpend']['Amount'])
                        forecast_spend = Decimal(budget_info['Budget']['CalculatedSpend']['ForecastedSpend']['Amount'])
                        # Reset accruedForcastedSpend whenever there is a budget update from AWS
                        update_pricing_info(range_key, budget_name, budget_amt, actual_spend, forecast_spend)
            return {'statusCode':'200','body':'Successfully rebased accruedForecastSpend'}
        # Monthly rebase of accruedApprovalSpend
        elif 'source' in event and event['source'] == 'aws.events' : 
            logger.info("Event received from CloudWatchRule")
            for entity in business_entities:
                logger.info("Reset accruedApprovedSpend for business entity {}".format(entity))
                budget_name = entity['budgetName']
                range_key = entity['rangeKey']
                reset_accrued_approved_amt(range_key,budget_name)
            return {'statusCode':'200','body':'Successfully rebased AccruedApproval Amount'}
    except Exception as e:
        logger.error(e)
        return {'statusCode': '500', 'body':e}