def lambda_handler()

in get-ec2-pricing/app.py [0:0]


def lambda_handler(event,context):
    # Do not do anything for CFN Update and Delete
    if 'RequestType' in event and event['RequestType'] != 'Create':
        sendResponse(event, context,'SUCCESS', {})
        return
    
    logger.info(json.dumps(event))
    event_payload = event["ResourceProperties"]
    instance_type = event_payload['InstanceType']
    term_type = event_payload['TermType']
    operating_system = event_payload['OperatingSystem']
    hours_left = hours_left_for_current_month()
    next_month_hrs = hours_for_next_month()
    logger.info("# of Hrs left for this month {}".format(hours_left))
    unit_price = getPrice_from_API(operating_system, instance_type, region, term_type)
    logger.info("Unit Price {}".format(unit_price))
    monthly_price = hours_left * unit_price
    monthly_avg = 31 * 24 * unit_price
    next_month_price = next_month_hrs * unit_price
    logger.info("Monthly Price: {}".format(monthly_price))
    result = {
        'Pricing': {
                'OperatingSystem': operating_system,
                'TermType': term_type,
                'InstanceType': instance_type,
                'UnitPrice': unit_price,
                'EstCurrMonthPrice': monthly_price,
                '31DayPrice': monthly_avg,
                'NextMonthPrice': next_month_price,
                'HoursLeftInCurrMonth': hours_left,
                'ResponseTime': str(datetime.datetime.utcnow())
            }
    }
    sendResponse(event, context,'SUCCESS', result)
    return result