def lambda_handler()

in source/elb_hostname_as_target.py [0:0]


def lambda_handler(event, context):
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    logger.info("INFO: Received event: {}".format(json.dumps(event)))
    # Get Currently Resgistered IPs list
    registered_ip_list = utils.describe_target_health(ELB_TG_ARN)

    # Create S3 Bucket / Verify Access to selected Bucket
    utils.create_s3_bucket(S3_BUCKET, BUCKET_REGION)

    # Query DNS for hostname IPs
    try:
        hostname_ip_list = []
        i = 1
        while i <= MAX_LOOKUP_PER_INVOCATION:
            dns_lookup_result = utils.dns_lookup(DNS_SERVER, TARGET_FQDN, "A")
            hostname_ip_list = dns_lookup_result + hostname_ip_list
            if len(dns_lookup_result) < 8:
                break
            i += 1
        logger.info(f"INFO: Hostname IPs resolved by DNS lookup: {hostname_ip_list}")

        # IPs that have not been registered, and missing from the old active IP list
        new_ips_to_register_list = list(set(hostname_ip_list) - set(registered_ip_list))

        # Register new targets
        if new_ips_to_register_list:
            utils.register_target(ELB_TG_ARN, new_ips_to_register_list)
            logger.info(f"INFO: Registering {format(new_ips_to_register_list)}")
        else:
            logger.info("INFO: No IPs to register.")

        # Set S3 object name
        s3_ip_list_key = ("Tracked IP list "
                          "for {} - {}".format(TARGET_FQDN, ELB_TG_ARN.replace('/', '-')))

        # Download previous IP Dictionary from S3
        ip_dict = utils.download_ip_list(S3_BUCKET, s3_ip_list_key)

        # Update IP Dictionary with current query results
        temp_hostname_ip_list = list(hostname_ip_list)
        expired_ip_list = []
        for ip in ip_dict:
            if ip not in (hostname_ip_list):
                ip_dict[ip] = ip_dict[ip] - 1
            else:
                temp_hostname_ip_list.remove(ip)
                ip_dict[ip] = INVOCATIONS_BEFORE_DEREGISTRATION
            if ip_dict[ip] == 0:
                expired_ip_list.append(ip)

        # Add new IPs to dictionary
        for ip in temp_hostname_ip_list:
            ip_dict[ip] = INVOCATIONS_BEFORE_DEREGISTRATION

        # If asked to track all IPs - Add all TG IPs to the tracking list
        if REMOVE_UNTRACKED_TG_IP:
            for ip in registered_ip_list:
                if ip not in ip_dict:
                    ip_dict[ip] = INVOCATIONS_BEFORE_DEREGISTRATION

        # Deregister IPs that were missing more than X times from the query
        for ip in expired_ip_list:
            if ip in expired_ip_list:
                ip_dict.pop(ip)

        if expired_ip_list:
            utils.deregister_target(ELB_TG_ARN, expired_ip_list)
        else:
            logger.info("INFO: No IPs to deregister.")

        # Update S3 IP Dictionary
        utils.upload_ip_list(S3_BUCKET, ip_dict, s3_ip_list_key)

        # Update CW metric
        if REPORT_IP_COUNT_CW_METRIC:
            utils.put_metric_data(ip_dict, TARGET_FQDN)

        # Report successful invocation
        logger.info("INFO: Update completed successfuly.")

    # Exception handler
    except Exception as e:
        logger.error("ERROR:", e)
        logger.error("ERROR: Invocation failed.")
        return(1)
    return (0)