Function

A Function is a portable code block that can be sent to remote hardware to run as a subroutine or service. It is comprised of the entrypoint, system (Cluster), and requirements necessary to run it.

Function Factory Methods

runhouse.function(fn: str | Callable | None = None, name: str | None = None, system: str | Cluster | None = None, env: str | List[str] | Env | None = None, dryrun: bool = False, load_secrets: bool = False, serialize_notebook_fn: bool = False)[source]

Builds an instance of Function.

Parameters:
  • fn (Optional[str or Callable]) – The function to execute on the remote system when the function is called.

  • name (Optional[str]) – Name of the Function to create or retrieve. This can be either from a local config or from the RNS.

  • system (Optional[str or Cluster]) – Hardware (cluster) on which to execute the Function. This can be either the string name of a Cluster object, or a Cluster object.

  • env (Optional[List[str] or Env or str]) – List of requirements to install on the remote cluster, or path to the requirements.txt file, or Env object or string name of an Env object.

  • dryrun (bool) – Whether to create the Function if it doesn’t exist, or load the Function object as a dryrun. (Default: False)

  • load_secrets (bool) – Whether or not to send secrets; only applicable if dryrun is set to False. (Default: False)

  • serialize_notebook_fn (bool) – If function is of a notebook setting, whether or not to serialized the function. (Default: False)

Returns:

The resulting Function object.

Return type:

Function

Example

runhouse.aws_lambda_fn(fn: Callable | None = None, name: str | None = None, env: Dict | List[str] | Env | None = None, runtime: str | None = None, timeout: int | None = None, memory_size: int | None = None, tmp_size: int | None = None, retention_time: int | None = None, dryrun: bool = False)[source]

Builds an instance of LambdaFunction.

Parameters:
  • fn (Optional[Callable]) – The Lambda function to be executed.

  • name (Optional[str]) – Name of the Lambda Function to create or retrieve. This can be either from a local config or from the RNS.

  • env (Optional[Dict or List[str] or Env]) –

    Specifies the requirements that will be installed, and the environment vars that should be attached to the Lambda. Accepts three possible types:

    1. A dict which should contain the following keys:

      a. reqs: a list of the python libraries, to be installed by the Lambda, or just a requirements.txt string.

      1. env_vars: dictionary containing the env_vars that will be a part of the lambda configuration.

    2. A list of strings, containing all the required python packages.

    3. An instance of Runhouse Env class.

    By default, runhouse package will be installed, and env_vars will include {HOME: /tmp/home}.

  • runtime – (Optional[str]): The coding language of the function. Should be one of the following: python3.7, python3.8, python3.9, python3.10, python3.11. (Default: python3.9)

  • timeout – Optional[int]: The maximum amount of time (in seconds) during which the Lambda will run in AWS without timing-out. (Default: 900, Min: 3, Max: 900)

  • memory_size – Optional[int], The amount of memory (in MB) to be allocated to the Lambda. (Default: 10240, Min: 128, Max: 10240)

  • tmp_size – Optional[int], This size of the /tmp folder in the aws lambda file system. (Default: 10240, Min: 512, Max: 10240).

  • retention_time – Optional[int] The time (in days) the Lambda execution logs will be saved in AWS cloudwatch. After that, they will be deleted. (Default: 30 days)

  • dryrun (bool) – Whether to create the Function if it doesn’t exist, or load the Function object as a dryrun. (Default: False).

Returns:

The resulting AWS Lambda Function object.

Note

When creating a Lambda function for the first time (not reloading it), a callable function is a mandatory argument.

Return type:

LambdaFunction

Examples

Function Class

class runhouse.Function(fn_pointers: Tuple | None = None, name: str | None = None, system: Cluster | None = None, env: Env | None = None, dryrun: bool = False, **kwargs)[source]
__init__(fn_pointers: Tuple | None = None, name: str | None = None, system: Cluster | None = None, env: Env | None = None, dryrun: bool = False, **kwargs)[source]

Runhouse Function object. It is comprised of the entrypoint, system/cluster, and dependencies necessary to run the service.

