def create_session()

in traffic_mirroring/app_helper.py [0:0]


def create_session(ec2, instance, config):
    target_id = get_or_create_target(ec2, instance, config)
    filter_id = config["filterId"]
    network_interface_id = instance.network_interface_id
    log.info("Creating a session with source: %s, target: %s, filter: %s", 
            network_interface_id, target_id, filter_id)

    try:
        response = create_traffic_mirror_session(ec2, network_interface_id, target_id, filter_id)
        log.info("Successfully created a traffic mirror session: %s", response)
    except ClientError as e:
        if e.response['Error']['Code'] == 'TrafficMirrorSourcesPerTargetLimitExceeded':
            log.info("Target %s has reached its limit.", target_id)
            # If a target was not supplied in the config one is created dynamically
            if not using_predefined_target(config):
                target_subnet_id = get_target_subnet_id(instance, config)
                log.info("Creating a new target in %s", target_subnet_id)
                create_target(ec2, target_subnet_id, config, target_id)
                create_session(ec2, instance, config)
            else:
                raise e
        else:
            raise e