Beam’s sandboxed code execution is a feature that allows you to run arbitrary code in a secure and isolated environment.

You can run sessions that terminate after each request, or keep them running indefinitelty.

Creating a Sandbox

You can create a sandbox by using the Pod object. Pods can be created using the CLI or the Python SDK.

from beam import Image, Pod

notebook = Pod(
    image=Image(base_image="jupyter/base-notebook:latest"),
    ports=[8888],
    cpu=1,
    memory=1024,
    env={
        "NOTEBOOK_ARGS": "--ip=0.0.0.0 --NotebookApp.token='' --NotebookApp.notebook_dir=/tmp"
    },
    entrypoint=["start-notebook.py"],
)

res = notebook.create()

# pod-384ced3c-f837-4429-bada-39e0b965c9f4-594be4af
print("Container ID:", res.container_id)
# https://384ced3c-f837-4429-bada-39e0b965c9f4-8888.app.beam.cloud
print("Sandbox URL:", res.url)

Parameters

Pods can be heavily customized to fit your needs.

Using Custom Images

You can customize the container image using the Image object. This can be customized with Python packages, shell commands, Conda packages, and much more.

from beam import Image, Pod

pod = Pod(
    image=Image(base_image="jupyter/base-notebook:latest"),
    entrypoint=["start-notebook.py"],
)

Specifying Entry Points

An entry point is the command or script that the container runs when it starts. In Beam, you can interact with Pods using the CLI or the Python SDK.

from beam import Image, Pod

pod = Pod(
    image=Image(base_image="jupyter/base-notebook:latest"),
    entrypoint=["start-notebook.py"],
)

pod.create()

Passing Environment Variables

You can pass environment variables into your container for credentials or other parameters. Like entry points, environment variables can be defined in both the CLI or the Python SDK:

from beam import Image, Pod

Pod(
    image=Image(base_image="jupyter/base-notebook:latest"),
    env={"NOTEBOOK_ARGS": "--ip=0.0.0.0 --NotebookApp.token='' --NotebookApp.notebook_dir=/tmp"},
    entrypoint=["start-notebook.py"],
)

Terminating a Pod

Pods can be terminated directly using the terminate() method.

Alternatively, you can terminate the container the Pod is running on by using the beam container stop <container-id> command.

from beam import Pod

# Initialize a pod
notebook = Pod()

# Launch the pod
notebook.create()

# Terminate the pod
notebook.terminate()

Lifecycle

Timeouts

Pods are serverless and automatically scale-to-zero.

By default, pods will be terminated after 10 minutes without receiving any connection requests. Making a connection request (i.e. accessing the URL in your browser) will keep the container alive, until the timeout is reached.

You can set a custom timeout by passing the keep-warm-seconds parameter when creating a pod.

Keep Alive for 5 minutes

beam run --image jupyter/base-notebook:latest --keep-warm-seconds -300

Keep Alive Indefinitely

There is no upper limit on the duration of a session.
beam run --image jupyter/base-notebook:latest --keep-warm-seconds -1

List Running Pods

You can list all running Pods using the beam container list command.

$ beam container list

  ID                                                  Status    Stub ID                                Deployment ID   Scheduled At    Uptime
 ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  pod-58a38dba-8b7b-4db3-b002-436c6d9b4858-a613eecd   RUNNING   58a38dba-8b7b-4db3-b002-436c6d9b4858                   5 seconds ago   4 seconds

  1 items

You can kill any container by running beam container stop <container-id>.