Note

To create a Function, please use the factory method function().

classmethod from_config(config: dict, dryrun: bool = False)[source]

Create a Function object from a config dictionary.

get(run_key)[source]

Get the result of a Function call that was submitted as async using run.

Parameters:

run_key – A single or list of runhouse run_key strings returned by a Function.remote() call. The ObjectRefs must be from the cluster that this Function is running on.

Example

http_url(curl_command=False, *args, **kwargs) β†’ str[source]

Return the endpoint needed to run the Function on the remote cluster, or provide the curl command if requested.

keep_warm(autostop_mins=None)[source]

Keep the system warm for autostop_mins. If autostop_mins is None or -1, keep warm indefinitely.

Example

method_signature(method)[source]

Extracts the properties of a method that we want to preserve when sending the method over the wire.

notebook(persist=False, sync_package_on_close=None, port_forward=8888)[source]

Tunnel into and launch notebook from the system.

remote(*args, local=True, **kwargs)[source]

Helper property to allow for access to remote properties, both public and private. Returning functions is not advised.

Example

share(*args, visibility=None, **kwargs)[source]

Grant access to the resource for a list of users (or a single user). If a user has a Runhouse account they will receive an email notifying them of their new access. If the user does not have a Runhouse account they will also receive instructions on creating one, after which they will be able to have access to the Resource. If visibility is set to public, users will not be notified.

Note

You can only grant access to other users if you have write access to the resource.

Parameters:
  • users (Union[str, list], optional) – Single user or list of user emails and / or runhouse account usernames. If none are provided and visibility is set to public, resource will be made publicly available to all users.

  • access_level (ResourceAccess, optional) – Access level to provide for the resource. Defaults to read.

  • visibility (ResourceVisibility, optional) – Type of visibility to provide for the shared resource. Defaults to private.

  • notify_users (bool, optional) – Whether to send an email notification to users who have been given access. Note: This is relevant for resources which are not shareable. Defaults to True.

  • headers (dict, optional) – Request headers to provide for the request to RNS. Contains the user’s auth token. Example: {"Authorization": f"Bearer {token}"}

Returns:

added_users:

Users who already have a Runhouse account and have been granted access to the resource.

new_users:

Users who do not have Runhouse accounts and received notifications via their emails.

valid_users:

Set of valid usernames and emails from users parameter.

Return type:

Tuple(Dict, Dict, Set)

Example

to(system: str | Cluster | None = None, env: List[str] | Env = [], force_install: bool = False)[source]

Set up a Function and Env on the given system. If the function is sent to AWS, the system should be aws_lambda See the args of the factory method function() for more information.

Example

Lambda Function Class

Note

Lambda Function support is an alpha and under active development. Please report any bugs and let us know of any feature requests.

A Lambda Function is a serverless compute service provided by AWS. It allows you to run applications and backend services without provisioning or managing servers. Runhouse will allow you to maintain, invoke and share your Lambda functions and their code. It is comprised of the entry point, configuration, and dependencies necessary to run the service.
There are few core options to create an AWS Lambda using Runhouse:
  1. Pass a callable Python function to the factory method.

  2. Follow a typical Lambda creation flow (as if you are using AWS APIs). That includes passing path(s) to Python files(s) (containing the code) and a handler function name to the from_handler_file() method. Arguments such as runtime, Lambda name, timeout and memory size are accepted by the from_handler_file() method as well, but they are not mandatory and have default values.

  3. Create rh.function instance, and than send it over to AWS Lambdas. For example: rh.function(summer).to(system="aws_lambda")

class runhouse.LambdaFunction(paths_to_code: List[str], handler_function_name: str, runtime: str, fn_pointers: tuple, name: str, env: Env, timeout: int, memory_size: int, tmp_size: int, retention_time: int = 30, dryrun: bool = False, access: str | None = None, **kwargs)[source]
__init__(paths_to_code: List[str], handler_function_name: str, runtime: str, fn_pointers: tuple, name: str, env: Env, timeout: int, memory_size: int, tmp_size: int, retention_time: int = 30, dryrun: bool = False, access: str | None = None, **kwargs)[source]

