> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sailresearch.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Migrate an Amazon EC2 agent worker to Sailboxes

> Give this guide to your coding agent to move a Python worker from Amazon EC2 to a Sailbox. It covers replacing SSH and SCP and checking access to services you keep on AWS.

## Sailboxes vs. EC2 On-Demand: autosleep and billing

A sleeping Sailbox keeps its memory and files. A standard Sailbox can sleep after at least 30 seconds of idle time by default. The next command or file operation wakes it. Sail stops charging for CPU, RAM, and local disk during sleep. Separate Sail volumes still have storage charges. AWS charges the EC2 On-Demand instance rate while the instance runs, even when it is idle. [Sail autosleep](https://docs.sailresearch.com/sailboxes-autosleep) · [Sail pricing](https://docs.sailresearch.com/sailboxes-pricing.md) · [EC2 On-Demand pricing](https://aws.amazon.com/ec2/pricing/on-demand/)

## Create the Sailbox

Install the [Sail Python SDK](https://docs.sailresearch.com/reference/python-sdk.md) on the machine that runs your controller. Run `python -m pip install sail`. Sign in with `sail auth login`. Create one Sailbox per session. This example saves its ID in a local file. In your application, store the ID with the agent session.

```python theme={null}
from pathlib import Path
import sail

app = sail.App.find(name="migration-demo", mint_if_missing=True)
sb = sail.Sailbox.create(app=app, name="ec2-worker")
Path("sailbox-id.txt").write_text(sb.sailbox_id)
```

## Transfer files

Replace SCP with the Sail filesystem API. Run this code from the directory that contains `worker.py` and `job.json`. The example worker reads `/tmp/job.json` and writes `/tmp/result.json` inside the Sailbox. Use the paths your worker needs.

```python theme={null}
sb.fs.write("/tmp/worker.py", Path("worker.py").read_bytes())
sb.fs.write("/tmp/job.json", Path("job.json").read_bytes())
```

Sail cannot import an AMI or an EBS snapshot as a Sailbox image. Rebuild the dependencies from your lockfile or [Dockerfile](https://docs.sailresearch.com/sailboxes-images). Stop writes before you copy the final data.

## Run your existing worker

Replace the SSH command with a Sail SDK call. Use the same Python entry point.

On EC2:

```bash theme={null}
ssh ec2-user@EC2_HOST 'python3 /tmp/worker.py'
```

On Sail:

```python theme={null}
result = sb.run(["python3", "/tmp/worker.py"], check=True)
print(result.stdout)
```

EC2 instance roles and private VPC routes do not transfer to Sail. Test AWS authentication and network access from the Sailbox. Let the EC2 worker finish its current jobs before you switch workers.

## Reconnect after sleep

An idle Sailbox [sleeps automatically by default](https://docs.sailresearch.com/sailboxes-autosleep). Use the saved ID to find the same Sailbox. Read the result file. This operation wakes a sleeping Sailbox.

```python theme={null}
from pathlib import Path
import sail

sb = sail.Sailbox.get(Path("sailbox-id.txt").read_text().strip())
print(sb.fs.read("/tmp/result.json").decode())
```

Sleep preserves files and memory. [Termination deletes the Sailbox.](https://docs.sailresearch.com/sailboxes-lifecycle.md)

## Check the worker output

Run your worker with the same input on EC2 and on Sail. Compare the command output and the files from both runs.
