Snapshots let you capture the filesystem and/or memory of a Sandbox as an immutable artifact. You can then use this artifact to create new Sandboxes with that same captured state. Use snapshots when you want to:
  • Fork Sandboxes to test different variations of code
  • Initialize Sandboxes with existing state for faster cold starts
  • Save a reproducible environment you can return to later

Creating a Filesystem Snapshot

from beam import Image, Sandbox

# Create a sandbox and make some changes to the filesystem
sandbox = Sandbox(cpu=1).create()
p = sandbox.process.exec("sh", "-c", "mkdir -p /something && touch /something/file.txt")

# Read the logs
p.wait()
print(p.logs.read())

# Generate a filesystem snapshot and terminate the sandbox
image_id = sandbox.create_image_from_filesystem()
sandbox.terminate()

Using Filesystem Snapshots

You can use Snapshots as a base image for any other abstraction or Sandbox on Beam, using Image.from_id:
from beam import Image, Sandbox

# Creates an image from a filesystem snapshot
image = Image.from_id(image_id)

sandbox = Sandbox(image=image).create()
p = sandbox.process.exec("ls", "-l", "/something")

p.wait()
print(p.logs.read())

sandbox.terminate()

Creating a Memory Snapshot

You can also create a memory snapshot of a running Sandbox, which will capture the state of the sandbox’s memory - including all running processes and exposed ports.
from beam import Image, Sandbox

# Create a sandbox and make some changes to the filesystem
sandbox = Sandbox(cpu=1).create()
sandbox.expose_port(8000)

p = sandbox.process.exec("python", "-c", "import http.server; http.server.HTTPServer(('', 8000), http.server.SimpleHTTPRequestHandler).serve_forever()")

# Generate a memory snapshot and terminate the sandbox
snapshot_id = sandbox.snapshot_memory()

print(sandbox.list_urls())

sandbox.terminate()

Using Memory Snapshots

You can use memory snapshots as a starting point for a new Sandbox, using Sandbox().create_from_memory_snapshot:
from beam import Image, Sandbox

# Creates a new sandbox from a memory snapshot

sandbox = Sandbox().create_from_memory_snapshot(snapshot_id)

print(sandbox.list_urls())

sandbox.terminate()