Runhouse AWS Lambda object. It is comprised of the entry point, configuration, and dependencies necessary to run the service on AWS infra.

Note

To create an AWS lambda resource, please use the factory method aws_lambda_fn().

classmethod from_config(config: dict, dryrun: bool = False)[source]

Create an AWS Lambda object from a config dictionary.

classmethod from_handler_file(paths_to_code: List[str], handler_function_name: str, name: str | None = None, env: dict | None = None, runtime: str | None = None, timeout: int | None = None, memory_size: int | None = None, tmp_size: int | None = None, retention_time: int | None = None, dryrun: bool = False)[source]

Creates an AWS Lambda function from a Python file.

Parameters:
  • paths_to_code – (Optional[List[str]]): List of the FULL paths to the Python code file(s) that should be sent to AWS Lambda. First path in the list should be the path to the handler file which contains the main (handler) function. If fn is provided, this argument is ignored.

  • handler_function_name – (Optional[str]): The name of the function in the handler file that will be executed by the Lambda. If fn is provided, this argument is ignored.

  • runtime – (Optional[str]): The coding language of the function. Should be one of the following: python3.7, python3.8, python3.9, python3.10, python3.11. (Default: python3.9)

  • name (Optional[str]) – Name of the Lambda Function to create or retrieve. This can be either from a local config or from the RNS.

  • env (Optional[Dict or List[str] or Env]) –

    Specifies the requirements that will be installed, and the environment vars that should be attached to the Lambda. Accepts three possible types:

    1. A dict which should contain the following keys:

      reqs: a list of the python libraries, to be installed by the Lambda, or just a ``requirements.txt``string.

      env_vars: dictionary containing the env_vars that will be a part of the lambda configuration.

    2. A list of strings, containing all the required python packages.

    3. An instance of Runhouse Env class. By default, runhouse package will be installed, and env_vars will include {HOME: /tmp/home}.

  • timeout – Optional[int]: The maximum amount of time (in seconds) during which the Lambda will run in AWS without timing-out. (Default: 900, Min: 3, Max: 900)

  • memory_size – Optional[int], The amount of memory (in MB) to be allocated to the Lambda. (Default: 10240, Min: 128, Max: 10240)

  • tmp_size – Optional[int], This size of the /tmp folder in the aws lambda file system. (Default: 10240, Min: 512, Max: 10240).

  • retention_time – Optional[int] The time (in days) the Lambda execution logs will be saved in AWS cloudwatch. After that, they will be deleted. (Default: 30 days)

  • dryrun (bool) – Whether to create the Function if it doesn’t exist, or load the Function object as a dryrun. (Default: False).

Returns:

The resulting AWS Lambda Function object.

Return type:

LambdaFunction

Example

classmethod from_name(name, dryrun=False, alt_options=None)[source]

Load existing Resource via its name.

map(*args, **kwargs)[source]

Map a function over a list of arguments.

Example

starmap(args_lists, **kwargs)[source]

Like map() except that the elements of the iterable are expected to be iterables that are unpacked as arguments. An iterable of [(1,2), (3, 4)] results in [func(1,2), func(3,4)].

Example

teardown()[source]

Deletes a Lambda function instance from AWS. All relevant AWS resources (role, log group) are deleted as well.

Example

Lambda Hardware Setup

To create an AWS Lambda, you must grant the necessary permissions to do so. They are provided by an IAM role, which should be attached to a certain AWS profile. This setup is made in the ~/.aws/config file.

For example, your local ~/.aws/config contains:

There are several ways to provide the necessary credentials which enables Lambda creation:

  1. Specify the profile name in your code editor:

    1. Install the AWS Toolkit extension.

    2. Choose the relevant profile (e.g. profile lambda) and region (e.g us-east-1).

  2. Environment Variable: setting AWS_PROFILE to "lambda"

Note

If no specific profile is provided, Runhouse will try using the default profile. Note if this default AWS identity will not have the relevant IAM permissions for creating a Lambda, you will not be able to create a Lambda.